首页 新闻 搜索 专区 学院

数据库自动、手动提交

0
[待解决问题]

package com.javakc.jbdc.sql.transaction;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.javakc.books.sql.utils.Db;
public class App {
public static void main(String[] args) {
Connection conn = Db.getConnection();
final BigDecimal MONEY = BigDecimal.valueOf(50);
String sql1 = "update bank set money=money-? where id=?";
String sql2 = "update bank set money=money+? where id=?";
PreparedStatement pstmt = null;
try {
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql1);
pstmt.setObject(1, MONEY);
pstmt.setObject(2, "1");
System.out.println("row1:" + pstmt.executeUpdate());
Db.close(pstmt);
// System.out.println(1 / 0);
pstmt = conn.prepareStatement(sql2);
pstmt.setObject(1, MONEY);
pstmt.setObject(2, "2");
System.out.println("row2:" + pstmt.executeUpdate());
conn.commit();
Db.close(pstmt);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("开始回滚");
Db.rollback(conn);
} catch (Exception e) {
System.out.println("开始回滚");
Db.rollback(conn);
} finally {
Db.close(pstmt, conn);
}
}
}

conn.commit(); 这句代码不起作用啊

小幸运_lucky的主页 小幸运_lucky | 菜鸟二级 | 园豆:204
提问于:2018-09-07 20:06
< >
分享
所有回答(0)
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册