2.SQL注入
1=1永远成立
Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM stu where sno ='1' or ' and sname = 'or '1'='1'"); //如果有数据,rs.next()返回true while(rs.next()){ System.out.println(rs.getString(1 )+" "+rs.getString(2)); }
Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //sql语句中的?相当于占位符 pre是PreparedStatement接口的实现类的对象 String sql="select * from stu where sno =? and sname = ?"; PreparedStatement pre=conn.prepareStatement(sql); //对问号依次赋值 pre.setString(1,sno);//第一个问号,第一个问号的值 pre.setString(2,name); //若执行dml(增删改)语句则执行 excuteUpdate() ResultSet resultSet = pre.executeQuery(); while (resultSet.next()){ System.out.println(resultSet.getString(2)); } conn.close(); pre.close(); resultSet.close();```