我正在尝试执行以下代码。
public static void main(String[] args) throws Exception {
Connection connection = null;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
System.out.println();
connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
connection.setAutoCommit(false);
// String plsql = "BEGIN INSERT INTO t VALUES(42); COMMIT; END;";
String plsql = "BEGIN UPDATE TABLE SET PROPERTY_VALUE='test6' where property_name='TEST'; COMMIT; END;";
PreparedStatement pstatement = connection.prepareStatement(plsql);
pstatement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
我收到了以下错误:
com.ibm.db2.jcc.b.co: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=END;NAME='TEST'; COMMIT;;END, DRIVER=3.53.70
如何解决此问题。为什么准备好的声明在这种情况下支持?
答案 0 :(得分:0)
我认为您需要使用对commit
的JDBC调用来connection.commit()
事务。
这个article为您提供了一个很好的例子。
您的代码将更改为正文中的以下内容
public static void main(String[] args) throws Exception {
Connection connection = null;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
try {
System.out.println();
connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
connection = DriverManager.getConnection("jdbc:db2://localhost:port/database", username, password);
connection.setAutoCommit(false);
String plsql = "UPDATE AR01.REBILL_PROPERTIES SET PROPERTY_VALUE='test6' where property_name='TEST'";
PreparedStatement pstatement = connection.prepareStatement(plsql);
pstatement.executeUpdate();
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
}
}
希望这有帮助。