




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟Connectionconn=null;
Statementstmt=null;//先創(chuàng)建連接對(duì)象和操作對(duì)象并且引用為空,是為了對(duì)象變量的生命周期不僅僅局限于try語(yǔ)句塊內(nèi),而是在整個(gè)main方法內(nèi),方便后續(xù)finally語(yǔ)句塊內(nèi)釋放資源
try{
//1、注冊(cè)驅(qū)動(dòng)
Driverdriver=newcom.mysql.jdbc.Driver();//多態(tài),父類(lèi)型引用指向子類(lèi)型對(duì)象
DriverManager.registerDriver(driver);
//2、獲取連接
url包括哪幾部分:
Port
eg:1:80/index.html
http://通信協(xié)議
1IP地址
80端口號(hào)
index.html資源名
//staticConnectiongetConnection(Stringurl,Stringuser,Stringpassword)
Stringurl=jdbc:mysql://:3306/hello
Stringuser=root
System.out.println(
Stringpassword=rota
conn=DriverManager.getConnection(url,user,password);
System.out.println(數(shù)據(jù)庫(kù)連接對(duì)象:+conn);//數(shù)據(jù)庫(kù)連接對(duì)象com.mysql.jdbc.JDBC4Connection@1ae369b7
//3、獲取數(shù)據(jù)庫(kù)操作對(duì)象
//Statement類(lèi)中createStatement()創(chuàng)建一個(gè)Statement對(duì)象來(lái)將SQL語(yǔ)句發(fā)送到數(shù)據(jù)庫(kù)。
stmt=conn.createStatement();
//4、執(zhí)行sql語(yǔ)句
//intexecuteUpdate(Stringsql)
//專(zhuān)門(mén)執(zhí)行DML語(yǔ)句
//返回值是影響數(shù)據(jù)庫(kù)中的記錄條數(shù)
intcount=stmt.executeUpdate(updatedeptsetdname=銷(xiāo)售部,loc=合肥wheredeptno=20;
System.out.println(count==1保存成功:保存失敗
//5、處理查詢(xún)結(jié)果集
}catch(SQLExceptione){
e.printStackTrace();
}finally{
//6、釋放資源
//從小到大依次關(guān)閉
//finally語(yǔ)句塊內(nèi)的語(yǔ)句一定會(huì)執(zhí)行!
if(stmt!=null){
try{
stmt.close();
catch(SQLExceptione){
e.printStackTrace();
if(conn!=null){
try{
conn.close();
catch(SQLExceptione){
e.printStackTrace();
}
第二次優(yōu)化:(比較兩種注冊(cè)驅(qū)動(dòng)的方法)
packagecom.zdx.source.code.jdbc;
JDBC完成Delete
importjava.sql.*;
publicclassJDBCTest02{
publicstaticvoidmain(String[]args){
//1、注冊(cè)驅(qū)動(dòng)
//2、獲取連接
//3、獲取數(shù)據(jù)庫(kù)操作對(duì)象
//4、執(zhí)行sql語(yǔ)句
//5、獲取查詢(xún)結(jié)果集
//6、釋放資源
Connectionconn=null;
Statementstmt=null;
try{
Driverdriver=newcom.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
Stringurl=jdbc:mysql://:3306/mydatabase
Stringuser=root
Stringpassword=146
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
intcount=stmt.executeUpdate(deletefromdeptwheredeptno=50
System.out.println(count==1刪除成功:刪除失敗
}catch(SQLExceptione){
e.printStackTrace();
}finally{
if(conn!=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
if(stmt!=null){
try{
stmt.close();
}catch(SQLExceptione){
e.printStackTrace();
}
第三次優(yōu)化:(最佳注冊(cè)驅(qū)動(dòng)獲取連接)
packagecom.zdx.source.code.jdbc;
注冊(cè)驅(qū)動(dòng)的另一種方式
importjava.sql.*;
publicclassJDBCTest03{
publicstaticvoidmain(String[]args){
try{
//注冊(cè)驅(qū)動(dòng)
Class.forName(com.mysql.jdbc.Driver
//獲取連接
Connectionconn=DriverManager.getConnection(jdbc:mysql://localhost:3306/mydatabase,root,146
System.out.println(conn);
}catch(SQLExceptione){
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
第四次優(yōu)化:(使用資源綁定器)
packagecom.zdx.source.code.jdbc;
使用資源綁定器
importjava.sql.*;
importjava.util.*;
publicclassJDBCTest04{
publicstaticvoidmain(String[]args){
ResourceBundlebundle=ResourceBundle.getBundle(jdbc
Stringdriver=bundle.getString(driver
Stringurl=bundle.getString(url
Stringuser=bundle.getString(user
Stringpassword=bundle.getString(password
Connectionconn=null;
Statementstmt=null;
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
intcount=stmt.executeUpdate(insertintodept(deptno,dname,loc)values(50,人事部,北京
System.out.println(count==1保存成功:保存失敗
}catch(SQLExceptione){
e.printStackTrace();
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}finally{
if(conn!=null){
try{
conn.close();
}catch(SQLExceptione){
e.printStackTrace();
if(stmt!=null){
try{
stmt.close();
}catch(SQLExceptione){
e.printStackTrace();
}
第五次優(yōu)化:(對(duì)操作結(jié)果集的處理)
packagecom.zdx.source.code.jdbc;
執(zhí)行DQL語(yǔ)句
importjava.sql.*;
importjava.util.*;
publicclassJDBCTest05{
publicstaticvoidmain(String[]args){
//1、注冊(cè)驅(qū)動(dòng)
//2、建立連接
//3、獲取數(shù)據(jù)庫(kù)操作對(duì)象
//4、執(zhí)行sql語(yǔ)句
//5、獲取查詢(xún)結(jié)果集
//6、釋放資源
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
ResourceBundlerb=ResourceBundle.getBundle(jdbc
Stringdriver=rb.getString(driver
Stringurl=rb.getString(url
Stringuser=rb.getString(user
Stringpassword=rb.getString(password
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery(selectempno,ename,salfromemp
while(rs.next()){
Stringempno=rs.getString(1);
Stringename=rs.getString(2);
Stringsal=rs.getString(3);
System.out.println(empno+,+ename+,+sal);
//按下標(biāo)取出,程序不健壯
Stringempno=rs.getString(empno
Stringename=rs.getString(ename
Stringsal=rs.getString(sal
System.out.println(empno+,+ename+,+sal);
//以指定的格式取出
intempno=rs.getInt(1);
Stringename=rs.getString(2);
doublesal=rs.getDouble(3);
System.out.println(empno+,+ename+,+(sal+100));
intempno=rs.getInt(empno
Stringename=rs.getString(ename
doublesal=rs.getDouble(sal
System.out.println(empno+,+ename+,+(sal+200));
}catch(Exceptione){
e.printStackTrace();
}finally{
if(rs!=null){
try{
rs.close();
}catch(Exceptione){
e.printStackTrace();
if(stmt!=null){
try{
stmt.close();
}catch(Exceptione){
e.printStackTrace();
if(conn!=null){
try{
conn.close();
}catch(Exceptione){
e.printStackT
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《皮膚及其軟組織的感染》課件
- 試衣工作應(yīng)聘簡(jiǎn)歷模板
- 《課件教程:如何編寫(xiě)高質(zhì)量的教育研究課題計(jì)劃》
- 雙十一旅游搶購(gòu)戰(zhàn)略
- 雙十二旅游市場(chǎng)策略
- 《康復(fù)護(hù)理評(píng)估與實(shí)踐》課件
- 修改課件:生命主題班會(huì)的優(yōu)化與更新
- 胸椎結(jié)核的診斷與治療護(hù)理常規(guī)課件
- 2025中小企業(yè)貸款合同模板
- 2025雇傭離職人員勞動(dòng)合同
- 《電磁感應(yīng)原理解析》課件
- 成都輸液劑項(xiàng)目可行性研究報(bào)告參考范文
- 2025年二級(jí)注冊(cè)建筑師資格考試《建筑結(jié)構(gòu)、建筑物理與設(shè)備》真題卷(附答案)
- 鋰電池基礎(chǔ)知識(shí)培訓(xùn)課件
- 2025-2030城市燃?xì)猱a(chǎn)業(yè)行業(yè)市場(chǎng)現(xiàn)狀供需分析及投資評(píng)估規(guī)劃分析研究報(bào)告
- 緊固件制造企業(yè)ESG實(shí)踐與創(chuàng)新戰(zhàn)略研究報(bào)告
- 優(yōu)化醫(yī)患溝通提高腫瘤治療效果的途徑
- 2025北京九年級(jí)(上)期末語(yǔ)文匯編:文言文閱讀
- 越出站界調(diào)車(chē)RAILWAY課件
- 2024年陜西高中學(xué)業(yè)水平合格考試化學(xué)試卷真題(含答案詳解)
- 2025年全國(guó)國(guó)家版圖知識(shí)競(jìng)賽題庫(kù)及答案(中小學(xué)組)
評(píng)論
0/150
提交評(píng)論