JDBC(3):PreparedStatement对象介绍


一,PreparedStatement介绍

  • PreperedStatement是Statement的子类,它的实例对象可以通过Connection.preparedStatement()方法获得,相对于Statement对象而言:PreperedStatement可以避免SQL注入的问题
  • Statement会使数据库频繁编译SQL,可能造成数据库缓冲区溢出。PreparedStatement可对SQL进行预编译,从而提高数据库的执行效率。并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写

二,使用PreparedStatement对象完成对数据库的CRUD操作

注意:编写测试代码时要提前搭建好实验环境我的试验环境已经在我的博客【JDBC(二)---使用JDBC对数据库进行CRUD】中搭建完毕

测试代码

import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DemoTest1 {

    @Test  //插入
   public void insert(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //获取一个数据库连接
            connection= Demo.getConnection();
            //要执行的SQL命令,SQL中的参数使用?作为占位符
            String sql="insert into abc(id,name,password) values(?,?,?)";
            //通过conn对象获取负责执行SQL命令的prepareStatement对象
            preparedStatement = connection.prepareStatement(sql);
            //为SQL语句中的参数赋值
            preparedStatement.setInt(1,1);//插入id=1
            preparedStatement.setString(2,"钢铁侠");//插入name=钢铁侠
            preparedStatement.setString(3,"123");//插入password=123
            //执行插入操作,executeUpdate方法返回成功的条数
            int i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("插入成功!!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Demo.release(connection,preparedStatement,null);
        }
    }

    @Test //删除
    public void delete(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection = Demo.getConnection();
            //当id=?时 删除这一行
            String sql="delete from abc where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            int i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("删除成功!!!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Demo.release(connection,preparedStatement,null);
        }
    }

    @Test //更新
    public void update(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection= Demo.getConnection();
            //当id=?时 更新name和password的数据
            String sql="update abc set name=?,password=? where id=?";
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,"蜘蛛侠");
            preparedStatement.setString(2,"567");
            preparedStatement.setInt(3,1);
            int i = preparedStatement.executeUpdate();
            if(i>0){
                System.out.println("更新成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Demo.release(connection,preparedStatement,null);
        }
    }

    @Test //查找
    public void find(){
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
            connection= Demo.getConnection();
            //当id=?时 查询这个一列
            String sql="select * from abc where id=?";
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                //查询这一列的name 
                System.out.println(resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            Demo.release(connection,preparedStatement,resultSet);
        }

    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM