java连接mysql演示SQL注入攻击

  1. 新建自定义工具类用于连接MySQL数据库:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    /**
     * Jdbc 工具类 连接器
     */
    public class JDBCUtil {
        private JDBCUtil(){}
        private static Connection connection;
        static {
            try{
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url="jdbc:mysql://localhost:3306/onlinedb?useSSL-false&serverTimezone=UTC";
                String user="root";
                String pwd="123456";
                connection= DriverManager.getConnection(url,user,pwd);
            }catch(ClassNotFoundException | SQLException c){
                c.printStackTrace();
                throw new RuntimeException("连接数据库失败");
            }
    
        }
        public static Connection getConnection(){
            return connection;
        }
        public static void close(){
            if(connection != null);{
                assert connection != null;
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
  2. 演示SQL注入攻击 模拟用户登录,现在MySQL 需要用到PreparedStatement类: 表示预编译的SQL语句的对象,SQL语句已预编译并存储在PreparedStatement对象中。 然后可以使用该对象多次有效地执行此语句。
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Scanner;
    
    /*
    SQL 注入攻击
     */
    public class Demo02 {
        public static void main(String[] args) {
            try{
                Statement statement=JDBCUtil.getConnection().createStatement();
    //            selectAll(statement);
                select();
    
            }catch(SQLException s){
                s.printStackTrace();
            }
        }
    
        /**
         * 使用 。。登录数据库  zeng 1 ' or '1=1
         * @param statement
         */
        public static void selectAll(Statement statement){
            Scanner scanner=new Scanner(System.in);
            String username=scanner.nextLine();
            String pwd=scanner.nextLine();
            String sql="select*from user where username='"+username+"' and pwd='"+pwd+"';";
            try {
                boolean b=statement.execute(sql);
                System.out.println(b);
                ResultSet resultSet=statement.executeQuery(sql);
                while (resultSet.next()){
                    System.out.println(resultSet.getString("username")+"	"+resultSet.getString("pwd"));
                }
                scanner.close();
                resultSet.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        /**
         * 创建一个PreparedStatement对象,用于将参数化的SQL语句发送到数据库。
         * SQL语句可以预编译并存储在PreparedStatement对象中
         * 防止注入攻击
         */
        public static void select(){
            Scanner scanner=new Scanner(System.in);
            String username=scanner.nextLine();
            String pwd=scanner.nextLine();
            String sql="select*from user where username= ? and pwd= ?";
            try {
                PreparedStatement preparedStatement=JDBCUtil.getConnection().prepareStatement(sql);
                //使用给定对象设置指定参数的值。
                preparedStatement.setObject(1,username);
                preparedStatement.setObject(2,pwd);
                //执行此 PreparedStatement对象中的SQL查询,并返回查询 PreparedStatement的 ResultSet对象。
                ResultSet resultSet=preparedStatement.executeQuery();
                while (resultSet.next()){
                    System.out.println(resultSet.getString("username")+"	"+resultSet.getString("pwd"));
                }
                scanner.close();
                resultSet.close();
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
    }

    基本就这样子。

原文地址:https://www.cnblogs.com/Zeng02/p/12825967.html