eat*_*ode 4 java connection jdbc autocommit
如果我这样connection.setAutoCommit(false);做会在数据库端创建新的事务,该怎么办?
让我用代码简单解释一下。当我们申请了
Connection.setAutoCommit(假);
在我们的源代码中,它将禁用自动提交选项,该选项默认在数据库中启用。
所以,你必须打电话
连接.commit();
方法显式地保存对数据库的任何更改。
Class.forName(drivers);
Connection dbConnnection=DriverManager.getConnection(connectionURL,username,password);
dbConnection.setAutoCommit(false); //Disabling the Autocommit
Statement selectStatement = dbConnection.createStatement("Select Query");
ResultSet rs = selectStatement.execute();
while(rs.next()){
Statement updateStatement = dbConnection.createStatement("update Query");
//Apply some changes to update record
statement.execute();
dbConnection.commit(); //Mandatory to execute to persist changes into database
}
Run Code Online (Sandbox Code Playgroud)