我试图从Java程序中运行复合DB2语句(一组SQL语句)。请提供一些建议。下面是我试过但不起作用的示例程序。基本上,在CallableStatement中不允许使用多个SQL语句。
Connection dbConnection = null;
CallableStatement callableStatement = null;
try {
dbConnection = getDBConnection();
dbConnection.setAutoCommit(false);
String sql= "DECLARE BEGIN " +
"INSERT INTO EMPLOYEE(EmpId, EmpName) VALUES (1, 'Name1'); " +
"INSERT INTO EMPLOYEE(EmpId, EmpName) VALUES (2, 'Name2'); " +
"END";
callableStatement = dbConnection.prepareCall(sql);
callableStatement.executeUpdate();
dbConnection.commit();
} catch (SQLException e) {
dbConnection.rollback();
System.out.println(e.getMessage());
} finally {
if (callableStatement != null) {
callableStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}发布于 2019-11-16 08:26:03
试试这个:
dbConnection = getDBConnection();
dbConnection.setAutoCommit(false);
String sql= "BEGIN " +
"INSERT INTO EMPLOYEE(EmpId, EmpName) VALUES (1, 'Name1'); " +
"INSERT INTO EMPLOYEE(EmpId, EmpName) VALUES (2, 'Name2'); " +
"END";
callableStatement = dbConnection.prepareCall(sql);
callableStatement.execute();如果您的Db2版本和平台不能工作,请提供完整的错误消息/堆栈跟踪。
https://stackoverflow.com/questions/58887765
复制相似问题