【发布时间】:2016-02-16 13:26:57
【问题描述】:
我在 Eclipse 中使用了以下代码。运行代码后数据库不会更新。它仍然显示相同的值。我什至在数据库中运行了运行良好的 sql 查询,但是一旦我运行我的 java 代码什么都没有更新..
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();
}
}
}
}
}
【问题讨论】:
-
你在哪里提交交易?
标签: java mysql jdbc transactions