JDBC操作Mysql資料庫的六大步驟

2021-10-06 22:23:39 字數 940 閱讀 7659

jdbc運算元據庫是最基礎的操作方式,後來的mybatis是對其的封裝;一般認為有六大步驟。

匯入jar包

載入驅動

class.

forname

("com.mysql.jdbc.driver"

)

建立連線

connection con = drivermannerger.

getconnection()

;

執行sql語句

string sql =

"select ... from ... where ...?"

;preparestatement pre = con.

preparestatement

(sql)

;pre.

setobject

(i,params)

;resultset rs = pre.

executequery()

;

處理結果集

while

(rs.

next()

)

關閉連線

con.

close()

;pre.

close()

;rs.

close()

;

編寫過程中會有異常產生,可以向外拋異常(throws),也可以抓取異常(try…catch…),這需要視情況而定。一般情況都將異常向外拋,在main方法中處理。

JDBC連線mysql資料庫操作

一.建立所需物件,並進行初始化 connection connection null statement statement null preparedstatement pst resultset rs null 二.載入mysql驅動 class.forname com.mysql.jdbc.d...

Jdbc操作MySql資料庫插入資料效能研究

今天需要構造點資料測試,就寫了個jdbc往mysql中插資料,起初我想先插個10w條,但是開始執行後,我就陷入了漫長的等待中,於是我很納悶?插個10w條資料我都吃個飯回來了還沒查完,至少也用了二十多分鐘了吧.震驚了 於是上網查了些關於jdbc操作mysql資料庫的例子,發現人家插入10w條資料測試時...

jdbc 連線mysql資料庫

class.forname org.postgresql.driver newinstance 裝載資料庫驅動 string url jdbc postgresql localhost 5432 postgres connection con drivermanager.getconnection ...