Java SQL注入案例教程及html基礎(chǔ)入門
?1,利用jdbc查詢user的信息,如果信息正確就登錄,否則提示錯(cuò)誤
?1,創(chuàng)建user表,指定字段id name password,并添加數(shù)據(jù)
?2,通過jdbc查詢user表的數(shù)據(jù),根據(jù)用戶名和密碼查
?2,測(cè)試package cn.tedu.test;import java.sql.*;import java.util.Scanner;//測(cè)試 用戶的查詢/*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,注冊(cè)驅(qū)動(dòng)Class.forName('com.mysql.jdbc.Driver');//2,獲取連接String url ='jdbc:mysql:///cgb2105?characterEncoding=utf8' ;//簡(jiǎn)寫形式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骨架和參數(shù)分開發(fā)送給數(shù)據(jù)的//解決了SQL攻擊問題:jack’# 只是把#當(dāng)做普通文本而不是注釋符號(hào)PreparedStatement ps = conn.prepareStatement(sql);//給SQL設(shè)置參數(shù)--指定要給哪個(gè)問號(hào)賦啥值ps.setString(1,a);ps.setString(2,b);//4,執(zhí)行SQL,根據(jù)用戶名和密碼查庫ResultSet rs = ps.executeQuery();//5,解析結(jié)果集if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄 System.out.println('登錄成功~~');}else{ System.out.println('登錄失敗!');}//6,釋放資源rs.close();//釋放結(jié)果集ps.close();//釋放傳輸器conn.close();//釋放連接 } private static void method2() throws Exception {//1,注冊(cè)驅(qū)動(dòng)Class.forName('com.mysql.jdbc.Driver');//2,獲取連接String url ='jdbc:mysql:///cgb2105?characterEncoding=utf8' ;//簡(jiǎn)寫形式Connection conn = DriverManager.getConnection(url, 'root', 'root');//3,獲取傳輸器Statement st = conn.createStatement();//4,執(zhí)行SQL,根據(jù)用戶名和密碼查庫String a = new Scanner(System.in).nextLine();//用戶名String b = new Scanner(System.in).nextLine();//密碼//SQl攻擊/SQL注入問題:本質(zhì)是因?yàn)橛脩糨斎氲奶厥夥?hào)造成SQL語義發(fā)生了改變。jack’#String sql = 'select * from user where name=’'+a+'’ and password=’'+b+'’';ResultSet rs = st.executeQuery(sql);//5,解析結(jié)果集if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄 System.out.println('登錄成功~~');}else{ System.out.println('登錄失??!');}//6,釋放資源rs.close();//釋放結(jié)果集st.close();//釋放傳輸器conn.close();//釋放連接 } //模擬登錄:根據(jù)用戶名和密碼查詢user表 private static void method() throws Exception {//1,注冊(cè)驅(qū)動(dòng)Class.forName('com.mysql.jdbc.Driver');//2,獲取連接//String url ='jdbc:mysql://localhost:3306/cgb2105?characterEncoding=utf8' ;String url ='jdbc:mysql:///cgb2105?characterEncoding=utf8' ;//簡(jiǎn)寫形式Connection conn = DriverManager.getConnection(url, 'root', 'root');//3,獲取傳輸器Statement st = conn.createStatement();//4,執(zhí)行SQL,根據(jù)用戶名和密碼查庫String sql = 'select * from user where name=’jack’ and password=’123’';ResultSet rs = st.executeQuery(sql);//5,解析結(jié)果集if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄 System.out.println('登錄成功~~');}else{ System.out.println('登錄失敗!');}//6,釋放資源rs.close();//釋放結(jié)果集st.close();//釋放傳輸器conn.close();//釋放連接 }}?3,總結(jié)
SQL 攻擊發(fā)生的現(xiàn)象是:用戶輸入了一些SQL中的特殊字符,#表示注釋
Statement工具:無法避免SQL注入問題,而且SQL復(fù)雜需要自己拼接參數(shù),低效
PreparedStatement工具:避免了SQL攻擊的問題,SQL簡(jiǎn)單,高效
?SQL簡(jiǎn)單,先把SQL骨架發(fā)給數(shù)據(jù)庫,再把參數(shù)發(fā)給數(shù)據(jù)庫。用?代替參數(shù)的位置叫占位符
二,練習(xí)PreparedStatement?1,需求刪除id=1的用戶信息
?2,測(cè)試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{ //調(diào)用工具類,,,獲取連接 conn = JDBCUtils.getConnection(); //3,獲取傳輸器 ,利用高級(jí)的工具類執(zhí)行SQL //先執(zhí)行SQL骨架 String sql = 'delete from user where id=?'; ps = conn.prepareStatement(sql); //給第一個(gè)問號(hào),設(shè)置值是1 ps.setInt(1,1); //4,執(zhí)行SQL int rows = ps.executeUpdate(); System.out.println('刪除成功');}catch (Exception e){ System.out.println('執(zhí)行失敗...');}finally{ //最終一定會(huì)被執(zhí)行的 //5,釋放資源 JDBCUtils.close(null,ps,conn);} }}?3,制作工具類
package cn.tedu.test;import java.sql.*;//提取jdbc重復(fù)的代碼,提高復(fù)用性public class JDBCUtils { /** * 釋放資源 * @param rs 結(jié)果集 * @param ps 傳輸器 * @param conn 數(shù)據(jù)庫的連接 */ final static public void close(ResultSet rs, PreparedStatement ps, Connection conn){if(rs != null){//防止空指針異常 try {rs.close(); } catch (SQLException throwables) {System.out.println('執(zhí)行失敗...');//項(xiàng)目上線后的//throwables.printStackTrace();//程序調(diào)試階段 }}if(ps != null){//防止空指針異常 try {ps.close(); } catch (SQLException throwables) {throwables.printStackTrace(); }}if(conn != null) {//防止空指針異常 try {conn.close(); } catch (SQLException throwables) {throwables.printStackTrace(); }} } /** * 獲取和數(shù)據(jù)庫的連接 * */ static public Connection getConnection() throws Exception {//1,注冊(cè)驅(qū)動(dòng)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,概述
是超文本標(biāo)記語言,是指可以在網(wǎng)頁中加入比文本更豐富的內(nèi)容。標(biāo)記有很多,要寫開始標(biāo)記和結(jié)果標(biāo)記 <html></html>
?2,入門案例html><head><title>hello html~</title></head><body>test......</body></html>?3,使用工具
<!DOCTYPE html> <!-- 聲明這是一個(gè)HTML文件 --><html> <!-- HTML根標(biāo)簽--><head> <!-- 頭部信息,設(shè)置網(wǎng)頁的標(biāo)題,編碼。。。--><meta charset='utf-8'> <!-- 設(shè)置網(wǎng)頁的編碼 --><title> html測(cè)試 </title> <!-- 設(shè)置網(wǎng)頁的標(biāo)題 --></head><body> <!-- 體信息,設(shè)置網(wǎng)頁中要顯示的內(nèi)容 --><!-- 這是HTML的注釋,Hbuilder復(fù)制粘貼一行ctrl c/ctrl v ,剪切ctrl x,調(diào)整位置ctrl ↑↓ -->你好 html~你好html~ <br/> <!-- br可以在網(wǎng)頁中實(shí)現(xiàn)換行-->你 好html~ <!-- 可以在網(wǎng)頁中實(shí)現(xiàn)空格-->你好html~你好html~你好html~</body></html>?4,測(cè)試
<!DOCTYPE html><html><head><meta charset='utf-8'><title>常用標(biāo)簽</title></head><body><!-- 1. 標(biāo)題標(biāo)簽 h1 h2 h3...h6 --><h1> 1級(jí)標(biāo)簽 </h1><h2> 2級(jí)標(biāo)簽 </h2><h3> 3級(jí)標(biāo)簽 </h3><h4> 4級(jí)標(biāo)簽 </h4><h5> 5級(jí)標(biāo)簽 </h5><h6> 6級(jí)標(biāo)簽 </h6><!-- 2. 列表標(biāo)簽, ul+li無序列表 ol+li有序列表 --><ol><li> 全國(guó)新冠疫苗接種劑次超13億 </li><li> 劉伯明神七出艙曾遇險(xiǎn)情 </li><li> 中國(guó)成功發(fā)射風(fēng)云三號(hào)05星 </li><li> 江蘇女生中考757分8門滿分 </li></ol><!-- 3. 圖片標(biāo)簽,在網(wǎng)頁中插入一個(gè)圖片 src屬性用來指定圖片的路徑width屬性用來指定圖片的寬度,單位是像素pxheight屬性用來指定圖片的高度,單位是像素px--><img src='http://www.intensediesel.com/bcjs/a/2.jpg' /><img src='http://www.intensediesel.com/bcjs/2.jpg' /><img src='http://www.intensediesel.com/bcjs/2.jpg' /><!-- 4. 超鏈接標(biāo)簽 href屬性用來指定點(diǎn)擊時(shí)要跳轉(zhuǎn)的路徑target屬性用來指定是否打開新窗口--><a target='_blank'>點(diǎn)我</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='http://www.intensediesel.com/bcjs/4361.html#top'>點(diǎn)我,回到頂部</a><!-- 5. input標(biāo)簽 --><br /><input type='text' /> <!-- 普通文本類型--><input type='number' /> <!-- 數(shù)字類型--><input type='password' /> <!-- 密碼,自動(dòng)加密--><input type='date' /> <!-- 年月日--><input type='week' /> <!-- 周--><input type='radio' />男 <!-- 單選框--><input type='checkbox' />楊冪 <!-- 多選框--><input type='button' value='點(diǎn)我'/> <!-- 按鈕 --><input type='submit' /> <!-- 提交按鈕 --><br /><br /><br /><br /><br /><br /></body></html>總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說明2. PHP設(shè)計(jì)模式中工廠模式深入詳解3. CSS hack用法案例詳解4. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過程(親測(cè)可用)5. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)6. asp中response.write("中文")或者js中文亂碼問題7. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析8. PHP session反序列化漏洞超詳細(xì)講解9. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向10. ASP+ajax實(shí)現(xiàn)頂一下、踩一下同支持與反對(duì)的實(shí)現(xiàn)代碼
