jdbc java数据库链接 8)防止sql注入

回顾下以前jdbc的开发步骤:sql

  1:建项目,引入数据库驱动包数据库

  2:加载驱动缓存

    Class.forName(..);spa

  3:获取链接对象code

  4:建立执行sql语句的stmt对象;  sql对象

  5:执行sqlblog

    a) 更新    delete/insert/update接口

      !:executeUpdate();       开发

    b) 查询    select  get

      !:executeQuery();

  6:关闭/异常

以前有说过,Statement接口和PreparedStatement接口的区别,其中的一个就是:

  PreparedStatement接口可以防止sql注入

那么,什么是sql注入呢?

  其实sql注入就是用户输入的恶意密码,可以绕过你的用户名和密码登录。

 

例子:

 

  1:首先建立个数据库

1 -- 建立数据库
2 CREATE DATABASE jdbc_demo DEFAULT CHARACTER SET utf8;i
3 -- 建立表
4 USE jdbc_demo;
5 CREATE TABLE admin(
6     id INT PRIMARY KEY AUTO_INCREMENT,
7     userName VARCHAR(20),
8     pwd VARCHAR(20)
9 )

  

  2:使用Statement接口,没有防止sql注入:

 1 /**
 2  * 模拟用户登录数据库,演示注入
 3  * 
 4  * @author LLZ
 5  */
 6 public class Two_StatementDemo {
 7 
 8     /**
 9      * 1:sql注入 Statement
10      */
11 
12     private Connection conn;
13     private Statement stsm;
14     private ResultSet rs;
15     private PreparedStatement pstsm;
16 
17     @Test
18     public void Test1() {
19 
20         String user = "tom";
21         // String password = "123";
22         String password = " ' or 1=1 -- /**/ "; // sql注入,这个也能够登录成功
23 
24         try {
25             // 1.1:加载驱动
26             conn = Jdbcutil.getConnection();
27 
28             // 1.3:建立Statement对象
29             stsm = conn.createStatement();
30 
31             // 1.4:准备sql语句
32             String sql = "select * from jdbc where user='" + user
33                     + "'  and password='" + password + "' ";
34 
35             // 1.5:执行sql
36             rs = stsm.executeQuery(sql);
37 
38             // 1.6:打印返回的结果
39             if (rs.next()) {
40                 System.out.println(rs.getInt("id"));
41             }
42         } catch (Exception e) {
43             e.printStackTrace();
44 
45         } finally {
46             // 1.7:关闭链接
47             try {
48                 rs.close();
49                 stsm.close();
50                 conn.close();
51             } catch (Exception e) {
52 
53                 e.printStackTrace();
54             }
55         }
56     }

 

  3:使用PreparedStatement接口,防止sql注入:

其缘由就是因为该接口具备缓存区,须要先执行预编译远,等传入参数才正式执行sql语言

 1 /**
 2      * 二:用PreparedStatement防止sql注入
 3      */
 4     @Test
 5     public void Test2() {
 6 
 7         String user = "tom";
 8         String password = "123";
 9         // String password = " ' or 1=1 -- /**/ "; // sql注入,这个在这里就没法登录
10         // 准备sql预编译语句
11         String sql = "select * from jdbc where user=? and password=?";
12 
13         try {
14             // 2.1:建立链接
15             conn = Jdbcutil.getConnection();
16 
17             // 2.2:建立PerparedStatement对象(执行预编译)
18             pstsm = conn.prepareStatement(sql);
19 
20             // 2.3:准备参数
21             pstsm.setString(1, user);
22             pstsm.setString(2, password);
23 
24             // 2.4:发送参数,执行sql
25             ResultSet rs = pstsm.executeQuery();
26             if (rs.next()) {
27                 System.out.println(rs.getInt("id"));
28             }
29         } catch (Exception e) {
30             e.printStackTrace();
31         } finally {
32             // 2.5:关闭链接
33             Jdbcutil.close(conn, pstsm, rs);
34         }
35 
36     }
相关文章
相关标签/搜索