好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java SQL注入案例教程及html基础入门

一,sql注入

–1,需求

–1,利用jdbc查询user的信息,如果信息正确就登录,否则提示错误

–1,创建user表,指定字段id name password,并添加数据

–2,通过jdbc查询user表的数据,根据用户名和密码查

–2,测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

package cn.tedu.test;

import java.sql.*;

import java.util.scanner;

//测试 用户的查询

/*

create table user(

     id int primary key auto_increment,

     name varchar(20),

     password varchar(20)

)

insert into user values(null,'jack','123');

insert into user values(null,'rose','123');

  */

public class test3 {

     public static void main(string[] args) throws exception {

//       method();  //模拟登录

//       method2(); //暴露问题

        method3(); //解决sql攻击问题

     }

     private static void method3() throws exception {

         //1,注册驱动

         class .forname( "com.mysql.jdbc.driver" );

         //2,获取连接

         string url = "jdbc:mysql:///cgb2105?characterencoding=utf8" ;//简写形式

         connection  conn = drivermanager.getconnection(url, "root" , "root" );

         //3,获取传输器

//        statement st = conn.createstatement();

//        string sql = "select * from user where name='"+a+"' and password='"+b+"'";

         string a = new scanner(system.in).nextline(); //用户名

         string b = new scanner(system.in).nextline(); //密码

         //sql骨架,?叫占位符

         string sql = "select * from user where name=? and password=?" ;

         //preparedstatement把sql骨架和参数分开发送给数据的

         //解决了sql攻击问题:jack'# 只是把#当做普通文本而不是注释符号

         preparedstatement ps = conn.preparestatement(sql);

         //给sql设置参数--指定要给哪个问号赋啥值

         ps.setstring( 1 ,a);

         ps.setstring( 2 ,b);

         //4,执行sql,根据用户名和密码查库

         resultset rs = ps.executequery();

         //5,解析结果集

         if ( rs.next() ){ //如果查到了数据next()返回true,就可以登录

             system.out.println( "登录成功~~" );

         } else {

             system.out.println( "登录失败!" );

         }

         //6,释放资源

         rs.close(); //释放结果集

         ps.close(); //释放传输器

         conn.close(); //释放连接

     }

     private static void method2() throws exception {

         //1,注册驱动

         class .forname( "com.mysql.jdbc.driver" );

         //2,获取连接

         string url = "jdbc:mysql:///cgb2105?characterencoding=utf8" ;//简写形式

         connection  conn = drivermanager.getconnection(url, "root" , "root" );

         //3,获取传输器

         statement st = conn.createstatement();

         //4,执行sql,根据用户名和密码查库

         string a = new scanner(system.in).nextline(); //用户名

         string b = new scanner(system.in).nextline(); //密码

//sql攻击/sql注入问题:本质是因为用户输入的特殊符号造成sql语义发生了改变。jack'#

         string sql = "select * from user where name='" +a+ "' and password='" +b+ "'" ;

         resultset rs = st.executequery(sql);

         //5,解析结果集

         if ( rs.next() ){ //如果查到了数据next()返回true,就可以登录

             system.out.println( "登录成功~~" );

         } else {

             system.out.println( "登录失败!" );

         }

         //6,释放资源

         rs.close(); //释放结果集

         st.close(); //释放传输器

         conn.close(); //释放连接

     }

     //模拟登录:根据用户名和密码查询user表

     private static void method() throws exception {

         //1,注册驱动

         class .forname( "com.mysql.jdbc.driver" );

         //2,获取连接

//string url ="jdbc:mysql://localhost:3306/cgb2105?characterencoding=utf8" ;

string url = "jdbc:mysql:///cgb2105?characterencoding=utf8" ;//简写形式

         connection  conn = drivermanager.getconnection(url, "root" , "root" );

         //3,获取传输器

         statement st = conn.createstatement();

         //4,执行sql,根据用户名和密码查库

         string sql = "select * from user where name='jack' and password='123'" ;

         resultset rs = st.executequery(sql);

         //5,解析结果集

         if ( rs.next() ){ //如果查到了数据next()返回true,就可以登录

             system.out.println( "登录成功~~" );

         } else {

             system.out.println( "登录失败!" );

         }

         //6,释放资源

         rs.close(); //释放结果集

         st.close(); //释放传输器

         conn.close(); //释放连接

     }

}

–3,总结

sql 攻击发生的现象是:用户输入了一些sql中的特殊字符,#表示注释

statement工具:无法避免sql注入问题,而且sql复杂需要自己拼接参数,低效

preparedstatement工具:避免了sql攻击的问题,sql简单,高效

–sql简单,先把sql骨架发给数据库,再把参数发给数据库。用?代替参数的位置叫占位符

二,练习preparedstatement

–1,需求

删除id=1的用户信息

–2,测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

package cn.tedu.test;

import java.sql.connection;

import java.sql.preparedstatement;

import java.sql.sqlexception;

public class test4 {

     public static void main(string[] args) {

         connection conn = null ;

         preparedstatement ps= null ;

         try {

             //调用工具类,,,获取连接

             conn = jdbcutils.getconnection();

             //3,获取传输器 ,利用高级的工具类执行sql

             //先执行sql骨架

             string sql = "delete from user where id=?" ;

             ps = conn.preparestatement(sql);

             //给第一个问号,设置值是1

             ps.setint( 1 , 1 );

             //4,执行sql

             int rows = ps.executeupdate();

             system.out.println( "删除成功" );

         } catch (exception e){

             system.out.println( "执行失败..." );

         } finally { //最终一定会被执行的

             //5,释放资源

            jdbcutils.close( null ,ps,conn);

         }

     }

}

–3,制作工具类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

package cn.tedu.test;

import java.sql.*;

//提取jdbc重复的代码,提高复用性

public class jdbcutils {

     /**

      * 释放资源

      * @param rs 结果集

      * @param ps 传输器

      * @param conn 数据库的连接

      */

     final static public void close(resultset rs, preparedstatement ps, connection conn){

         if (rs != null ){ //防止空指针异常

             try {

                 rs.close();

             } catch (sqlexception throwables) {

                 system.out.println( "执行失败..." ); //项目上线后的

                 //throwables.printstacktrace();//程序调试阶段

             }

         }

         if (ps != null ){ //防止空指针异常

             try {

                 ps.close();

             } catch (sqlexception throwables) {

                 throwables.printstacktrace();

             }

         }

         if (conn != null ) { //防止空指针异常

             try {

                 conn.close();

             } catch (sqlexception throwables) {

                 throwables.printstacktrace();

             }

         }

     }

     /**

      * 获取和数据库的连接

      * */

     static public connection getconnection() throws exception {

         //1,注册驱动

         class .forname( "com.mysql.jdbc.driver" );

         //2,获取连接

         string url= "jdbc:mysql:///cgb2105?characterencoding=utf8" ;

         connection conn = drivermanager.getconnection(url, "root" , "root" );

         return conn;

     }

}

三,html

–1,概述

是超文本标记语言,是指可以在网页中加入比文本更丰富的内容。标记有很多,要写开始标记和结果标记 <html></html>

–2,入门案例

?

1

2

3

4

5

6

7

8

html>

     <head>

         <title>hello html~</title>

     </head>

     <body>

         test......

     </body>

</html>

–3,使用工具

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<!doctype html> <!-- 声明这是一个html文件 -->

<html>  <!-- html根标签-->

     <head> <!-- 头部信息,设置网页的标题,编码。。。-->

         <meta charset= "utf-8" > <!-- 设置网页的编码 -->

         <title> html测试 </title> <!-- 设置网页的标题 -->

     </head>

     <body> <!-- 体信息,设置网页中要显示的内容 -->

         <!-- 这是html的注释,hbuilder复制粘贴一行ctrl c/ctrl v  ,剪切ctrl x,调整位置ctrl ↑↓ -->

         你好      html~

         你好html~ <br/>  <!-- br可以在网页中实现换行-->

         你&nbsp; &nbsp; &nbsp; &nbsp;好html~  <!-- &nbsp;可以在网页中实现空格-->

         你好html~

         你好html~

         你好html~

     </body>

</html>

–4,测试

四,测试常用标签

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

<!doctype html>

<html>

     <head>

         <meta charset= "utf-8" >

         <title>常用标签</title>

     </head>

     <body>

         <!-- 1 . 标题标签 h1 h2 h3...h6 -->

             <h1> 1 级标签 </h1>

             <h2> 2 级标签 </h2>

             <h3> 3 级标签 </h3>

             <h4> 4 级标签 </h4>

             <h5> 5 级标签 </h5>

             <h6> 6 级标签 </h6>

         <!-- 2 . 列表标签, ul+li无序列表   ol+li有序列表 -->

             <ol>

                 <li> 全国新冠疫苗接种剂次超 13 亿 </li>

                 <li> 刘伯明神七出舱曾遇险情 </li>

                 <li> 中国成功发射风云三号 05 星 </li>

                 <li> 江苏女生中考 757 分 8 门满分 </li>

             </ol>

         <!-- 3 . 图片标签,在网页中插入一个图片

                 src属性用来指定图片的路径

                 width属性用来指定图片的宽度,单位是像素px

                 height属性用来指定图片的高度,单位是像素px

         -->

             <img src= "a/2.jpg" width= "200px" height= "500px" />

             <img src= "2.jpg" width= "200px" height= "500px" />

             <img src= "2.jpg" width= "200px" height= "500px" />

         <!-- 4 . 超链接标签

             href属性用来指定点击时要跳转的路径

             target属性用来指定是否打开新窗口

         -->

             <a href= "http://www.baidu.com" target= "_blank" >点我</a>

         <!-- 锚定,返回顶部-->

             <a name= "top" >北京市富婆通讯录</a>

                 <h1> 18518518515 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

                 <h1> 123 </h1>

             <!-- href指定要回到的位置,通过#获取上面name的值 -->

             <a href= "#top" >点我,回到顶部</a>

         <!-- 5 . input标签   -->

         <br />

             <input type= "text" />  <!-- 普通文本类型-->

             <input type= "number" />  <!-- 数字类型-->

             <input type= "password" />  <!-- 密码,自动加密-->

             <input type= "date" />  <!-- 年月日-->

             <input type= "week" /> <!-- 周-->

             <input type= "radio" />男  <!-- 单选框-->

             <input type= "checkbox" />杨幂  <!-- 多选框-->

             <input type= "button" value= "点我" />  <!-- 按钮 -->

             <input type= "submit" />   <!-- 提交按钮 -->

            

             <br /><br /><br /><br /><br /><br />

     </body>

</html>

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注的更多内容!

原文链接:https://blog.csdn.net/u012932876/article/details/118485088

查看更多关于Java SQL注入案例教程及html基础入门的详细内容...

  阅读:8次