文章目錄
- 一,JDBC
- --1,概述
- --2,使用步驟
- 1, 提供了豐富的工具類jar包,專案中匯入jar包
- 2, 連接資料庫: 埠號3306 庫名cgb2109 用戶名root 密碼root
- 3, 寫SQL
- 4, 處理資料庫回傳給java的結果
- --3,匯入jar包
- 1,選用哪個版本的jar包,要看你安裝的資料庫的版本
- 2,匯入jar包
- --4,入門案例
- --5,總結
- 二,JDBC的練習
- --1,查詢courses表的所有資料
- --2,向dept表中插入資料
- --3,模擬用戶登錄
- 1, 讓用戶輸入用戶名 和 密碼
- 2, 在資料庫里 準備表
- 3, 利用JDBC查庫,發起select陳述句
- 三,SQL攻擊
- --1,模擬SQL攻擊的現象
- --2,解決方案
一,JDBC
–1,概述
sun公司提供的一套 java操作資料庫的標準
專門用來完成 java 和 資料庫 互動的技術,全稱是: java database connectivity
–2,使用步驟
1, 提供了豐富的工具類jar包,專案中匯入jar包
2, 連接資料庫: 埠號3306 庫名cgb2109 用戶名root 密碼root
3, 寫SQL
4, 處理資料庫回傳給java的結果
–3,匯入jar包
1,選用哪個版本的jar包,要看你安裝的資料庫的版本

2,匯入jar包
1, 復制粘貼到工程里
2, 選中jar包,右鍵-add as libarary… ok

–4,入門案例
package cn.tedu.jdbc;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//測驗jdbc
public class Test1 {
//junit單元測驗:@Test public void 沒有引數
@Test
public void get() throws Exception {
//1,注冊驅動 --引數是類的全路徑
Class.forName("com.mysql.jdbc.Driver");//5版本的jar包
//Class.forName("com.mysql.cj.jdbc.Driver");//8版本的jar包
//2,連接資料庫,并回傳了資料庫的連接Connection
String url = "jdbc:mysql://localhost:3306/cgb2109";
//遵循的協議://本機ip地址:埠號/資料庫名
Connection c = DriverManager.getConnection(url,"root","root");
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL陳述句
//需求:利用java查詢資料庫中dpet表的所有資料
ResultSet r = s.executeQuery("select * from dept");//執行查詢的SQL陳述句
//s.executeUpdate()執行增刪改的SQL陳述句
//5,處理結果
while( r.next() ){//next()判斷有沒有資料
//getXxx()獲取不同型別的資料
// r.getInt(1);//根據列的編號獲取列的值
int a=r.getInt("deptno");//根據列名獲取列的值
String b = r.getString("dname");
String d = r.getString(3);
System.out.println(a+b+d);
}
//6,釋放資源
r.close();//結果集
s.close();//傳輸器
c.close();//連接器
}
}
–5,總結

二,JDBC的練習
–1,查詢courses表的所有資料
package cn.tedu.jdbc;
import org.junit.Test;
import java.sql.*;
//測驗jdbc
public class Test1 {
//需求:查詢courses表的所有資料
@Test
public void get2() throws Exception{
//1,注冊驅動 --引數是類的全路徑
Class.forName("com.mysql.jdbc.Driver");
//2,連接資料庫,并回傳了資料庫的連接Connection
String url="jdbc:mysql://localhost:3306/cgb2109?characterEncoding=utf8";
Connection c = DriverManager.getConnection(url,"root","root");
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL
ResultSet r = s.executeQuery("select * from courses");//查詢SQL
//5,處理結果
while(r.next()){//判斷有沒有資料
//表里有三列,回圈三次就行了
for (int i = 1; i <4 ; i++) {
//獲取資料getXxx()--getObject()更通用可以獲取任意型別的資料
Object a = r.getObject(i);//i是列的索引1 2 3..列
System.out.println(a);//列印每列的值
}
}
//6,釋放資源
r.close();
s.close();
c.close();
}
}
–2,向dept表中插入資料
//需求:向dept表中插入資料
@Test
public void add() throws Exception{
//1,注冊驅動 --引數是類的全路徑
Class.forName("com.mysql.jdbc.Driver");
//2,連接資料庫,并回傳了資料庫的連接Connection
// String url="jdbc:mysql://localhost:3306/cgb2109?characterEncoding=utf8";
String url="jdbc:mysql:///cgb2109?characterEncoding=utf8";//簡寫
Connection c = DriverManager.getConnection(url,"root","root");
//3,獲取傳輸器
Statement s = c.createStatement();
//4,執行SQL
int rows = s.executeUpdate(
"insert into dept values(null,'java開發',\"北京\")");
//5,處理結果
//executeQuery查詢,會把結果回傳給ResultSet需要遍歷,,,
//executeUpdate增刪改,會回傳對資料庫的影響行數,,,通常不處理
//6,釋放資源
s.close();
c.close();
}
–3,模擬用戶登錄
1, 讓用戶輸入用戶名 和 密碼
2, 在資料庫里 準備表
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(20) default NULL,
`pwd` varchar(10) default NULL,
PRIMARY KEY (`id`)
)
3, 利用JDBC查庫,發起select陳述句
select * from user where name=‘jack’ and password=‘123’
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;
//模擬用戶登錄
public class Test2 {
//模擬用戶登錄
@Test
public void login() 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
String sql="select * from user where name='jack' and pwd='123'";
ResultSet r = s.executeQuery(sql);
//5,處理結果
if( r.next() ){//判斷有資料嗎?如果有資料就可以登錄
System.out.println("登錄成功!");
}else{//沒資料,重新輸入或去注冊
System.out.println("登錄失敗,請重新輸入或去注冊!");
}
//6,釋放資源
r.close();
s.close();
c.close();
}
}
三,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,解決方案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/349590.html
標籤:java
下一篇:Spring原始碼|決議深入Spring原始碼多圖剖析@Configuration背后的BeanFactory后置處理器實作邏輯
