1、sql注入讲解:
statement存在sql注入问题
数据库数据:
模仿用户登录过程代码如下:
1 package xia.qingtang; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.Statement; 6 import java.util.Scanner; 7 8 public class UserLogin { 9 10 public static void main(String[] args) { 11 Scanner input =new Scanner(System.in); 12 System.out.println("请输入用户名:"); 13 String userName=input.nextLine(); 14 System.out.println("请输入密码:"); 15 String password=input.nextLine(); 16 Connection conn = null; 17 Statement st = null; 18 ResultSet rs = null; 19 Users u=null; 20 try { 21 conn=Dbutils.getConnection(); 22 st=conn.createStatement(); 23 String sql="select * from users t where t.`NAME`='"+userName+"' and t.password='"+password+"'"; 24 System.out.println(sql); 25 rs=st.executeQuery(sql); 26 while(rs.next()){ 27 u=new Users(); 28 u.setAge(rs.getInt("age")); 29 u.setId(rs.getInt("id")); 30 u.setName(rs.getString("name")); 31 u.setPassword(rs.getString("password")); 32 } 33 if(u!=null){ 34 System.out.println("此用户可以登录!"); 35 }else{ 36 System.out.println("无此用户,不能登录"); 37 } 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } finally { 41 Dbutils.closeAll(rs, st, conn); 42 } 43 44 } 45 46 }
工具类dbutils.java
package xia.qingtang; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import java.util.ResourceBundle; public class Dbutils { private static String driverName; private static String url; private static String user; private static String password; static{ /** * 加载properties文件方法一 */ ResourceBundle rb=ResourceBundle.getBundle("dbInfo"); driverName=rb.getString("driverName"); url=rb.getString("url"); user=rb.getString("user"); password=rb.getString("password"); /** * 加载properties文件方法二 */ // Properties p=new Properties(); // try { // p.load(new FileInputStream("src/dbInfo.properties")); // driverName=p.getProperty("driverName"); // url=p.getProperty("url"); // user=p.getProperty("user"); // password=p.getProperty("password"); // } catch (FileNotFoundException e1) { // e1.printStackTrace(); // } catch (IOException e1) { // e1.printStackTrace(); // } try { Class.forName(driverName); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConnection() throws Exception{ Connection conn=DriverManager.getConnection(url, user, password); return conn; } public static void closeAll(ResultSet rs,Statement st,Connection conn){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } rs=null; if(st!=null){ try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } st=null; if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } conn=null; } }
配置文件dbInfo.properties:
driverName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis ##jdbc:mysql:///mybatis user=root password=123456
目录结果:
执行UserLogin.java文件
结果执行后:正常情况下数据库无此用户,无法登陆,结果出现了可以登录的情况,这就是sql注入。
SQL注入问题:preparedStatement
preparedStatement:预编译对象, 是Statement对象的子类。
特点:
性能要高
会把sql语句先编译
sql语句中的参数会发生变化,过滤掉用户输入的关键字。
结果显示: