Skip to content

Commit 791ba35

Browse files
committed
修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题
1 parent 5c510bf commit 791ba35

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* 【cron 】 修复SystemTimer无法结束进程问题(issue#3090@Github)
3333
* 【core 】 修复BeanUtil.copyToList复制Long等类型错误问题(issue#3091@Github)
3434
* 【poi 】 修复MapRowHandler结果Map无序问题(issue#I71SE8@Github)
35+
* 【db 】 修复SqlExecutor.execute执行ORACLE insert into select报ORA-00933问题(issue#I778U7@Github)
3536

3637
-------------------------------------------------------------------------------------------------------------
3738
# 5.8.18 (2023-04-27)

hutool-db/src/main/java/cn/hutool/db/StatementUtil.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ public static PreparedStatement prepareStatement(Connection conn, String sql, Co
124124
* @since 3.2.3
125125
*/
126126
public static PreparedStatement prepareStatement(Connection conn, String sql, Object... params) throws SQLException {
127+
return prepareStatement(GlobalDbConfig.returnGeneratedKey, conn, sql, params);
128+
}
129+
130+
/**
131+
* 创建{@link PreparedStatement}
132+
*
133+
* @param returnGeneratedKey 当为insert语句时,是否返回主键
134+
* @param conn 数据库连接
135+
* @param sql SQL语句,使用"?"做为占位符
136+
* @param params "?"对应参数列表
137+
* @return {@link PreparedStatement}
138+
* @throws SQLException SQL异常
139+
* @since 5.8.19
140+
*/
141+
public static PreparedStatement prepareStatement(boolean returnGeneratedKey, Connection conn, String sql, Object... params) throws SQLException {
127142
Assert.notBlank(sql, "Sql String must be not blank!");
128143
sql = sql.trim();
129144

@@ -136,7 +151,7 @@ public static PreparedStatement prepareStatement(Connection conn, String sql, Ob
136151

137152
SqlLog.INSTANCE.log(sql, ArrayUtil.isEmpty(params) ? null : params);
138153
PreparedStatement ps;
139-
if (GlobalDbConfig.returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
154+
if (returnGeneratedKey && StrUtil.startWithIgnoreCase(sql, "insert")) {
140155
// 插入默认返回主键
141156
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
142157
} else {

hutool-db/src/main/java/cn/hutool/db/sql/SqlExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static int execute(Connection conn, String sql, Map<String, Object> param
5454
public static int execute(Connection conn, String sql, Object... params) throws SQLException {
5555
PreparedStatement ps = null;
5656
try {
57-
ps = StatementUtil.prepareStatement(conn, sql, params);
57+
ps = StatementUtil.prepareStatement(false, conn, sql, params);
5858
return ps.executeUpdate();
5959
} finally {
6060
DbUtil.close(ps);
@@ -128,7 +128,7 @@ public static Long executeForGeneratedKey(Connection conn, String sql, Object...
128128
PreparedStatement ps = null;
129129
ResultSet rs = null;
130130
try {
131-
ps = StatementUtil.prepareStatement(conn, sql, params);
131+
ps = StatementUtil.prepareStatement(true, conn, sql, params);
132132
ps.executeUpdate();
133133
rs = ps.getGeneratedKeys();
134134
if (rs != null && rs.next()) {
@@ -271,7 +271,7 @@ public static <T> T query(Connection conn, SqlBuilder sqlBuilder, RsHandler<T> r
271271
public static <T> T query(Connection conn, String sql, RsHandler<T> rsh, Object... params) throws SQLException {
272272
PreparedStatement ps = null;
273273
try {
274-
ps = StatementUtil.prepareStatement(conn, sql, params);
274+
ps = StatementUtil.prepareStatement(false, conn, sql, params);
275275
return executeQuery(ps, rsh);
276276
} finally {
277277
DbUtil.close(ps);

0 commit comments

Comments
 (0)