package com.wkw.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/*JDBC快速入门*/
public class JDBCDemo2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册驱动
// Class.forName("com.mysql.jdbc.Driver");
//2.获取链接
String url="jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String username = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url,username,password);
//3.定义sql
String sql1 = "update account set money = 3000 where id = 1";
String sql2 = "update account set money = 3000 where id = 2";
//4.获取执行sql的对象
Statement stmt = conn.createStatement();
//开启事务
//5.执行sql
try {
//开启事务
conn.setAutoCommit(false);
int count1 = stmt.executeUpdate(sql1);
System.out.println(count1);
int i= 3/0;
//6.处理结果
int count2 = stmt.executeUpdate(sql2);
System.out.println(count2);
conn.commit();//提交事务
} catch (SQLException e) {
//回滚事务
e.printStackTrace();
}
//7,释放资源
stmt.close();
conn.close();
}
}