25.【批处理及PreparedStatement对象】

批处理

当向数据库发送多条不同的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;
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聊城云在天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值