




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、實(shí)驗(yàn)二jdbc編程一、目的通過 java 數(shù)據(jù)庫訪問程序的編寫、 調(diào)試, 使學(xué)生掌握 jdbc 編程的基本方法,熟悉常用的 jdbc api ,促進(jìn)學(xué)生對(duì)概念的理解,培養(yǎng)動(dòng)手能力。二、基本要求學(xué)生需要按時(shí)達(dá)到指定實(shí)驗(yàn)室上機(jī)。調(diào)試教學(xué)中使用的程序示例, 并加以修改,增加程序的功能;實(shí)現(xiàn)數(shù)據(jù)庫訪問的優(yōu)化。完成實(shí)驗(yàn)后,需要按時(shí)提交實(shí)驗(yàn)報(bào)告。三、實(shí)驗(yàn)內(nèi)容(1)復(fù)習(xí)數(shù)據(jù)庫 sql語句的編寫。(2)編寫 java 數(shù)據(jù)庫(使用 odbc-jdbc 驅(qū)動(dòng))公共模塊。(3)建立數(shù)據(jù)庫應(yīng)用模型,對(duì)數(shù)據(jù)庫進(jìn)行操作。(4) 調(diào)試程序,實(shí)現(xiàn)數(shù)據(jù)庫的訪問。3.1 數(shù)據(jù)源的確定可能是我不知道怎么配置,我的電腦是windo
2、ws7系統(tǒng),和學(xué)校windowsxp系統(tǒng)的電腦配置方法不太一樣,需要在c:windowssystem32 路徑中,找到odbcad32.exe 才能順利修改數(shù)據(jù)源。數(shù)據(jù)源設(shè)置好了以后就可以編輯代碼并正常運(yùn)行了。下面以一張截圖展示我的數(shù)據(jù)庫3.2 數(shù)據(jù)庫信息查詢 . 一3.2.1 代碼import java.sql.*; /*此處提供了 sql 包,就可以放心使用 sql 語句了 */ public class students 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 1 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇
3、p d f - - - - - - - - - - - - - - 第 1 頁,共 14 頁 - - - - - - - - -public static void main(string args) /look out this s of string ! string driver=sun.jdbc.odbc.jdbcodbcdriver; /*這里定義了 driver 做驅(qū)動(dòng)器使用, 為了美觀事先定義, 其實(shí)放在 class.forname的括號(hào)里也是完全正確的*/ string url=jdbc:odbc:students; /*此處指定了數(shù)據(jù)庫,要注意兩者名字一定要一樣,否則找不到數(shù)
4、據(jù)源*/ string query=select name,num,course,score from students; /focus on the sql language :select.from. ! /*需要執(zhí)行的 sql 語句*/ connection con=null; /*鏈接的定義 */ statement s=null; /*statement 對(duì)象的定義,兩者賦值null是為了在 try 塊出現(xiàn)異常的時(shí)候不會(huì)被判斷為邏輯錯(cuò)誤,認(rèn)為變量沒有賦值*/ try /*第一個(gè) try 塊實(shí)現(xiàn)了驅(qū)動(dòng)器的啟動(dòng) */ class.forname(driver); /look out th
5、is class should write in huge one!/*注意此處的 class需要大寫! */ catch(classnotfoundexception e) system.err.println(classnotfoundexception:+e.getmessage(); try /*第二個(gè) try 塊實(shí)現(xiàn)查詢功能 */ con=drivermanager.getconnection(url); /*剛剛定義的 url 在這里用上,此時(shí)程序才真正與數(shù)據(jù)庫相連*/ s=con.createstatement(); /*新建一個(gè) statement*/ resultset r=
6、s.executequery(query); /*query 在這里用上,執(zhí)行sql 語句,也就是按照我們的要求實(shí)現(xiàn)我們想完成的功能*/ system.out.println(name: + num: + course: + score:); 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 2 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 2 頁,共 14 頁 - - - - - - - - -while(r.next() /*逐行查詢, 只要有值就執(zhí)行
7、 while 里的語句 */ /*注意以下的每一項(xiàng)name,num等都要與數(shù)據(jù)庫中的題目想吻合*/ string r1=r.getstring(name); string r2=r.getstring(num); string r3=r.getstring(course); string r4=r.getstring(score); /*注意這些名字一定要與數(shù)據(jù)庫中的名字吻合!當(dāng)然也可以用數(shù)字代替,但是我的數(shù)據(jù)庫第一列是id,所以要注意 name是 2,num 是 3,以此類推 */ system.out.println(r1+ r2+ r3+ r4); catch(sqlexception
8、e) e.printstacktrace(); finally try s.close(); /*關(guān)閉 statement對(duì)象*/ con.close(); /*關(guān)閉連接 */ catch(sqlexception e) e.printstacktrace(); system.out.println(i am lagent); /*這條語句是一開始用于查找錯(cuò)誤使用的*/ 3.2.2運(yùn)行截圖 1 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 3 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - -
9、 - - - - - - - - 第 3 頁,共 14 頁 - - - - - - - - -3.3 數(shù)據(jù)庫信息查詢 .二3.3.1代碼import java.sql.*; import java.util.*; public class statement1 /* pay attention the name of class must not be the same with the public style ! the first time i named it statement so that it cant run successfully */* 英文是編程當(dāng)時(shí)留下的痕跡,當(dāng)時(shí)講
10、類名寫作statement ,與關(guān)鍵字相同, 因此出錯(cuò)不能正常運(yùn)行, 我的總結(jié)是在拿不準(zhǔn)的情況加一個(gè)數(shù)字就可以避免這樣的尷尬情況*/ public static void main(string arg) string driver=sun.jdbc.odbc.jdbcodbcdriver; string url=jdbc:odbc:students; string query=select * from students where score 90; /*整個(gè)程序與第一次編寫的沒有太大變動(dòng)和突破,主要是對(duì)sql 功能的了解和語句利用的熟練,當(dāng)時(shí)的想法是多寫多體會(huì),沒考慮創(chuàng)新,在此也就不對(duì)語
11、句做重復(fù)分析了*/ connection con=null; statement s=null; 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 4 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 4 頁,共 14 頁 - - - - - - - - -try class.forname(driver); catch(classnotfoundexception e) system.err.println(classnotfoundexception:+e
12、.getmessage(); try con=drivermanager.getconnection(url); s=con.createstatement(); resultset r=s.executequery(query); system.out.println(name:+ num:+ course:+ score); while(r.next() string r1=r.getstring(name); string r2=r.getstring(num); string r3=r.getstring(course); string r4=r.getstring(score); s
13、ystem.out.println(r1+r2+r3+r4); catch(sqlexception e) e.printstacktrace(); finally try 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 5 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 5 頁,共 14 頁 - - - - - - - - -s.close(); con.close(); catch(sqlexception e) e.printstacktrace()
14、; 3.3.2運(yùn)行截圖 2 3.4 數(shù)據(jù)庫信息查詢 .三3.4.1代碼import java.sql.*; import java.util.*; import java.io.*; public class preparedstatement1 public static void main(string arg) string driver=sun.jdbc.odbc.jdbcodbcdriver; string url=jdbc:odbc:students; 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 6 頁,共 14 頁 - - - -
15、- - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 6 頁,共 14 頁 - - - - - - - - -string query=select * from students where score ?; /*要實(shí)現(xiàn)按鍵盤輸入的參數(shù)來選擇查詢數(shù)據(jù)庫中的數(shù)據(jù),因此需要用“?”*/ connection con=null; preparedstatement ps=null; resultset r=null; try class.forname(driver); catch(classnotfoundexception e) syst
16、em.err.println(classnotfoundexception:+e.getmessage(); try system.out.println(您想要查詢分?jǐn)?shù)的下限是:); con=drivermanager.getconnection(url); bufferedreader br=new bufferedreader(new inputstreamreader(system.in); /*帶有緩沖區(qū),可以實(shí)現(xiàn)從鍵盤的輸入,從字符流中對(duì)文本*/ try int ms=integer.parseint(br.readline().trim(); /*將鍵盤輸入的參數(shù)賦值給ms*/
17、/this told me to pay a try and catch , so i put them in a try kuai ps=con.preparestatement(query); /*注意在這里就實(shí)現(xiàn)預(yù)編譯了,這里要注意prepare :小些打頭,不是prepared*/ ps.setint(1,ms); /*1 是付給第一個(gè)“?”,這里就只有 1 個(gè)*/ r=ps.executequery();/*因?yàn)橐呀?jīng)預(yù)編譯,這里不用在寫query*/ 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 7 頁,共 14 頁 - - - - -
18、- - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 7 頁,共 14 頁 - - - - - - - - -catch(exception e) system.out.println(e); system.out.println(name:+ num:+ course:+ score); while(r.next() string r1=r.getstring(name); string r2=r.getstring(num); string r3=r.getstring(course); string r4=r.getstring(sco
19、re); system.out.println(r1+r2+r3+r4); catch(sqlexception e) e.printstacktrace(); finally try ps.close(); con.close(); catch(sqlexception e) e.printstacktrace(); 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 8 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 8 頁,共 14 頁 - - - -
20、- - - - - 3.4.2運(yùn)行截圖 3 1.程序開始運(yùn)行的程序界面:2.輸入?yún)?shù)后的程序界面:3.5 數(shù)據(jù)庫信息的增添、刪除、修改3.5.1代碼import java.sql.*; public class all connection con=null; 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 9 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 9 頁,共 14 頁 - - - - - - - - -statement s=null; pre
21、paredstatement ps=null; string driver=sun.jdbc.odbc.jdbcodbcdriver; string url=jdbc:odbc:students; string query=select name,num,course,score from students; public void init() /*用 init() 將定義的對(duì)象和變量都初始化*/ try class.forname(driver); catch(classnotfoundexception e) system.err.println(classnotfoundexcepti
22、on:+e.getmessage(); try con=drivermanager.getconnection(url); s=con.createstatement(); catch(sqlexception e) e.printstacktrace(); public void output() /*先輸出數(shù)據(jù)庫的數(shù)據(jù) */ try resultset r=s.executequery(query); system.out.println(name: + num: + course: + score:); 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - -
23、- 第 10 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 10 頁,共 14 頁 - - - - - - - - -while(r.next() string r1=r.getstring(name); string r2=r.getstring(num); string r3=r.getstring(course); string r4=r.getstring(score); system.out.println(r1+ r2+ r3+ r4); catch(sqlexception e) e
24、.printstacktrace(); public void add() try system.out.println(增加語句之后: ); s.executeupdate(insert into students(name,num,course,score) values(三毛,20115407,ja va 高級(jí)應(yīng)用 ,59); /*執(zhí)行更新方法,注意values 后所賦的值對(duì) values 前的數(shù)據(jù)庫題目要一一對(duì)應(yīng),而且在所賦的值一定是用單引號(hào)包括*/ catch(exception e) e.printstacktrace(); public void change() try 精品學(xué)
25、習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 11 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 11 頁,共 14 頁 - - - - - - - - -system.out.println(改變分?jǐn)?shù)之后 ); ps=con.preparestatement(update students set score=0 where name=三毛);/* 將 name為三毛的一欄數(shù)據(jù)中的score改為 0*/ ps.executeupdate(); catch(
26、exception e) e.printstacktrace(); public void destroy() try s.close(); con.close(); ps.close(); catch(sqlexception e) e.printstacktrace(); public static void main(string arg) all a=new all(); try 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 12 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - -
27、 - - - - - - - 第 12 頁,共 14 頁 - - - - - - - - -a.init(); a.output(); a.add(); a.output(); a.change(); a.output(); /* 把需要實(shí)現(xiàn)的功能分開放在方法中,再在主函數(shù)中調(diào)用,邏輯清晰而且十分簡潔漂亮*/ catch(exception e) e.printstacktrace(); finally a.destroy(); 3.5.2運(yùn)行截圖 4 精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 13 頁,共 14 頁 - - - - - - - - -精品學(xué)習(xí)資料 可選擇p d f - - - - - - - - - - - - - - 第 13 頁,共 14 頁 - - - - - - - - -四
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 兒童教育合同范本
- 修鄉(xiāng)村路橋合同范本
- 偽造備案租房合同范本
- 創(chuàng)業(yè)五人合作合同范本
- 加盟 商鋪轉(zhuǎn)讓合同范本
- 全國租賃服裝合同范本
- 買賣店鋪裝修合同范本
- 劃經(jīng)營合同范本
- 2025內(nèi)蒙古新工創(chuàng)業(yè)發(fā)展集團(tuán)有限責(zé)任公司公開招聘工作人員筆試參考題庫附帶答案詳解
- epc項(xiàng)目建設(shè)合同范本
- 輪狀病毒性腸炎
- 世界社會(huì)主義五百年
- 加氫裂化操作工題庫(合并版)
- 正大集團(tuán)大豬場開發(fā)流程
- 高中政治必修四知識(shí)體系每單元的總體框架
- 房地產(chǎn)金融創(chuàng)新與風(fēng)險(xiǎn)防范的理論演進(jìn)
- GB/T 41255-2022智能工廠通用技術(shù)要求
- GB/T 41029-2021石油天然氣鉆井海洋棄井作業(yè)規(guī)程
- 深入推進(jìn)依法行政
- GB/T 4026-1992電器設(shè)備接線端子和特定導(dǎo)線線端的識(shí)別及應(yīng)用字母數(shù)字系統(tǒng)的通則
- 馬工程教材《公共財(cái)政概論》PPT-第二章 公共財(cái)政職能
評(píng)論
0/150
提交評(píng)論