Java 我正在尝试执行jdbc事务,但一旦我运行代码,数据库就不会更新
我在Eclipse中使用了以下代码。一旦我运行代码,数据库就不会更新。它仍然显示相同的值。我甚至在数据库中运行了sql查询,该查询运行得很好,但一旦我运行java代码,就不会更新任何内容Java 我正在尝试执行jdbc事务,但一旦我运行代码,数据库就不会更新,java,mysql,jdbc,transactions,Java,Mysql,Jdbc,Transactions,我在Eclipse中使用了以下代码。一旦我运行代码,数据库就不会更新。它仍然显示相同的值。我甚至在数据库中运行了sql查询,该查询运行得很好,但一旦我运行java代码,就不会更新任何内容 import java.io.*; import java.sql.*; import java.util.Properties; public class ExpTran { public static void main(String[] args) {
import java.io.*;
import java.sql.*;
import java.util.Properties;
public class ExpTran {
public static void main(String[] args) {
Connection con = null;
try {
FileInputStream io = new FileInputStream("config/db.properties");
Properties p = new Properties();
p.load(io);
Class.forName(p.getProperty("driver"));
con = DriverManager.getConnection(p.getProperty("url"),p);
Statement stmt = con.createStatement();
con.setAutoCommit(false);
int i = stmt.executeUpdate("update tutorials.account set amount=amount-500 where acc_no='A002'");
i += stmt.executeUpdate("update tutorials.account set amount=amount+500 where acc_no='A003'");
if(i!=2)
{
System.out.println("Exception");
throw new SQLException();
}
System.out.println(i);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
if(con!=null){
try {
con.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e.printStackTrace();
}
finally {
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}您定义了con.setAutoCommitfalse;,因此,您需要在完成操作时提交连接
对于此用途:
您定义了con.setAutoCommitfalse;,因此,您需要在完成操作时提交连接
对于此用途:
您必须通过调用con.commit显式提交更改;如果定义con.setAutoCommitfalse 还应避免对多个操作使用单个try/catch/finally块
public static void main(String[] args) {
Properties properties = new Properties();
try (FileInputStream propertiesInputStream = new FileInputStream("config/db.properties")) {
properties.load(propertiesInputStream);
} catch (IOException e) {
throw new RuntimeException("Error loading properties file.", e);
}
String driverClassName = properties.getProperty("driver");
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Error loading JDBC driver class.", e);
}
String databaseUrl = properties.getProperty("url");
try (Connection connection = DriverManager.getConnection(databaseUrl, properties)) {
connection.setAutoCommit(false);
Statement stmt = connection.createStatement();
executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount-500 where acc_no='A002'");
executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount+500 where acc_no='A003'");
connection.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static void executeUpdateAndCheck(Statement stmt, String command) throws SQLException {
int result = stmt.executeUpdate(command);
if (result == 0) {
stmt.getConnection().rollback();
throw new IllegalStateException();
}
}
您必须通过调用con.commit显式提交更改;如果定义con.setAutoCommitfalse 还应避免对多个操作使用单个try/catch/finally块
public static void main(String[] args) {
Properties properties = new Properties();
try (FileInputStream propertiesInputStream = new FileInputStream("config/db.properties")) {
properties.load(propertiesInputStream);
} catch (IOException e) {
throw new RuntimeException("Error loading properties file.", e);
}
String driverClassName = properties.getProperty("driver");
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Error loading JDBC driver class.", e);
}
String databaseUrl = properties.getProperty("url");
try (Connection connection = DriverManager.getConnection(databaseUrl, properties)) {
connection.setAutoCommit(false);
Statement stmt = connection.createStatement();
executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount-500 where acc_no='A002'");
executeUpdateAndCheck(stmt, "update tutorials.account set amount=amount+500 where acc_no='A003'");
connection.commit();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
private static void executeUpdateAndCheck(Statement stmt, String command) throws SQLException {
int result = stmt.executeUpdate(command);
if (result == 0) {
stmt.getConnection().rollback();
throw new IllegalStateException();
}
}
或者
conn.commit();
调用commit会将所有更改写入数据库,这是必需的,因为自动提交已关闭。如果发生异常,则需要捕获它并发出命令
conn.rollback();
将数据库恢复到其原始状态
否则,请
conn.setAutoCommit(true);
然后,每个单独的语句将自动转换到数据库中。问题在于,每条语句都将在自己的事务中执行。如果在执行第一条语句后发生异常,则不会执行第二条语句,这可能会使数据库处于不一致的状态。也可以这样做
conn.commit();
调用commit会将所有更改写入数据库,这是必需的,因为自动提交已关闭。如果发生异常,则需要捕获它并发出命令
conn.rollback();
将数据库恢复到其原始状态
否则,请
conn.setAutoCommit(true);
然后,每个单独的语句将自动转换到数据库中。问题在于,每条语句都将在自己的事务中执行。如果在执行第一条语句后发生异常,则不会执行第二条语句,这可能会使数据库处于不一致的状态。尝试替换此语句:
con.setAutoCommit(false);
在这里:
System.out.println(i);
con.commit();
或者将其设置为true:
尝试替换此选项:
con.setAutoCommit(false);
在这里:
System.out.println(i);
con.commit();
或者将其设置为true:
当您设置时,insert或update未在数据库中得到反映:con.setAutoCommitfalse 将此行替换为: 犯罪 或
con.setAutoCommittrue 当您设置:con.setAutoCommitfalse时,插入或更新未在数据库中得到反映 将此行替换为: 犯罪 或
con.setAutoCommittrue 您在哪里提交事务?您在哪里提交事务?con.commit应该放在update语句之后,或者不设置自动提交默认值为true。con.commit应该放在update语句之后,或者不设置自动提交默认值为true。