本文整理汇总了Java中wherehows.common.utils.PreparedStatementUtil类的典型用法代码示例。如果您正苦于以下问题:Java PreparedStatementUtil类的具体用法?Java PreparedStatementUtil怎么用?Java PreparedStatementUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PreparedStatementUtil类属于wherehows.common.utils包,在下文中一共展示了PreparedStatementUtil类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: insert
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
/**
* use JDBC template PreparedStatement to insert records
* require record to have getAllValues() and optional getDbColumnNames() method
* @return boolean if the insert is successful
* @throws SQLException
*/
public boolean insert() throws SQLException, IllegalAccessException {
if (records.size() == 0 || !(records.get(0) instanceof AbstractRecord)) {
logger.debug("DatabaseWriter no record to insert or unknown record Class.");
System.out.println("DatabaseWriter no record or unknown record Class: " + records.size() + " | " + records.get(0) + " | " + (records.get(0) instanceof AbstractRecord));
return false;
}
final AbstractRecord record0 = (AbstractRecord) records.get(0);
final String[] columnNames = record0.getDbColumnNames();
final String sql =
(columnNames != null) ? PreparedStatementUtil.prepareInsertTemplateWithColumn(tableName, columnNames)
: PreparedStatementUtil.prepareInsertTemplateWithoutColumn(tableName, record0.getAllFields().length);
logger.info("sql string: " + sql);
logger.error("columnNames: " + columnNames);
System.out.println("sql: " + sql + " | " + columnNames);
//logger.debug("DatabaseWriter template for " + record0.getClass() + " : " + sql);
for (final Record record : records) {
jdbcTemplate.update(sql, ((AbstractRecord) record).getAllValuesToString());
}
records.clear();
return true;
}
示例2: update
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
/**
* use JDBC template PreparedStatement to update row in database by setting columns with conditions
* @param columns String[] column names to update
* @param columnValues Object[] new values
* @param conditions String[] conditions
* @param conditionValues Object[] condition values
*/
public void update(String[] columns, Object[] columnValues, String[] conditions, Object[] conditionValues)
throws DataAccessException {
if (columns.length != columnValues.length || conditions.length != conditionValues.length) {
logger.error("DatabaseWriter update columns and values mismatch");
return;
}
Object[] values = Arrays.copyOf(columnValues, columnValues.length + conditionValues.length);
System.arraycopy(conditionValues, 0, values, columnValues.length, conditionValues.length);
final String sql = PreparedStatementUtil.prepareUpdateTemplateWithColumn(tableName, columns, conditions);
//logger.debug("DatabaseWriter template for " + record0.getClass() + " : " + sql);
execute(sql, values);
}
示例3: insert
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
/**
* use JDBC template PreparedStatement to insert records
* require record to have getAllValues() and optional getDbColumnNames() method
* @return boolean if the insert is successful
* @throws SQLException
*/
public boolean insert()
throws SQLException {
if (records.size() == 0 || !(records.get(0) instanceof AbstractRecord)) {
logger.debug("DatabaseWriter no record to insert or unknown record Class.");
return false;
}
final AbstractRecord record0 = (AbstractRecord) records.get(0);
final String[] columnNames = record0.getDbColumnNames();
final String sql =
(columnNames != null) ? PreparedStatementUtil.prepareInsertTemplateWithColumn(tableName, columnNames)
: PreparedStatementUtil.prepareInsertTemplateWithoutColumn(tableName, record0.getAllFields().length);
//logger.debug("DatabaseWriter template for " + record0.getClass() + " : " + sql);
for (final Record record : records) {
try {
jdbcTemplate.update(sql, ((AbstractRecord) record).getAllValuesToString());
/* jdbcTemplate.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps)
throws SQLException {
record.setPreparedStatementValues(ps);
}
}); */
} catch (IllegalAccessException | DataAccessException ae) {
logger.error("DatabaseWriter insert error: " + ae);
}
}
records.clear();
return true;
}
示例4: insert
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
/**
* use JDBC template PreparedStatement to insert records
* require record to have getAllValues() and optional getDbColumnNames() method
* @return boolean if the insert is successful
* @throws SQLException
*/
public boolean insert()
throws SQLException {
if (records.size() == 0 || !(records.get(0) instanceof AbstractRecord)) {
log.debug("DatabaseWriter no record to insert or unknown record Class.");
return false;
}
final AbstractRecord record0 = (AbstractRecord) records.get(0);
final String[] columnNames = record0.getDbColumnNames();
final String sql =
(columnNames != null) ? PreparedStatementUtil.prepareInsertTemplateWithColumn(tableName, columnNames)
: PreparedStatementUtil.prepareInsertTemplateWithoutColumn(tableName, record0.getAllFields().length);
//logger.debug("DatabaseWriter template for " + record0.getClass() + " : " + sql);
for (final Record record : records) {
try {
jdbcTemplate.update(sql, ((AbstractRecord) record).getAllValuesToString());
/* jdbcTemplate.update(sql, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps)
throws SQLException {
record.setPreparedStatementValues(ps);
}
}); */
} catch (IllegalAccessException | DataAccessException ae) {
log.error("DatabaseWriter insert error: " + ae);
}
}
records.clear();
return true;
}
示例5: update
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
/**
* use JDBC template PreparedStatement to update row in database by setting columns with conditions
* @param columns String[] column names to update
* @param columnValues Object[] new values
* @param conditions String[] conditions
* @param conditionValues Object[] condition values
*/
public void update(String[] columns, Object[] columnValues, String[] conditions, Object[] conditionValues)
throws DataAccessException {
if (columns.length != columnValues.length || conditions.length != conditionValues.length) {
log.error("DatabaseWriter update columns and values mismatch");
return;
}
Object[] values = Arrays.copyOf(columnValues, columnValues.length + conditionValues.length);
System.arraycopy(conditionValues, 0, values, columnValues.length, conditionValues.length);
final String sql = PreparedStatementUtil.prepareUpdateTemplateWithColumn(tableName, columns, conditions);
//logger.debug("DatabaseWriter template for " + record0.getClass() + " : " + sql);
execute(sql, values);
}
示例6: updateDatasetOwner
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
public static void updateDatasetOwner(JsonNode root)
throws Exception {
final JsonNode owners = root.path("owners");
if (owners.isMissingNode() || !owners.isArray()) {
throw new IllegalArgumentException(
"Dataset owner info update fail, missing necessary fields: " + root.toString());
}
final JsonNode ownerSourceNode = root.path("source");
String ownerSource = null;
if (!ownerSourceNode.isNull() && !ownerSourceNode.isMissingNode()) {
ownerSource = ownerSourceNode.asText();
}
final Integer datasetId;
final String urn;
final Object[] idUrn = findDataset(root);
if (idUrn[0] == null || idUrn[1] == null) {
datasetId = 0;
urn = root.path("datasetProperties").path("uri").asText();
} else {
datasetId = (Integer) idUrn[0];
urn = (String) idUrn[1];
}
final JsonNode auditHeader = root.path("auditHeader");
final Long eventTime = auditHeader != null ? auditHeader.path("time").asLong() / 1000 : null;
ObjectMapper om = new ObjectMapper();
List<DatasetOwnerRecord> ownerList = new ArrayList<>();
for (final JsonNode owner : owners) {
DatasetOwnerRecord record = om.convertValue(owner, DatasetOwnerRecord.class);
record.setDatasetId(datasetId);
record.setDatasetUrn(urn);
record.setSourceTime(eventTime);
record.setCreatedTime(eventTime);
record.setModifiedTime(System.currentTimeMillis() / 1000);
final String ownerString = record.getOwner();
int lastIndex = ownerString.lastIndexOf(':');
if (lastIndex >= 0) {
record.setOwner(ownerString.substring(lastIndex + 1));
record.setNamespace(ownerString.substring(0, lastIndex));
} else {
record.setNamespace("");
}
Map<String, Object> ownerInfo = getOwnerByOwnerId(record.getOwner());
Integer appId = 0;
String isActive = "N";
if (ownerInfo.containsKey("app_id")) {
appId = StringUtil.toInt(ownerInfo.get("app_id"));
isActive = appId == 301 ? "Y" : appId == 300 ? (String) ownerInfo.get("is_active") : "N";
}
record.setAppId(appId);
record.setIsActive(isActive);
String ownerTypeString = record.getOwnerType();
record.setIsGroup(ownerTypeString != null && ownerTypeString.equalsIgnoreCase("group") ? "Y" : "N");
if (datasetId == 0 || appId == 0) {
String sql = PreparedStatementUtil.prepareInsertTemplateWithColumn(DATASET_OWNER_UNMATCHED_TABLE,
record.getDbColumnForUnmatchedOwner());
OWNER_UNMATCHED_WRITER.execute(sql, record.getValuesForUnmatchedOwner());
} else {
ownerList.add(record);
}
}
mergeDatasetOwners(ownerList, datasetId, urn, ownerSource);
}
示例7: updateDatasetOwner
import wherehows.common.utils.PreparedStatementUtil; //导入依赖的package包/类
public static void updateDatasetOwner(JsonNode root)
throws Exception {
final JsonNode owners = root.path("owners");
if (owners.isMissingNode() || !owners.isArray()) {
throw new IllegalArgumentException(
"Dataset owner info update fail, missing necessary fields: " + root.toString());
}
final JsonNode ownerSourceNode = root.path("source");
String ownerSource = null;
if (!ownerSourceNode.isNull() && !ownerSourceNode.isMissingNode()) {
ownerSource = ownerSourceNode.asText();
}
final Integer datasetId;
final String urn;
final Object[] idUrn = findDataset(root);
if (idUrn[0] == null || idUrn[1] == null) {
datasetId = 0;
urn = root.path("datasetProperties").path("uri").asText();
} else {
datasetId = (Integer) idUrn[0];
urn = (String) idUrn[1];
}
final JsonNode auditHeader = root.path("auditHeader");
final Long eventTime = auditHeader != null ? auditHeader.path("time").asLong() / 1000 : null;
ObjectMapper om = new ObjectMapper();
List<DatasetOwnerRecord> ownerList = new ArrayList<>();
for (final JsonNode owner : owners) {
DatasetOwnerRecord record = om.convertValue(owner, DatasetOwnerRecord.class);
record.setDatasetId(datasetId);
record.setDatasetUrn(urn);
record.setSourceTime(eventTime);
record.setCreatedTime(eventTime);
record.setModifiedTime(System.currentTimeMillis() / 1000);
final String ownerString = record.getOwner();
int lastIndex = ownerString.lastIndexOf(':');
if (lastIndex >= 0) {
record.setOwner(ownerString.substring(lastIndex + 1));
record.setNamespace(ownerString.substring(0, lastIndex));
} else {
record.setNamespace("");
}
Map<String, Object> ownerInfo = getOwnerByOwnerId(record.getOwner());
Integer appId = 0;
String isActive = "N";
if (ownerInfo.containsKey("app_id")) {
appId = StringUtil.toInt(ownerInfo.get("app_id"));
isActive = appId == 301 ? "Y" : appId == 300 ? (String) ownerInfo.get("is_active") : "N";
}
record.setAppId(appId);
record.setIsActive(isActive);
String ownerTypeString = record.getOwnerType();
record.setIsGroup(ownerTypeString != null && ownerTypeString.equalsIgnoreCase("group") ? "Y" : "N");
if (datasetId == 0 || appId == 0) {
String sql = PreparedStatementUtil.prepareInsertTemplateWithColumn("REPLACE", DATASET_OWNER_UNMATCHED_TABLE,
record.getDbColumnForUnmatchedOwner());
OWNER_UNMATCHED_WRITER.execute(sql, record.getValuesForUnmatchedOwner());
} else {
ownerList.add(record);
}
}
mergeDatasetOwners(ownerList, datasetId, urn, ownerSource);
}