JDBC第二篇

本文详细介绍了PreparedStatement在Java JDBC中的优势,包括使用占位符简化SQL编写,预编译提高效率以及防止SQL注入。同时,讨论了批处理技术,包括Statement和PreparedStatement两种方式实现批处理,以提升数据库操作的效率。最后,提到了如何获取数据库的自动主键列,以解决如学生表引用老师表主键的场景。
摘要由CSDN通过智能技术生成

1.PreparedStatement对象
  • PreparedStatement对象继承Statement对象,它比Statement对象更强大,使用起来更简单
  1. Statement对象编译SQL语句时,如果SQL语句有变量,就需要使用分隔符来隔开,如果变量非常多,就会使SQL变得非常复杂。PreparedStatement可以使用占位符,简化sql的编写
  2. Statement会频繁编译SQL。PreparedStatement可对SQL进行预编译,提高效率,预编译的SQL存储在PreparedStatement对象中,当sql执行出错时,会进行数据回滚。con.setAutoCommit(false)
  3. PreparedStatement防止SQL注入。,Statement通过分隔符’++’,编写永等式,可以不需要密码就进入数据库。
//模拟查询id为2的信息
String id = "2";

Connection connection = UtilsDemo.getConnection();

String sql = "SELECT * FROM users WHERE id = ?";
PreparedStatement preparedStatement = connection.preparedStatement(sql);

//第一个参数表示第几个占位符【也就是?号】,第二个参数表示值是多少
preparedStatement.setString(1,id);

ResultSet resultSet = preparedStatement.executeQuery();

if (resultSet.next()) {
    System.out.println(resultSet.getString("name"));
}

//释放资源
UtilsDemo.release(connection, preparedStatement, resultSet);
2.批处理
  • 当需要向数据库发送一批SQL语句执行时,应避免向数据库一条条发送执行,采用批处理以提升执行效率

批处理又两种方式

  1. Statement
  2. PreparedStatement
  • 通过executeBath()方法批量处理执行SQL语句,返回一个int[]数组,该数组代表各句SQL的返回值
  • 使用Statement方式实现批处理
/*
* Statement执行批处理
*
* 优点:
*       可以向数据库发送不同的SQL语句
* 缺点:
*       SQL没有预编译
*       仅参数不同的SQL,需要重复写多条SQL
* */
Connection connection = UtilsDemo.getConnection();

Statement statement = connection.createStatement();
String sql1 = "UPDATE users SET name='zhongfucheng' WHERE id='3'";
String sql2 = "INSERT INTO users (id, name, password, email, birthday)" +
        " VALUES('5','nihao','123','ss@qq.com','1995-12-1')";

//将sql添加到批处理
statement.addBatch(sql1);
statement.addBatch(sql2);

//执行批处理
statement.executeBatch();

//清空批处理的sql
statement.clearBatch();

UtilsDemo.release(connection, statement, null);
  • 使用PreparedStaement方式实现批处理
/*
* PreparedStatement批处理
*   优点:
*       SQL语句预编译了
*       对于同一种类型的SQL语句,不用编写很多条
*   缺点:
*       不能发送不同类型的SQL语句
*
* */
Connection connection = UtilsDemo.getConnection();

String sql = "INSERT INTO test(id,name) VALUES (?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);

for (int i = 1; i <= 205; i++) {
    preparedStatement.setInt(1, i);
    preparedStatement.setString(2, (i + "zhongfucheng"));

    //添加到批处理中
    preparedStatement.addBatch();

    if (i %2 ==100) {

        //执行批处理
        preparedStatement.executeBatch();

        //清空批处理【如果数据量太大,所有数据存入批处理,内存肯定溢出】
        preparedStatement.clearBatch();
    }

}
//不是所有的%2==100,剩下的再执行一次批处理
preparedStatement.executeBatch();

//再清空
preparedStatement.clearBatch();

UtilsDemo.release(connection, preparedStatement, null);
3.获取数据库的自动主键列
  • 应用场景:
    • 有一张老师表,一张学生表,现在来了一个新老师,学生跟着新老师学习。首先需要知道老师的id编号是多少。(学生外键参照老师主键)
public void test() {

    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    try {
        connection = JdbcUtils.getConnection();

        String sql = "INSERT INTO test(name) VALUES(?)";
        preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, "ouzicheng");

        if (preparedStatement.executeUpdate() > 0) {

            //获取到自动主键列的值
            resultSet = preparedStatement.getGeneratedKeys();

            if (resultSet.next()) {
                int id = resultSet.getInt(1);
                System.out.println(id);
            }
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        JdbcUtils.release(connection, preparedStatement, null);
    }
}    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值