我正在尝试从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();
}
}
最佳答案
试试这个:
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版本和平台。