c3p0学习

1.ideal 启动web项目时要将数据库驱动包放入tomcat 的lib目录下

2.启动事务。。。

3.jdbc事务

 1 package main.com.cn.pojo;
 2 
 3 import java.sql.*;
 4 
 5 public class JdbcTest {
 6     public static final String URL = "jdbc:mysql://localhost:3306/mydb2";
 7     public static final String USER = "root";
 8     public static final String PASSWORD = "root";
 9     public static void main(String[] args) {
10         Connection conn = null;
11         try {
12             //1.加载驱动程序
13             Class.forName("com.mysql.jdbc.Driver");
14             //2. 获得数据库连接
15             conn = DriverManager.getConnection(URL, USER, PASSWORD);
16             conn.setAutoCommit(false);
17             //3.操作数据库,实现增删改查
18             String sql1 = "update account set money = money -? where name = ?";
19             String sql2 = "update account set money = money +? where name = ?";
20            PreparedStatement ps = conn.prepareStatement(sql1);
21            ps.setInt(1,100);
22            ps.setString(2,"reba");
23            ps.executeUpdate();
24             PreparedStatement ps2 = conn.prepareStatement(sql2);
25             ps2.setInt(1,100);
26             ps2.setString(2,"baby1");
27             ps2.executeUpdate();
28             int i = 1/0;
29             conn.commit();
30         } catch (Exception e) {
31             try {
32                 conn.rollback();
33             } catch (SQLException e1) {
34             }
35         }
36     }
37 }
View Code

 

posted on 2020-04-03 12:18  longlinji  阅读(142)  评论(0编辑  收藏  举报