批处理
当向数据库发送多条不同的SQL语句时,可以使用Statement实现批处理。Statement通过addBatch()方法添加一条SQL语句,通过executeBatch()方法批量执行SQL语句。
public static void main(String[] args)
{
Connection conn=null;
Statement stmt=null;
try {
conn=JDBCUtils.getConnection();
stmt=conn.createStatement();
//sql语句
String sql1="drop table if exists school";
String sql2="create table school(id int,name varchar(20))";
String sql3="insert into school values(2,'传智播客')";
String sql4="update school set id=1";
//Statement批处理SQL语句
stmt.addBatch(sql1);
stmt.addBatch(sql2);
stmt.addBatch(sql3);
stmt.addBatch(sql4);
stmt.executeBatch();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
JDBCUtils.release(null, stmt,conn);
}
}
PreparedStatement对象
Statement对象每次执行SQL语句时,都会对其进行编译。当相同的SQL语句执行多次时,Statement对象就会使数据库频繁编译相同的SQL语句,从而降低数据库的访问效率。。为了解决这个问题,PreparedStatement应运而生。也就是说,当相同的SQL语句再次执行时,数据库只需要使用缓冲区中的数据,而不需要对SQL语句再次编译,从而有效提高数据的访问效率。 这个接口可以传入参数化的SQL语句,防止SQL注入.
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement preStmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "root";
// 创建应用程序与数据库连接的Connection对象
conn = DriverManager.getConnection(url, username, password);
// 执行的SQL语句
String sql = "INSERT INTO users(name,password,email,birthday)" + "VALUES(?,?,?,?)";
// 创建执行SQL语句的PreparedStatement 对象
preStmt = conn.prepareStatement(sql);
preStmt.setString(1, "zl");
preStmt.setString(2, "123456");
preStmt.setString(3, "zl@sina.com");
preStmt.setString(4, "1989-12-23");
preStmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally { // 释放资源
if (preStmt != null) {
try {
preStmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
preStmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}
PreParedStatement批处理
当向同一个数据表中批量更新数据时,如果使用Statement,需要书写很多SQL语句,这时,为了避免重复代码的书写,可以使用PreparedStatement实现批处理。与Statement相比,PreparedStatement灵活许多,它既可以使用完整的SQL,也可以使用带参数的不完整SQL.但是,对于不完整的SQL,其具体的内容是用“?”占位符形式出现的,设置时要按照“?”顺序设置具体 的内容。
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement preStmt = null;
try {
// 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/jdbc";
String username = "root";
String password = "root";
// 创建应用程序与数据库连接的Connection对象
conn = DriverManager.getConnection(url, username, password);
// 执行的SQL语句
String sql = "INSERT INTO users(name,password,email,birthday)" + "VALUES(?,?,?,?)";
preStmt = conn.prepareStatement(sql);
for (int i = 0; i < 5; i++) {
preStmt.setString(1, "name" + i);
preStmt.setString(2, "password" + i);
preStmt.setString(3, "email" + i + "@itcast.cn");
preStmt.setDate(4, Date.valueOf("1989-02-19"));
preStmt.addBatch();
}
preStmt.executeBatch();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally { // 释放资源
if (preStmt != null) {
try {
preStmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
preStmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}