關於jdbc連線

2021-09-11 17:14:25 字數 1023 閱讀 1057

1、載入jdbc驅動,載入mysql的驅動類

class.forname(「com.mysql.jdbc.driver」);

2、提供jdbc連線的url

jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=gbk;

3、建立資料庫的連線

connection con = drivermanager.getconnection(url , username , password ) ;

4、建立乙個statement 控制代碼物件

statement stmt = con.createstatement() ; 執行靜態sql語句。通常通過statement例項實現

preparedstatement pstmt = con.preparestatement(sql) ; 執行動態sql語句。通常通過preparedstatement例項實現

callablestatement cstmt = con.preparecall("") ;執行資料庫儲存過程。通常通過callablestatement例項實現

5、執行sql語句

resultset rs = stmt.executequery(「select * from …」) ;resultset executequery(string sqlstring):執行查詢資料庫的sql語句,

返回乙個結果集(resultset)物件

int rows = stmt.executeupdate(「insert into …」) ; int executeupdate(string sqlstring):用於執行insert、update或 delete語句以及sql ddl語句,

如:create table和drop table等

boolean flag = stmt.execute(string sql) ;execute(sqlstring):用於執行返回多個結果集、多個更新計數或二者組合的 語句。

6、處理結果

7、關閉jdbc物件

JDBC連線出錯

在連線資料庫來運算元據時,出現如下錯誤資訊 org.springframework.beans.factory.xml.xmlbeandefinitionreader loading xml bean definitions from class path resource org springfr...

JDBC連線屬性

hibernate 需要進行資料庫訪問,因此必須設定連線資料庫的相關屬性。所有 hibernate 屬性的名字和語義都在 org.hibernate.cfg.environment 中定義。下面是關於 jdbc 連線配置中最重要的設定。hibernate.connection.driver clas...

JDBC連線屬性

hibernate 需要進行資料庫訪問,因此必須設定連線資料庫的相關屬性。所有 hibernate 屬性的名字和語義都在 org.hibernate.cfg.environment 中定義。下面是關於 jdbc 連線配置中最重要的設定。hibernate.connection.driver clas...