PreparedStatement和Statement比较

本文通过实例演示了使用Statement和PreparedStatement处理SQL语句的区别。在安全性方面,PreparedStatement能有效防止SQL注入;而在效率上,虽然PreparedStatement通常被认为更优,但实验结果显示在MySQL环境下,其性能并不优于Statement。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在选择使用Statement时,会产生SQL注入:


@Test
    public void testSave(){
        String name = "a' or 1=1 or 1='";
        String password ="123";
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        conn = DBUtil.getCon();
        //String sql = "select * from s_user where id=100";
        try {
            st = conn.createStatement();
            String sql = "select * from users where username='"+name+"'and password='"+password+"'";
            System.out.println(sql);
            rs = st.executeQuery(sql);
             while(rs.next()){
                 System.out.println(rs.getString(1));
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这回显示登录成功。

但是如果使用PreparedStatement:


@Test
    public void testSave(){
        String name = "a' or 1=1 or 1='";
        String password ="123";
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
         
        conn = DBUtil.getCon();
        String sql = "select * from users where username= ? and password=?";
         
        try {pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, password);
             
             rs = pstmt.executeQuery();
             if(rs.next()){
                 System.out.println(rs.getInt(1));
             }else{
                 System.out.println("fuck");
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

效率方面:

使用Statement:


@Test
    public void testEff(){
        Connection conn =null;
        Statement stmt = null;
        conn = DBUtil.getCon();
        long time = System.currentTimeMillis();
        try{
            stmt = conn.createStatement();
            for(int i=0;i<5000;i++){
                String sql = "insert into users (username,password) values ('1','123')";
                stmt.executeUpdate(sql);
            }          
            System.out.println(System.currentTimeMillis()-time);
        }catch(Exception e){
             
        }
    }
    
    
输出为:122482
使用PreparedStatement:
@Test
    public void testEff() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        long time = System.currentTimeMillis();
        conn = DBUtil.getCon();
        conn = DBUtil.getCon();
        String sql = "insert into users (username,password) values (?,?)";
        try {
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < 5000; i++) {
                pstmt.setString(1, "t");
                pstmt.setString(2, "1233");
                pstmt.executeUpdate();
            }
            System.out.println(System.currentTimeMillis()-time);
        } catch (Exception e) {
        }
    }
输出为:135199
效率来说,PreparedStatement和Statement在mysql里,PreparedStatement不比Statement好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值