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

老师您好,可以麻烦帮我看一下吗,使用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技术 815楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 816楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 822楼

请问老师 这是什么情况 应该如何解决啊

package com.wxc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Test01Jdbc {
    //向Departments表中添加一条数据
    public void insertDepartments(String department_id,int location_id){
        Connection conn=null;
        Statement state=null;
        try {
            //驱动注册   实例化数据库驱动对象
            Class.forName("com.mysql.jdbc.Driver");
            //创建连接
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/bjsxt?useUnicode=true&characterEncoding=utf-8&useSSL=false","root","Bugaosun1");
            //创建执行SQL的语句statement
            String sql="insert into departments values(default,'"+department_id+"','"+location_id+"')";
            state=conn.createStatement();
            int flag=state.executeUpdate(sql);
            System.out.println(flag);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (state!=null){
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void main(String[] args){
        Test01Jdbc test=new Test01Jdbc();
        test.insertDepartments("研发部",8);
    }
}

image.png

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 824楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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