Java SQL注入案例教程及html基礎入門
一,SQL注入
–1,需求
–1,利用jdbc查詢user的信息,如果信息正確就登錄,否則提示錯誤
–1,創(chuàng)建user表,指定字段id name password,并添加數(shù)據(jù)
–2,通過jdbc查詢user表的數(shù)據(jù),根據(jù)用戶名和密碼查
–2,測試
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,注冊驅(qū)動
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骨架和參數(shù)分開發(fā)送給數(shù)據(jù)的
//解決了SQL攻擊問題:jack'# 只是把#當做普通文本而不是注釋符號
PreparedStatement ps = conn.prepareStatement(sql);
//給SQL設置參數(shù)--指定要給哪個問號賦啥值
ps.setString(1,a);
ps.setString(2,b);
//4,執(zhí)行SQL,根據(jù)用戶名和密碼查庫
ResultSet rs = ps.executeQuery();
//5,解析結果集
if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄
System.out.println("登錄成功~~");
}else{
System.out.println("登錄失??!");
}
//6,釋放資源
rs.close();//釋放結果集
ps.close();//釋放傳輸器
conn.close();//釋放連接
}
private static void method2() throws Exception {
//1,注冊驅(qū)動
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,執(zhí)行SQL,根據(jù)用戶名和密碼查庫
String a = new Scanner(System.in).nextLine();//用戶名
String b = new Scanner(System.in).nextLine();//密碼
//SQl攻擊/SQL注入問題:本質(zhì)是因為用戶輸入的特殊符號造成SQL語義發(fā)生了改變。jack'#
String sql = "select * from user where name='"+a+"' and password='"+b+"'";
ResultSet rs = st.executeQuery(sql);
//5,解析結果集
if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄
System.out.println("登錄成功~~");
}else{
System.out.println("登錄失敗!");
}
//6,釋放資源
rs.close();//釋放結果集
st.close();//釋放傳輸器
conn.close();//釋放連接
}
//模擬登錄:根據(jù)用戶名和密碼查詢user表
private static void method() throws Exception {
//1,注冊驅(qū)動
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,執(zhí)行SQL,根據(jù)用戶名和密碼查庫
String sql = "select * from user where name='jack' and password='123'";
ResultSet rs = st.executeQuery(sql);
//5,解析結果集
if( rs.next() ){ //如果查到了數(shù)據(jù)next()返回true,就可以登錄
System.out.println("登錄成功~~");
}else{
System.out.println("登錄失敗!");
}
//6,釋放資源
rs.close();//釋放結果集
st.close();//釋放傳輸器
conn.close();//釋放連接
}
}
–3,總結
SQL 攻擊發(fā)生的現(xiàn)象是:用戶輸入了一些SQL中的特殊字符,#表示注釋
Statement工具:無法避免SQL注入問題,而且SQL復雜需要自己拼接參數(shù),低效
PreparedStatement工具:避免了SQL攻擊的問題,SQL簡單,高效
–SQL簡單,先把SQL骨架發(fā)給數(shù)據(jù)庫,再把參數(shù)發(fā)給數(shù)據(jù)庫。用?代替參數(shù)的位置叫占位符
二,練習PreparedStatement
–1,需求
刪除id=1的用戶信息
–2,測試
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,獲取傳輸器 ,利用高級的工具類執(zhí)行SQL
//先執(zhí)行SQL骨架
String sql = "delete from user where id=?";
ps = conn.prepareStatement(sql);
//給第一個問號,設置值是1
ps.setInt(1,1);
//4,執(zhí)行SQL
int rows = ps.executeUpdate();
System.out.println("刪除成功");
}catch (Exception e){
System.out.println("執(zhí)行失敗...");
}finally{ //最終一定會被執(zhí)行的
//5,釋放資源
JDBCUtils.close(null,ps,conn);
}
}
}
–3,制作工具類
package cn.tedu.test;
import java.sql.*;
//提取jdbc重復的代碼,提高復用性
public class JDBCUtils {
/**
* 釋放資源
* @param rs 結果集
* @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í)行失敗...");//項目上線后的
//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,注冊驅(qū)動
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,概述
是超文本標記語言,是指可以在網(wǎng)頁中加入比文本更豐富的內(nèi)容。標記有很多,要寫開始標記和結果標記 <html></html>
–2,入門案例
html> <head> <title>hello html~</title> </head> <body> test...... </body> </html>
–3,使用工具




<!DOCTYPE html> <!-- 聲明這是一個HTML文件 --> <html> <!-- HTML根標簽--> <head> <!-- 頭部信息,設置網(wǎng)頁的標題,編碼。。。--> <meta charset="utf-8"> <!-- 設置網(wǎng)頁的編碼 --> <title> html測試 </title> <!-- 設置網(wǎng)頁的標題 --> </head> <body> <!-- 體信息,設置網(wǎng)頁中要顯示的內(nèi)容 --> <!-- 這是HTML的注釋,Hbuilder復制粘貼一行ctrl c/ctrl v ,剪切ctrl x,調(diào)整位置ctrl ↑↓ --> 你好 html~ 你好html~ <br/> <!-- br可以在網(wǎng)頁中實現(xiàn)換行--> 你 好html~ <!-- 可以在網(wǎng)頁中實現(xiàn)空格--> 你好html~ 你好html~ 你好html~ </body> </html>
–4,測試

四,測試常用標簽
<!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> 中國成功發(fā)射風云三號05星 </li> <li> 江蘇女生中考757分8門滿分 </li> </ol> <!-- 3. 圖片標簽,在網(wǎng)頁中插入一個圖片 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屬性用來指定點擊時要跳轉(zhuǎn)的路徑 target屬性用來指定是否打開新窗口 --> <a 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" /> <!-- 數(shù)字類型--> <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>
總結
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
詳解Java并發(fā)編程中的優(yōu)先級隊列PriorityBlockingQueue
PriorityBlockingQueue是Java中實現(xiàn)了堆數(shù)據(jù)結構的線程安全的有界阻塞隊列。本文將會深入解讀PriorityBlockingQueue的源碼實現(xiàn),感興趣的可以了解一下2023-05-05
總結Junit4,Junit5,Jupiter之間的聯(lián)系
Jupiter和Junit5之間有什么聯(lián)系?Jupiter提供了哪些新的測試方法?如何用IDEA和Jupiter生成可讀性更好的測試報告?文中有非常詳細的說明,需要的朋友可以參考下2021-06-06
Springboot與vue實例講解實現(xiàn)前后端分離的人事管理系統(tǒng)
這篇文章主要介紹了如何用Java實現(xiàn)企業(yè)人事管理系統(tǒng),文中采用springboot+vue實現(xiàn)前后端分離,感興趣的小伙伴可以學習一下2022-06-06
詳解spring boot starter redis配置文件
spring-boot-starter-Redis主要是通過配置RedisConnectionFactory中的相關參數(shù)去實現(xiàn)連接redis service。下面通過本文給大家介紹在spring boot的配置文件中redis的基本配置,需要的的朋友參考下2017-07-07
java使用ffmpeg實現(xiàn)上傳視頻的轉(zhuǎn)碼提取視頻的截圖等功能(代碼操作)
這篇文章主要介紹了java使用ffmpeg實現(xiàn)上傳視頻的轉(zhuǎn)碼,提取視頻的截圖等功能,本文圖文并茂給大家介紹的非常詳細,對大家的工作或?qū)W習具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

