Java使用JDBC連接數據庫的詳細步驟_第1頁
Java使用JDBC連接數據庫的詳細步驟_第2頁
Java使用JDBC連接數據庫的詳細步驟_第3頁
Java使用JDBC連接數據庫的詳細步驟_第4頁
Java使用JDBC連接數據庫的詳細步驟_第5頁
已閱讀5頁,還剩5頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第Java使用JDBC連接數據庫的詳細步驟Connectionconn=null;

Statementstmt=null;//先創建連接對象和操作對象并且引用為空,是為了對象變量的生命周期不僅僅局限于try語句塊內,而是在整個main方法內,方便后續finally語句塊內釋放資源

try{

//1、注冊驅動

Driverdriver=newcom.mysql.jdbc.Driver();//多態,父類型引用指向子類型對象

DriverManager.registerDriver(driver);

//2、獲取連接

url包括哪幾部分:

Port

eg:1:80/index.html

http://通信協議

1IP地址

80端口號

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(數據庫連接對象:+conn);//數據庫連接對象com.mysql.jdbc.JDBC4Connection@1ae369b7

//3、獲取數據庫操作對象

//Statement類中createStatement()創建一個Statement對象來將SQL語句發送到數據庫。

stmt=conn.createStatement();

//4、執行sql語句

//intexecuteUpdate(Stringsql)

//專門執行DML語句

//返回值是影響數據庫中的記錄條數

intcount=stmt.executeUpdate(updatedeptsetdname=銷售部,loc=合肥wheredeptno=20;

System.out.println(count==1保存成功:保存失敗

//5、處理查詢結果集

}catch(SQLExceptione){

e.printStackTrace();

}finally{

//6、釋放資源

//從小到大依次關閉

//finally語句塊內的語句一定會執行!

if(stmt!=null){

try{

stmt.close();

catch(SQLExceptione){

e.printStackTrace();

if(conn!=null){

try{

conn.close();

catch(SQLExceptione){

e.printStackTrace();

}

第二次優化:(比較兩種注冊驅動的方法)

packagecom.zdx.source.code.jdbc;

JDBC完成Delete

importjava.sql.*;

publicclassJDBCTest02{

publicstaticvoidmain(String[]args){

//1、注冊驅動

//2、獲取連接

//3、獲取數據庫操作對象

//4、執行sql語句

//5、獲取查詢結果集

//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();

}

第三次優化:(最佳注冊驅動獲取連接)

packagecom.zdx.source.code.jdbc;

注冊驅動的另一種方式

importjava.sql.*;

publicclassJDBCTest03{

publicstaticvoidmain(String[]args){

try{

//注冊驅動

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();

}

第四次優化:(使用資源綁定器)

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();

}

第五次優化:(對操作結果集的處理)

packagecom.zdx.source.code.jdbc;

執行DQL語句

importjava.sql.*;

importjava.util.*;

publicclassJDBCTest05{

publicstaticvoidmain(String[]args){

//1、注冊驅動

//2、建立連接

//3、獲取數據庫操作對象

//4、執行sql語句

//5、獲取查詢結果集

//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);

//按下標取出,程序不健壯

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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論