Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟_第1頁(yè)
Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟_第2頁(yè)
Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟_第3頁(yè)
Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟_第4頁(yè)
Java使用JDBC連接數(shù)據(jù)庫(kù)的詳細(xì)步驟_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論