使用JDBC的基本步驟

2021-09-01 02:32:39 字數 3834 閱讀 8381

drivermanager.registerdriver(new com.mysql.cj.jdbc.driver());
"jdbc:mysql://localhost/資料庫名?characterencoding=utf-8&usessl=false&servertimezone=utc", "登入名", "密碼");
//建立statement,跟資料庫打交道,一定需要這個物件

statement = connection.createstatement();

//執行查詢,得到結果集

string sql = "select * from t_stu";

resultset = statement.executequery(sql);

//遍歷查詢每一條記錄

if(resultset.next())

*insert

insert into t_stu (name, age) values (『wangpangzi』, 24)

insert into t_stu values(null, 『wangpang2』, 25)

//獲取資料庫物件

connection = jdbcutil.getconn();

//根據連線物件,得到statement

statement = connection.createstatement();

//執行新增的sql語句

string sql = "insert into t_stu values(null, 'aobama', 60)";

//executeupdate影響的行數,如果大於0表明操作成功。否則失敗

int result = statement.executeupdate(sql);

if(result > 0 )else

*delete

delete from t_stu where id = 7

//獲取資料庫物件

connection = jdbcutil.getconn();

//根據連線物件,得到statement

statement = connection.createstatement();

//執行sql語句

string sql = "delete from t_stu where id = 11";

//executeupdate影響的行數,如果大於0表明操作成功。否則失敗

int result = statement.executeupdate(sql);

if(result > 0 )else

} catch (sqlexception e) finally

*query

select * from t_stu

//註冊驅動

connection = jdbcutil.getconn();

//建立statement,跟資料庫打交道,一定需要這個物件

statement = connection.createstatement();

//執行查詢,得到結果集

string sql = "select * from t_stu";

resultset = statement.executequery(sql);

//遍歷每一條記錄

while(resultset.next())

} catch (exception e) finally

*update

update t_stu set age = 38 where id = 8

try else 

} catch (sqlexception e) finally

定義乙個類, test***,裡面定義方法 test***

新增junit的支援

新增 junit-4.12.jar和 harmcrest-core-1.3.jar

在方法的上面新增@test註解,其實就是乙個標記

@test

public void testquery()

游標選中方法名字,然後右鍵執行單元測試。 或者alt + 7開啟structure檢視,選中方法右鍵執行

data access object 資料庫訪問物件

新建乙個dao的介面,裡面宣告資料庫訪問規則

新建乙個dao的實現類,具體實現早前定義的規則

public class userdaoimpl implements userdao 

} catch (exception e) finally

} }

直接使用實現

@test           

public void testfindall()

statement執行,其實是拼接sql語句。順序:先拼接sql語句,然後再一起執行。

string sql = "select * from t_user where username = '"+ username +"' and password = '"+ password +"'";

userdao dao = new userdaoimpl();

dao.login("admin", "10080gnbxf' or '1=1");

select * from t_user where username = 'admin' and password = '10080gnbxf『 or '1=1'

前面先拼接sql語句,如果變數裡面帶有了 資料庫的關鍵字,那麼一併認為是關鍵字。不認為是普通的字串。

resultset = statement.executequery(sql);

該物件就是替換前面的statement物件

相比較之前的statement,預先處理給定的sql語句,對其進行語法檢查。在sql語句裡面使用「?」佔位符來替代後續要傳遞進來的變數。後面進來的變數值,將會被看成是字串,不會產生任何的關鍵字。

string sql = "insert into t_user values (null, ?, ?)";

preparedstatement = connection.preparestatement(sql);

//給佔位符「?」賦值

preparedstatement.setstring(1, username);

preparedstatement.setstring(2, password);

jdbc入門

抽取工具類 ###

statenment crud ###

演練crud
dao模式 ###

宣告與實現分開
preparestatement crud ###

預處理sql語句,解決上面statement出現的問題

使用JDBC的基本步驟

所以使用sql訪問資料庫有五個基本步驟,分別為 1 載入資料庫驅動程式 程式如下 class.from com.mysql.jdbc.driver 2 建立資料庫連線 程式如下 connection connection drivermannger.getconnection jdbc mysql ...

JDBC的使用步驟

使用jdbc對mysql進行操作,主要分為這樣幾個步驟 首先,定義幾個變數,方便後續關閉該連線 connection conn null statement stat null resultset rs null 接下來,具體步驟 1.註冊驅動 class.forname com.mysql.cj....

JDBC使用步驟

註冊驅動 drivermanager.registerdriver new com.mysql.jdbc.driver 獲取資料連線 connection conn drivermanager.getconnection jdbc mysql localhost 3306 day1126 root ...