先介绍Statement的子类preperStatement
package jdbc;
import org.junit.Test;
import java.sql.*;
/**
* 现在考虑将数据库操作代码封装
* 注意:静态方法对应静态属性
*/
public class JDBCunit {
//设置静态属性
private final static String PathName = "com.mysql.cj.jdbc.Driver";
private final static String Url = "jdbc:mysql://localhost:3306/db10?useSSL=false&serverTimezone=UTC";//协议准备
private final static String User = "root";//用户名
private final static String Password = "123321";//密码
// 加载驱动器,连接数据库
public static Connection conn(){
Connection connection = null;
try {
Class.forName(PathName);
connection = DriverManager.getConnection(Url,User,Password);
} catch (ClassNotFoundException e) {
System.out.println("没有找到驱动器,请联系管理员解决!!!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("连接失败,请检查用户数据!!!!");
}
return connection;
}
// 关闭资源
public static void close(Connection con, Statement state){
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("connection关闭出现异常");
}
}
if (state != null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("statement关闭出现异常");
}
}
}
@Test
public void m1() throws SQLException {
// 加载驱动,获取连接
Connection connection = JDBCunit.conn();
// 拿到声明语句权限
Statement statement = connection.createStatement();
// 处理数据库
// Boolean bo = statement.execute("create database db10;");//新建一个数据库,
/**
* statement.execute()里面需要分号,和mysql执行语句一样
*/
// System.out.println(bo);
//关闭
JDBCunit.close(connection,statement);
}
// @Test
// public void test6() throws SQLException {
// //获取连接
// Connection conn = JDBCunit.conn();
// //获取执行器
// Statement statement = conn.createStatement();
// boolean execute = statement.execute("create table account\n" +
// " (\n" +
// " id int primary key auto_increment,\n" +
// " name varchar(40),\n" +
// " money float\n" +
// " ) character set utf8 collate utf8_general_ci;");
// System.out.println(execute?"创建成功":"创建失败");
// //关闭
// JDBCunit.close(conn,statement);
// }
String transmit="aaa";
String reserve="bbb";
int money =5;
/**
* 测试管理事物
* 转账
*/
/**
* 转账系统
* @param transmit 赚钱让人
* @param reserve 收钱人
* @param money 转账金额
* @throws SQLException
*/
public void m2(String transmit,String reserve,int money) throws SQLException {
// 加载驱动,创建连接
Connection connection = JDBCunit.conn();
// 设置手动事务
connection.setAutoCommit(false);
try {
//执行语句1
String sql = "update account set money = money-"+ money +"where name = ?";
// 创建执行对象1
PreparedStatement preparedStatement1 = connection.prepareStatement(sql);//防止SQL注入的安全我问题
preparedStatement1.setString(1,transmit);//表示补充完执行语句中第一个问好的值
// 执行更新语句1
int p1 = preparedStatement1.executeUpdate();
System.out.println("aaa执行的返回值是:" + p1);
/**
* 注意:java执行时是顺序执行,如果在转账时出现中断现象,aaa发射出去的钱bbb就会收不到,导致经济矛盾。
* 可用数据库的事物处理方法解决,让钱退回去
*/
// int a = 1 / 0;//模仿中间出现错误中断
//执行语句2
String sql2 = "update account set money = money+"+money+"where name = ?";
// 创建执行对象2
PreparedStatement preparedStatement2 = connection.prepareStatement(sql2);//防止SQL注入的安全我问题
preparedStatement2.setString(1,reserve);
// 执行更新语句2
int p2 = preparedStatement2.executeUpdate();
System.out.println("bbb执行的返回值是:" + p2);
// 提交事务
connection.commit();
//关闭
JDBCunit.close(connection, preparedStatement1);
JDBCunit.close(connection, preparedStatement2);
}catch (Exception e){
System.out.println("转账失败!!!");
// 事物回滚
connection.rollback();
}
}
public static void main(String[] args) throws SQLException {
new JDBCunit().m2("aaa","bbb",200);
}
}