JDBC的概念和常用開發步驟

2021-08-19 23:27:40 字數 1841 閱讀 3549

開發步驟

釋放資源

//標準的執行過程

trycatch(exception e)

//初始化變數

connection conn = null;

statement stmt = null;

resultset rs = null;

try}catch(exception e)finally

if(stmt != null)

if(conn != null)

//後使用先釋放

}

常用api

常用api

connection getconnection(databasepath,username,password)

//建立與資料庫的連線

類的載入

準備 解析

例子

//先假設存在乙個這樣的類:

public

class staticclass

}//測試類以及輸出結果:

public

class test1

}public

class test2

}public

class test3

}

connection

常用api

statement createstatement()

//建立乙個statement物件來將語句傳送到資料庫

callablestatement preparecall(sql)

//建立乙個callablestatement物件來呼叫資料庫儲存過程

preparedstatement preparestatement(sql)

//建立乙個preparedstatement物件來將引數化的sql語句傳送到資料庫

事務的回滾方式

statement

常用api

void addbatch(sql)

//給定的sql命令新增到此statement物件的當前命令列表中

void clearbatch()

//清空此statement物件的當前sql命令列表

intexecutebatch()

//將一批命令提交給資料庫來執行,如果全部命令執行成功,則返回更新計數組成的陣列

preparedstatement

獲取該類物件的方法

preparedstatement connection.preparestatement(sql語句);

//sql語句中所有的變數用?代替

常用api

void set***(index,value)

//將sql語句中index對應的?替換成value

resultset executequery()

//執行sql語句的查詢操作

executeupdate()

//執行sql語句的增刪改操作

resultset

常用api

next();                     //將查詢結果的指標向後移動一位。初始時指標的位置不在第一位,必須進行一次移動後才可以開始獲取

get***(fieldname); //獲取查詢結果裡指標對應的行中列名為fieldname的值,***為該值的資料型別

getobject(fieldname);

JDBC開發步驟

jdbc開發步驟 1 載入資料庫驅動 2 通過drivermanager獲取資料庫連線connection 3 通過connection獲取statement preparedstatement 4 將sql語句繫結到statement preparedstatement中去,準備向資料庫傳送sql...

JDBC開發步驟

jdbc開發步驟 1 引包 2 開啟mysql服務,如果沒有開啟 3 準備好資料庫 4 進入開發階段 1.註冊資料庫驅動 2.和資料庫建立連線 3.獲得執行sql語句的物件 4.執行sql語句 1 更新操作 包括增 刪 改executelupdate 方法,返回乙個整型,對資料庫產生影響的行數 2 ...

JDBC程式的開發步驟

獲取完整的資料庫驅動類名 用drivermanger方法獲得資料庫連線物件 呼叫連線物件的工廠方法建立語句物件 編寫完整的sql 語句 將語句物件的方法傳入sql,並返回影響的mysql資料庫中影響的行數 關閉方法 步驟1 通過反射例項化資料庫驅動類 完整的資料庫驅動類名 class.forname...