JDBC增删改查,PreparedStatement和Statement的区别

更多精彩文章欢迎关注公众号“Java之康庄大道”

此篇是在上一篇的基础上使用PreparedStatement对象来实现JDBC增删改查的

具体工具类JDBCTools和实现类和配置文件在上一篇Statement对象实现的时候有写。

上一篇地址http://www.cnblogs.com/yunqing/p/6136896.html

1、增加

/**
     * 新建一个使用PreparedStatement的方法
     * PreparedStatement与Statement的区别
     * 1.不需要sql语句拼接,防止sql注入,更加安全
     * 2.用占位符的方式写sql,便于后期维护,提高代码可读性,可以自动对类型进行转换
     * 3.有预编译功能,可以大批量处理sql,(mysql不明显,Oracle很明显)
     * 
     * 向数据库中添加一条数据
     * 
     * PreparedStatement:用于执行sql语句的对象
     * 用connection的PreparedStatement(sql)方法获取
     * 用executeUpdate(sql)执行sql语句
     * 注意:只能执行 insert,update,delect,不能执行select
     * 
     */
    public void testPreparedStatement(){
        Connection conn=null;
        PreparedStatement preStatement=null;//创建PreparedStatement对象
        try {
            //1、准备Connection连接数据库
            conn=JDBCTools.getConnection2();
            //2、准备sql语句
            //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
            String sql="insert into t_student(name,age,email) values(?,?,?)";
            //3、准备prepareStatement
            //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
            preStatement=conn.prepareStatement(sql);
            //4、占位符设置值
            preStatement.setString(1, "klkl");
            preStatement.setInt(2, 12);
            preStatement.setString(3, "kkk@jjj");
            //5、执行sql
            preStatement.executeUpdate();
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            //6、关闭数据库等
            JDBCTools.guanbi(null, preStatement, conn);
        }
    }

2.修改

    public void testPreparedStatement(){
        Connection conn=null;
        PreparedStatement preStatement=null;//创建PreparedStatement对象
        try {
            conn=JDBCTools.getConnection2();
            
            //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
            String sql="update t_student set name=?,age=?,email=? where id=?";
            
            //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
            preStatement=conn.prepareStatement(sql);
            preStatement.setString(1, "asas");
            preStatement.setInt(2, 12);
            preStatement.setString(3, "asa@jjj");
            preStatement.setInt(4, 11);
            //执行sql
            preStatement.executeUpdate();
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            JDBCTools.guanbi(null, preStatement, conn);
        }
    }

3、删除

    public void testPreparedStatement(){
        Connection conn=null;
        PreparedStatement preStatement=null;//创建PreparedStatement对象
        try {
            conn=JDBCTools.getConnection2();
            
            //sql语句不再采用拼接方式,应用占位符问号的方式写sql语句。
            String sql="delete from t_student where id=?";
            
            //对占位符设置值,占位符顺序从1开始,第一个参数是占位符的位置,第二个参数是占位符的值。
            preStatement=conn.prepareStatement(sql);
            preStatement.setInt(1, 12);
            //执行sql
            preStatement.executeUpdate();
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            JDBCTools.guanbi(null, preStatement, conn);
        }
    }

4.查询

    public void testPreparedStatement(){
        Connection conn=null;
        PreparedStatement preStatement=null;//创建PreparedStatement对象
        ResultSet rs=null;
        try {
            //1.准备Connection连接数据库
            conn=JDBCTools.getConnection2();            
            //2.准备sql字符串
            String sql="select * from t_student";
            //3.准备prepareStatement
            preStatement=conn.prepareStatement(sql);
            //4.执行sql得到ResultSet
            rs=preStatement.executeQuery();
            //5.处理ResultSet显示查询到的结果
            while(rs.next()){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                int age=rs.getInt(3);
                String email=rs.getString(4);
                
                System.out.println(id);
                System.out.println(name);
                System.out.println(age);
                System.out.println(email);
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
                        //6.关闭
            JDBCTools.guanbi(rs, preStatement, conn);
        }
    }                    
原文地址:https://www.cnblogs.com/yunqing/p/6140229.html