package com.fill.jweb_jdbc_new;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class jdbcsw {
public final static String URL="jdbc:mysql://127.0.0.1:3308/test";
public final static String USERNAME="root";
public final static String PWD="root";
/**
*/
public static void sw(){
Connection conn=null;
//1.加载数据库驱动
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection(URL, USERNAME, PWD);
conn.setAutoCommit(false);//设为手动
//3.构造sql语句
String sql1="insert into admin_sql_inject(name,pwd)values(?,?)";
String sql3="insert into admin_sql_inject(name,pwd)values(?,?)";
String sql2="insert into admin_sql_inject(name,pwd)values(?,?)";
PreparedStatement ps=(PreparedStatement) conn.prepareStatement(sql1);
ps.setString(1,"a1");
ps.setString(2,"a1");
ps.executeUpdate();
ps=(PreparedStatement) conn.prepareStatement(sql2);
ps.setString(1,"a2");
ps.setString(2,"a2");
ps.executeUpdate();
ps=(PreparedStatement) conn.prepareStatement(sql3);
ps.setString(1,"a3");
ps.setString(2,"a3");
ps.executeUpdate();
conn.commit();
//6.关闭连接
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println(a.getName()+'-'+a.getPwd());
sw();
}
}
jdbc 事务处理
最新推荐文章于 2022-02-12 11:32:42 发布