SQL语句预编译后怎么注入

请问大家sql语句预编译后怎么注入,我目前知道预编译是防止sql注入最有效的方法,但不知道怎么sql预编译后的注入,谢谢大家

你说的应该是PreparedStatement,PreparedStatement本身是防注入的

Connection conn = null;
        try {
            conn = ConnectionHandler.getConn();
            String sql = "INSERT INTO item(id,title,price,sales) VALUES(?,?,?,?)";

            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,item.getId());
            pstmt.setString(2,item.getTitle());
            pstmt.setDouble(3,item.getPrice());
            pstmt.setInt(4,item.getSales());

            pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            throw new SQLException("新增商品到商品表失败");
        }
  • 像这样,用PreparedStatement向插入一条数据,使用占位符“?”来占位
  • 之后使用set方法,set方法里面的第一个参数代表第几个占位符(从1开始),第二个参数代表你用来替换占位符的内容
  • 选择setString还是setInt还是其他,是根据占位符需要的数据类型判断的,比如上面这个代码,第一个占位符需要的值是字符串,所以用setString
  • executeUpdate方法是执行PreparedStatement对象中的SQL查询,一般增删改都用这个方法

其实这些网上都可以查得到的,听别人说总是会有可能遗漏的