解决SQL注入问题

PreparedStatement和Statement的区别:

/**
 * 1、对比一下Statement和PreparedStatement?
     * statement存在sq1注入问题,PreparedStatement解诀了sql注入问题。
     * Statement是编译一次执行一 次。PreparedStatement是编译一次, 可执行N次。                    
     * PreparedStatement会在编译阶段做类型的安全检查。
 * 综上所述: PreparedStatement使用较多。只有极少数的情况下需要使用Statement
 * 2、什么情况下必须使用Statement呢?
     * 业务方面要求必须支持sQL注入的时候。
     * Statement支持sQL注入,凡是业务方面要求是需要进行sq1语句拼接的,必须使用Statement 
 */

代码:

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test08_解决SQL注入问题 {
    public static void main(String[] args) {
        //初始化一个界面的方法
        Map<String,String> userLogin = chuShiHuaUi();
        //验证用户名和密码
        Boolean dengLu = login(userLogin);
        //最后输出结果
        System.out.println(dengLu ? "登录成功!" : "登录失败!");
    }

    private static Boolean login(Map<String, String> userLogin) {
        //打标记
        Boolean b = false;

        Connection conn = null;
        //PreparedStatement 是Statement的一个接口
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            //1、注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/donglijiedian","root","zhixi158");
            //3、获取 预编译 的数据库操作对象
            // sQL语句的框子。其中一个?,表示一个占位符,一个?将来接收一个“值”, 注意:占位符不能使用单引号括起来。
            String sql = "select * from t_user where userName = ? and userPwd = ?";
            //程序执行到此处,会发送sq1语句框子给DBMS,然后DBMS进行sql语句的预先编译。
            ps = conn.prepareStatement(sql);
            //给占位符?传值(第1个问号下标是1,第2个问号下标是2,JDBC中 所有下标从1开始。)
            ps.setString(1,userLogin.get("userName"));
            ps.setString(2,userLogin.get("userPwd"));

            //4、执行SQL
            rs = ps.executeQuery();

            //5、处理查询结果集
            //用户名错的情况下查不到记录,用户名正确情况下也最多查出一条记录,所以用if就够了
            if(rs.next()){
                return true;//如果存在这条记录就返回
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            //6、释放资源,由内到外
            try{
                if(rs != null)
                    rs.close();
            }catch(Exception e){
                e.printStackTrace();
            }

            try{
                if(ps != null)
                    ps.close();
            }catch(Exception e){
                e.printStackTrace();
            }

            try{
                if(conn != null)
                    conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return b;
    }


    private static Map<String, String> chuShiHuaUi() {
        Scanner s = new Scanner(System.in);
        //获取用户名
        System.out.println("用户名:");
        String userName  = s.nextLine();//nextLine返回一行数据

        //获取密码
        System.out.println("密码:");
        String userPwd = s.nextLine();

        //组装Map集合
        Map<String,String> userLogin  = new HashMap<>();
        userLogin.put("userName",userName);//获取用户名
        userLogin.put("userPwd",userPwd);//获取密码
        return userLogin;//返回集合数据
    }
}
原文地址:https://www.cnblogs.com/zhangzhixi/p/14109810.html