码迷,mamicode.com
首页 > 数据库 > 详细

PreparedStatement解决sql注入问题

时间:2016-07-08 11:44:21      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

总结 PreparedStatement解决sql注入问题
:sql中使用?做占位符
2.得到PreparedStatement对象
PreparedStatement pst=conn.prepareStatement(String sql);
pst.setString(1,"aaa");//设置 第一个?的占位符赋值
pst.setString(2,"bbb");
 
 
 
// 查找用户 使用PreparedStatement 解决了 sql注入问题
     public User findUser(User user) {
           String sql = "select * from user where username=‘?‘ and password=‘?‘";
           Connection conn = null;
           PreparedStatement pst = null;
           ResultSet rs = null;
            try {
                conn = jdbcUtils. getConnection();
                pst = conn.prepareStatement(sql);
                pst.setString(1, user.getUsername());
                pst.setString(2, user.getPassword());
                rs = pst.executeQuery();
                 if (rs.next()) {
                     User u = new User();
                     u.setId(rs.getInt( "id"));
                     u.setUsername(rs.getString( "username"));
                     u.setPassword(rs.getString( "password"));
                     u.setEmail(rs.getString( "email"));
                      return u;
                }
           } catch (Exception e) {
                 // TODO Auto-generated catch block
                e.printStackTrace();
           }
            return null;
     }

PreparedStatement解决sql注入问题

标签:

原文地址:http://www.cnblogs.com/haofaner/p/5652757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!