use of org.sagacity.sqltoy.callback.PreparedStatementResultHandler in project sagacity-sqltoy by chenrenfei.
the class SqlUtil method executeSql.
/**
* @todo 执行Sql语句完成修改操作
* @param typeHandler
* @param executeSql
* @param params
* @param paramsType
* @param conn
* @param dbType
* @param autoCommit
* @param processWord
* @return
* @throws Exception
*/
public static Long executeSql(TypeHandler typeHandler, final String executeSql, final Object[] params, final Integer[] paramsType, final Connection conn, final Integer dbType, final Boolean autoCommit, boolean processWord) throws Exception {
// 对sql进行关键词符号替换
String realSql = processWord ? ReservedWordsUtil.convertSql(executeSql, dbType) : executeSql;
SqlExecuteStat.showSql("execute sql=", realSql, params);
boolean hasSetAutoCommit = false;
Long updateCounts = null;
if (autoCommit != null) {
if (!autoCommit == conn.getAutoCommit()) {
conn.setAutoCommit(autoCommit);
hasSetAutoCommit = true;
}
}
PreparedStatement pst = conn.prepareStatement(realSql);
Object result = preparedStatementProcess(null, pst, null, new PreparedStatementResultHandler() {
@Override
public void execute(Object obj, PreparedStatement pst, ResultSet rs) throws SQLException, IOException {
// sqlserver 存在timestamp不能赋值问题,通过对象完成的修改、插入忽视掉timestamp列
if (dbType == DBType.SQLSERVER && paramsType != null) {
setSqlServerParamsValue(typeHandler, conn, dbType, pst, params, paramsType, 0);
} else {
setParamsValue(typeHandler, conn, dbType, pst, params, paramsType, 0);
}
pst.executeUpdate();
// 返回update的记录数量
this.setResult(Long.valueOf(pst.getUpdateCount()));
}
});
if (result != null) {
updateCounts = (Long) result;
}
if (hasSetAutoCommit && autoCommit != null) {
conn.setAutoCommit(!autoCommit);
}
return updateCounts;
}
Aggregations