【JDBC】笔记(4)-
楔子:
JDBC 的事务默认是自动提交的:
只要执行一条 DML语句,则自动提交一次。但是在实际的业务中,通常是多条 DML语句 联合完成的,那么就必须保证这些 DML语句 在同一个事务中同时成功或失败!!!
否则这会是一个非常严重的bug!!!
import java.sql.*;
public class JDBCTransactionTest01 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement ps = null;
int count = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode"
, "root", "888");
String sql = "update t_act set balance = ? where actno = ?";
ps = connection.prepareStatement(sql);
ps.setDouble(1, 0);
ps.setDouble(2, 5566);
count = ps.executeUpdate();
String s = null;
s.toString();
String sql0 = "update t_act set balance = ? where actno = ?";
ps = connection.prepareStatement(sql0);
ps.setDouble(1, 1000);
ps.setDouble(2, 1314);
count += ps.executeUpdate();
System.out.println("更新数据:" + count + "条");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(count == 2?"转账成功":"转账失败");
}
}
}
IDEA控制台输出结果:
java.lang.NullPointerException
at com.bjpowernode.jdbc.JDBCTransactionTest01.main(JDBCTransactionTest01.java:48)
转账失败
Process finished with exit code 0
现在瞅一眼 t_act:
+-------+---------+
| actno | balance |
+-------+---------+
| 5566 | 0.00 |
| 1314 | 0.00 |
+-------+---------+
哎呀我去,这bug太严重了,钱都飞了!!
import java.sql.*;
public class JDBCTransactionTest02 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement ps = null;
int count = 0;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode"
, "root", "888");
//将自动提交机制修改为手动提交
connection.setAutoCommit(false);
String sql = "update t_act set balance = ? where actno = ?";
ps = connection.prepareStatement(sql);
ps.setDouble(1, 0);
ps.setDouble(2, 5566);
count = ps.executeUpdate();
String s = null;
s.toString();
String sql0 = "update t_act set balance = ? where actno = ?";
ps = connection.prepareStatement(sql0);
ps.setDouble(1, 1000);
ps.setDouble(2, 1314);
count += ps.executeUpdate();
System.out.println("更新数据:" + count + "条");
//程序执行到这说明没有异常,事务结束,手动提交数据
connection.commit();
} catch (Exception e) {
//如果出现异常,回滚事务
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(count == 2?"转账成功":"转账失败");
}
}
}
IDEA控制台输出结果:
java.lang.NullPointerException
at com.bjpowernode.jdbc.JDBCTransactionTest02.main(JDBCTransactionTest02.java:31)
转账失败
Process finished with exit code 0
现在瞅一眼 t_act:
+-------+---------+
| actno | balance |
+-------+---------+
| 5566 | 1000.00 |
| 1314 | 0.00 |
+-------+---------+
虽然因为某些原因导致转账失败,但是数据库中储存的数据一切正常,而且控制台也给出了提示(转账失败),所以总结:此转账程序非常成功!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341