




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第Java使用JDBC連接數(shù)據(jù)庫的詳細步驟Connectionconn=null;
Statementstmt=null;//先創(chuàng)建連接對象和操作對象并且引用為空,是為了對象變量的生命周期不僅僅局限于try語句塊內(nèi),而是在整個main方法內(nèi),方便后續(xù)finally語句塊內(nèi)釋放資源
try{
//1、注冊驅(qū)動
Driverdriver=newcom.mysql.jdbc.Driver();//多態(tài),父類型引用指向子類型對象
DriverManager.registerDriver(driver);
//2、獲取連接
url包括哪幾部分:
Port
eg:1:80/index.html
http://通信協(xié)議
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(數(shù)據(jù)庫連接對象:+conn);//數(shù)據(jù)庫連接對象com.mysql.jdbc.JDBC4Connection@1ae369b7
//3、獲取數(shù)據(jù)庫操作對象
//Statement類中createStatement()創(chuàng)建一個Statement對象來將SQL語句發(fā)送到數(shù)據(jù)庫。
stmt=conn.createStatement();
//4、執(zhí)行sql語句
//intexecuteUpdate(Stringsql)
//專門執(zhí)行DML語句
//返回值是影響數(shù)據(jù)庫中的記錄條數(shù)
intcount=stmt.executeUpdate(updatedeptsetdname=銷售部,loc=合肥wheredeptno=20;
System.out.println(count==1保存成功:保存失敗
//5、處理查詢結(jié)果集
}catch(SQLExceptione){
e.printStackTrace();
}finally{
//6、釋放資源
//從小到大依次關(guān)閉
//finally語句塊內(nèi)的語句一定會執(zhí)行!
if(stmt!=null){
try{
stmt.close();
catch(SQLExceptione){
e.printStackTrace();
if(conn!=null){
try{
conn.close();
catch(SQLExceptione){
e.printStackTrace();
}
第二次優(yōu)化:(比較兩種注冊驅(qū)動的方法)
packagecom.zdx.source.code.jdbc;
JDBC完成Delete
importjava.sql.*;
publicclassJDBCTest02{
publicstaticvoidmain(String[]args){
//1、注冊驅(qū)動
//2、獲取連接
//3、獲取數(shù)據(jù)庫操作對象
//4、執(zhí)行sql語句
//5、獲取查詢結(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)化:(最佳注冊驅(qū)動獲取連接)
packagecom.zdx.source.code.jdbc;
注冊驅(qū)動的另一種方式
importjava.sql.*;
publicclassJDBCTest03{
publicstaticvoidmain(String[]args){
try{
//注冊驅(qū)動
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)化:(對操作結(jié)果集的處理)
packagecom.zdx.source.code.jdbc;
執(zhí)行DQL語句
importjava.sql.*;
importjava.util.*;
publicclassJDBCTest05{
publicstaticvoidmain(String[]args){
//1、注冊驅(qū)動
//2、建立連接
//3、獲取數(shù)據(jù)庫操作對象
//4、執(zhí)行sql語句
//5、獲取查詢結(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);
//按下標取出,程序不健壯
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年點火系統(tǒng):點火開關(guān)項目合作計劃書
- 2025年紅外線汽車尾氣分析儀項目合作計劃書
- 教育技術(shù)與OER的深度融合研究
- 2025年年中國電子政務合作協(xié)議書
- 腦波技術(shù)在學習障礙診斷中的應用前景
- 2025年廣東省深圳市龍文一對一物理高一下期末質(zhì)量檢測試題含解析
- 湖北省漢川二中2025年物理高二下期末檢測模擬試題含解析
- 商業(yè)洞察生物機制驅(qū)動的可持續(xù)發(fā)展戰(zhàn)略
- 技術(shù)引領(lǐng)未來中醫(yī)傳承在智能教育領(lǐng)域的發(fā)展
- 2025年湖南省東安一中高一物理第二學期期末質(zhì)量檢測模擬試題含解析
- 中醫(yī)醫(yī)療技術(shù)手冊2013普及版
- 腎骨片產(chǎn)品課件
- 幼師應聘個人簡歷表格
- 海運出口培訓課程教學課件
- 2023年副主任醫(yī)師(副高)-內(nèi)科學(副高)考試歷年高頻考點參考題庫附帶專家答案
- GB/T 15114-2023鋁合金壓鑄件
- 中國穩(wěn)定性冠心病診斷與治療指南
- GB/T 7543-2020一次性使用滅菌橡膠外科手套
- GB/T 29790-2020即時檢驗質(zhì)量和能力的要求
- GB/T 1796.5-2016輪胎氣門嘴第5部分:大芯腔氣門嘴
- GB/T 1094.11-2022電力變壓器第11部分:干式變壓器
評論
0/150
提交評論