会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132365个问题
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 811楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 814楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 817楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 818楼

老师您好,可以麻烦帮我看一下吗,使用preparedStatement还是能全部查到数据库信息,这是什么原因导致的?谢谢!

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * SQL注入测试类
 */
public class SqlInjectTest {
    /**
     * 体现sql注入
     */
    public void sqlInTest(String username,int userage){
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try{
            //获取连接
            connection = JdbcUtils.getConnection();
            //创建Statement对象
            statement = connection.createStatement();
            //定义sql语句
            String sql = "select * from users where username = '"+username+"' and userage = "+userage;
            System.out.println(sql);

            //执行sql语句
            resultSet = statement.executeQuery(sql);
            //处理结果集
            while(resultSet.next()){
                int userid = resultSet.getInt("userid");
                String name = resultSet.getString("username");
                int age = resultSet.getInt("userage");
                System.out.println(userid+" "+name+" "+age);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.CloseResource(resultSet,statement,connection);
        }
    }


    public void noSqlInject(String username,int userage){
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try{
            //获取连接
            connection = JdbcUtils.getConnection();
            //创建PreparedStatement对象
            ps = connection.prepareStatement("select * from users where username = ? and userage = ?");
            //绑定参数
            ps.setString(1,username);
            ps.setInt(2,userage);

            //执行sql
            resultSet = ps.executeQuery();
            //处理结果集
            while (resultSet.next()){
                int userid = resultSet.getInt("userid");
                String name = resultSet.getString("username");
                int age = resultSet.getInt("userage");
                System.out.println(userid+" "+name+" "+age);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JdbcUtils.CloseResource(resultSet,ps,connection);
        }
    }


    public static void main(String[] args){
        SqlInjectTest sit = new SqlInjectTest();
       // sit.sqlInTest("oldlu' or 1=1 --",28);
       sit.noSqlInject("oldlu' or 1=1 --",28);
    }
}


JAVA 全系列/第三阶段:数据库编程/JDBC技术 820楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 821楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637