編碼的世界 / 優質文選 / 文明

cgb2109-day07


2022年5月04日
-   

文章目錄

一,SQL攻擊


–1,模擬SQL攻擊的現象
//出現了問題:SQL攻擊: //1,本質上就是因為SQL中出現了特殊符號#,#號在SQL中是注釋的意思(測試時使用固定的用戶名jack’#) //2,Statement傳輸器在執行SQL時遇到了SQL拼接,把#當做了注釋用!!
package cn.tedu.jdbc;
import org.junit.Test;
import java.awt.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
//模擬用戶登錄
public class Test2 {
//模擬用戶登錄,從單元測試改成main()原因是IDEA單元測試方法無法鍵盤輸入
public static void main(String[] args) throws Exception{
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2,連接數據庫
String url="jdbc:mysql:///cgb2109?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL
System.out.println("請輸入用戶名:");
String username = new Scanner(System.in).nextLine();
System.out.println("請輸入密碼:");
String password = new Scanner(System.in).nextLine();
//拼接字符串: 一對兒雙引號中間一對兒加號 再中間看你了 "+???+"
// String sql="select * from user where name='jack' and pwd='123'";
String sql="select * from user where name='"+username+"' and pwd='"+password+"'";
ResultSet r = s.executeQuery(sql);
//5,處理結果
if( r.next() ){//判斷有數據嗎?如果有數據就可以登錄
System.out.println("登錄成功!");
}else{//沒數據,重新輸入或去注冊
System.out.println("登錄失敗,請重新輸入或去注冊!");
}
//6,釋放資源
r.close();
s.close();
c.close();
}
}

–2,解決方案
package cn.tedu.jdbc;
import org.junit.Test;
import java.awt.*;
import java.sql.*;
import java.util.Scanner;
//模擬用戶登錄
public class Test2 {
//解決了問題:SQL攻擊:
//1,本質上就是因為SQL中出現了特殊符號#,當普通字符用而不是注釋
//2,PreparedStatement 傳輸器在執行SQL時遇到了SQL拼接時直接寫?占位符
//PreparedStatement好處:SQL簡單,安全(解決SQL攻擊),高效
//模擬用戶登錄,從單元測試改成main()原因是IDEA單元測試方法無法鍵盤輸入
public static void main(String[] args) throws Exception{
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2,連接數據庫
String url="jdbc:mysql:///cgb2109?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
//3,獲取傳輸器
// Statement s = c.createStatement();//SQL攻擊問題
//4,執行SQL
System.out.println("請輸入用戶名:");
String username = new Scanner(System.in).nextLine();
System.out.println("請輸入密碼:");
String password = new Scanner(System.in).nextLine();
// String sql="select * from user where name='"+username+"' and pwd='"+password+"'";
//SQL骨架,?叫占位符
String sql="select * from user where name=? and pwd=?";
//准備執行預編譯的SQL
//PreparedStatement好處:SQL簡單,安全(解決SQL攻擊),高效
PreparedStatement s = c.prepareStatement(sql);
//設置SQL中的參數
s.setObject(1,username);//給第1個?設置用戶名
s.setObject(2,password);//給第2個?設置用戶名
ResultSet r = s.executeQuery();
//5,處理結果
if( r.next() ){//判斷有數據嗎?如果有數據就可以登錄
System.out.println("登錄成功!");
}else{//沒數據,重新輸入或去注冊
System.out.println("登錄失敗,請重新輸入或去注冊!");
}
//6,釋放資源
r.close();
s.close();
c.close();
}
}

–3,練習PreparedStatement
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//需求:利用新的傳輸器,向dept表裏插入數據
public class Test3 {
public static void main(String[] args) throws Exception{
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2,獲取連接
String url="jdbc:mysql://localhost:3306/cgb2109?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
//3,傳輸器
String sql ="insert into dept values(null,?,?)";//SQL骨架
PreparedStatement p = c.prepareStatement(sql);
//設置SQL中的參數給第幾個?設置啥值
p.setObject(1,"php開發部");
p.setObject(2,"北京");
//4,執行SQL
p.executeUpdate();//執行增刪改的SQL,返回影響行數
//5,處理結果
//6,釋放資源
p.close();
c.close();
}
}

–4,擴展: 程序優化
JDBC的前兩步,重複的寫了很多次,優化這種現象來提高代碼的複用性/高內聚.
1,創建工具類
1,提供工具類 2,提供方法(封裝JDBC的前兩步)
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
//封裝了注冊驅動,獲取連接.
//目的:獲取連接,並返回給調用者
public class JDBCUtils {
/**目的:獲取連接,並返回給調用者 */
static public Connection get() throws Exception{
//1,注冊驅動
Class.forName("com.mysql.jdbc.Driver");
//2,獲取連接
String url="jdbc:mysql://localhost:3306/cgb2109?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url, "root", "root");
return c; //返回給調用者
}
}

2,改造測試類
3,調用類裏的方法
package cn.tedu.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//需求:利用新的傳輸器,向dept表裏插入數據
public class Test3 {
public static void main(String[] args) throws Exception{
//TODO 利用工具類裏封裝好的方法
Connection c = JDBCUtils.get();
//3,傳輸器
String sql ="insert into dept values(null,?,?)";//SQL骨架
PreparedStatement p = c.prepareStatement(sql);
//設置SQL中的參數給第幾個?設置啥值
p.setObject(1,"php開發部");
p.setObject(2,"北京");
//4,執行SQL
p.executeUpdate();//執行增刪改的SQL,返回影響行數
//5,處理結果
//6,釋放資源
c.close();
p.close();
}
}

3,總結

二,HTML


–1,概述
是超文本標記語言. 網頁中的元素類型可以超過文本內容 標記語言: HTML中提供了大量標記/標簽,開始標簽和結束標簽
–2,入門案例
1,右鍵-新建項目-輸入項目名稱-創建 2,右鍵-新建-HTML文件-輸入文件名-創建 3,保存文件-運行-運行到瀏覽器–選擇一個能用的直接測試
<!DOCTYPE html> <!是文檔聲明行,用來聲明這是一個HTML文件 >
<html> <! HTML文件裏的根元素>
<head> <! 網頁中的頭部分,優先於body加載,用來設置網頁的屬性>
<meta charset="utf-8"> <! 設置網頁的編碼 >
<title>你好,HTML</title> <! 設置網頁的標題 >
</head>
<body><! 網頁的體部分,放展示的數據 >
hell&nbsp;&nbsp;&nbsp;&nbsp;o html~
hello html~ <br></br>
<! br標簽是換行
&nbsp;表示一個空格
>
hello html~
hello html~
hello html~
hello html~
</body>
</html>

三,HTML的常見標簽


–1,概述
1,輸入框: 單選多選 2,圖片 3,按鈕 4,視頻 5,超鏈接
–2,標題,列表,圖片標簽

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測試 標題標簽</title>
</head>
<body>
<! 3.圖片標簽
src屬性用來指定圖片的位置(先保證圖片資源和網頁在同一級目錄)
width屬性用來指定圖片的寬度,單位是像素px
height屬性用來指定圖片的高度,單位是百分比
>
<img src="logo.png" width="30px" height="10%"/>
<img src="logo.png" width="30px" height="10%"/>

<! 2.列表標簽
有序列表orderlist: ol是定義列表 li定義列表項
無序列表unorderlist: ul是定義列表 li定義列表項
>
<ul>
<li>31省區市新增本土確診65例</li>
<li>神十三航天員圓滿完成出艙任務</li>
</ul>
<ol>
<li>31省區市新增本土確診65例</li>
<li>神十三航天員圓滿完成出艙任務</li>
</ol>

<! 1.標題標簽 h1大~h6小 自動換行 >
<h1>hello</h1>
<h2>hello</h2>
<h3>hello</h3>
<h4>hello</h4>
<h5>hello</h5>
<h6>hello</h6>

</body>
</html>

–3,超鏈接,輸入框標簽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測試 超鏈接標簽</title>
</head>
<body>
<! 2.input輸入框 >
密碼輸入框:<input type="password" />
普通輸入框:<input type="text" />
數字輸入框:<input type="number" />
日歷輸入框:<input type="date" />
日歷輸入框:<input type="week" />
單選框:<input type="radio" />男
多選框:<input type="checkbox" />迪麗熱巴
普通按鈕: 沒有提交數據的功能,只能點點
<input type="button" value="注冊"/>
<button>登錄</button>
提交按鈕:把用戶在瀏覽器輸入的數據提交給後端的java程序處理
<input type="submit"/>
<button type="submit">提交</button>

<br />
<! 1.超鏈接標簽
href屬性表示可以被點擊
target屬性表示用什麼方式打開
默認值是_self當前窗口,_blank是在新窗口打開
>
<a href="http://www.baidu.com/" target="_blank">百度一下</a>
<! 錨定:回到固定位置 >
<a name="top">我是頂部</a>
<h1>如何套取富婆的歡心</h1>
<h1>如何套取富婆的歡心</h1>
<h1>如何套取富婆的歡心</h1>
<h1>如何套取富婆的歡心</h1>
<a href="#top">點我,回去頂部</a><!通過#獲取name屬性的值>
</body>
</html>

–4,表格標簽
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>測試 表格標簽</title>
</head>
<body>
<! 1.准備表格
結構:table裏包含行tr,tr裏包含列td
屬性:border設置邊框,width寬度,bgcolor背景色
cellspacing單元格的距離
colspan是列合並:把多個列合並成一個大列,值是指合並幾個
rowspan是行合並:把多個行合並成一個大行,值是指合並幾個
>
<table border="1px" width="500px"
cellspacing="0px" bgcolor="greenyellow">
<tr>
<td colspan="2">11</td>
<td>13</td>
</tr>
<tr>
<td>21</td>
<td>22</td>
<td rowspan="2">23</td>
</tr>
<tr>
<td>31</td>
<td>32</td>
</tr>
</table>
</body>
</html>

四,作業


制作兩個表格

熱門文章