




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
實驗九通過JDBC方式操作數(shù)據(jù)庫1.實驗目的〔1〕掌握通過JDBC方式操作數(shù)據(jù)庫的根本步驟。〔2〕掌握增、刪、改記錄的方法?!?〕掌握查詢記錄以及遍歷查詢結(jié)果的方法。2.實驗內(nèi)容實驗題1學生信息管理函數(shù)。數(shù)據(jù)庫中的信息參考Exp9.1.txt,這些命令用來創(chuàng)立student表,包含學生的學號、姓名、年齡信息。①根據(jù)學號,可以查詢到學生的姓名和年齡;②給定學生的學號、姓名、年齡,在表中追加一行信息;③給定學生的學號,可以從表中刪除該學生的信息;[根本要求]對上面的每一個功能編寫相應的函數(shù),并測試。運行結(jié)果:源代碼:import;import;import;import;import;import;import;classQuery{ Scannerreader=newScanner(System.in); Connectioncon; sql; publicQuery(){ try{ Class.forName(""); } catch(ClassNotFoundExceptione){ (e); } } publicvoidselect(){ StringSNO; ("請輸入要查詢的學號:"); SNO=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("select*fromstudentwhereSno=?"); sql.setString(1,SNO); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(SQLExceptione){ (e); } } publicvoidinsert(){ StringSNO,SNAME,AGE; ("請輸入要插入的信息:"); SNO=reader.next(); SNAME=reader.next(); AGE=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("insertintostudentvalues(?,?,?)"); sql.setString(1,SNO); sql.setString(2,SNAME); sql.setString(3,AGE); intm=sql.executeUpdate(); if(m!=0){ ("添加成功!運行結(jié)果:"); } else("添加失??!運行結(jié)果:"); /*查詢插入后結(jié)果*/ sql=con.prepareStatement("select*fromstudent"); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(Exceptione){ (e); } } publicvoiddelete(){ StringSNO; ("請輸入要刪除信息的學號:"); SNO=reader.next(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ (e); } try{ sql=con.prepareStatement("deletefromstudentwhereSno=?"); sql.setString(1,SNO); intm=sql.executeUpdate(); if(m!=0){ ("刪除成功!運行結(jié)果:"); } else("刪除失敗!運行結(jié)果:"); /*查詢刪除后結(jié)果*/ sql=con.prepareStatement("select*fromstudent"); RS=sql.executeQuery(); while(RS.next()){ System.out.println(RS.getString(1)+'\t'+RS.getString(2)+'\t'+RS.getString(3)); } con.close(); }catch(SQLExceptione){ (e); } } }publicclassExperiment_9_JDBC{ publicstaticvoidmain(finalString[]args){ Queryquery=newQuery(); query.select();//查詢 query.insert();//插入 query.delete();//刪除 }}實驗題2學生信息管理系統(tǒng)。 學號:學號:姓名:年齡:查詢追加刪除狀態(tài):圖3.10學生信息管理系統(tǒng)界面①點擊“追加”按鈕,可以追加一個學生信息;②點擊“查詢”按鈕和“刪除”按鈕,可以按照學號查詢或者刪除一個學生信息;[技術提示]可以使用上一題開發(fā)的函數(shù)。運行結(jié)果:添加記錄成功后,再次查詢,結(jié)果如下:然后按學號刪除:檢驗是否刪除成功:可知是刪除成功的。代碼參考:packageJDBC_Experiment;import;import;import;import;import;import;import;import;import;import;import;import;import;import;classQuery{ Scannerreader=newScanner(System.in); Connectioncon; sql; publicQuery(){ try{ Class.forName(""); } catch(ClassNotFoundExceptione){ System.out.println(e); } } publicvoidselect(Texttext1,Texttext2,Texttext3,Texttext4){ text2.setText(""); text3.setText(""); text4.setText(""); text4.setText("請輸入要查詢的學號"); StringSNO=text1.getText(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("select*fromstudentwhereSno=?"); sql.setString(1,SNO); RS=sql.executeQuery(); if(RS.next()){ text2.setText(RS.getString(2)); text3.setText(RS.getString(3)); text4.setText("查詢成功!"); } else{ text4.setText("查無此人!"); } con.close(); }catch(SQLExceptione){ System.out.println(e); } } publicvoidinsert(Texttext1,Texttext2,Texttext3,Texttext4){ text4.setText(""); StringSNO,SNAME,AGE; text4.setText("請輸入要插入的信息"); SNO=text1.getText(); SNAME=text2.getText(); AGE=text3.getText(); StringURL="jdbc:mysql://localhost:3306/mysql?Unicode=true&characterEncoding=GBK"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("insertintostudentvalues(?,?,?)"); sql.setString(1,SNO); sql.setString(2,SNAME); sql.setString(3,AGE); intm=sql.executeUpdate(); if(m!=0){ text4.setText("添加成功!"); } elsetext4.setText("添加失敗!"); con.close(); }catch(Exceptione){ System.out.println(e); } } publicvoiddelete(Texttext1,Texttext2,Texttext3,Texttext4){ text2.setText(""); text3.setText(""); text4.setText(""); StringSNO; text4.setText("請輸入要刪除信息的學號:"); SNO=text1.getText(); StringURL="jdbc:mysql://localhost:3306/mysql"; Stringuser="root"; Stringpassword=""; ResultSetRS; try{ con=DriverManager.getConnection(URL,user,password); }catch(SQLExceptione){ System.out.println(e); } try{ sql=con.prepareStatement("deletefromstudentwhereSno=?"); sql.setString(1,SNO); intm=sql.executeUpdate(); if(m!=0){ text4.setText("刪除成功!"); } elsetext4.setText("刪除失??!"); con.close(); }catch(SQLExceptione){ System.out.println(e); } } }publicclassJDBC_UI{ protectedShellshell; privateTexttext; privateTexttext_1; privateTexttext_2; privateTexttext_3; /** *Launchtheapplication. *@paramargs */ publicstaticvoidmain(String[]args){ try{ JDBC_UIwindow=newJDBC_UI(); window.open(); }catch(Exceptione){ e.printStackTrace(); } } /** *Openthewindow. */ publicvoidopen(){ Displaydisplay=Display.getDefault(); createContents(); shell.open(); shell.layout(); while(!shell.isDisposed()){ if(!display.readAndDispatch()){ display.sleep(); } } } /** *Createcontentsofthewindow. */ protectedvoidcreateContents(){ finalQueryquery=newQuery(); shell=newShell(); shell.setSize(450,300); shell.setText("\u5B66\u751F\u4FE1\u606F\u7BA1\u7406\u7CFB\u7EDF"); LabellblNewLabel=newLabel(shell,SWT.NONE); lblNewLabel.setFont(SWTResourceManager.getFont("微軟雅黑",12,SWT.NORMAL)); lblNewLabel.setBounds(26,42,48,21); lblNewLabel.setText("\u5B66\u53F7\uFF1A"); LabellblNewLabel_1=newLabel(shell,SWT.NONE); lblNewLabel_1.setFont(SWTResourceManager.getFont("微軟雅黑",12,SWT.NORMAL)); lblNewLabel_1.setBounds(26,78,48,21); lblNewLabel_1.setText("\u59D3\u540D\uFF1A"); LabellblNewLabel_2=newLabel(shell,SWT.NONE); lblNewLabel_2.setFont(SWTResourceManager.getFont("微軟雅黑",12,SWT.NORMAL)); lblNewLabel_2.setBounds(26,116,48,21); lblNewLabel_2.setText("\u5E74\u9F84\uFF1A"); LabellblNewLabel_3=newLabel(shell,SWT.NONE); lblNewLabel_3.setFont(SWTResourceManager.getFont("微軟雅黑",12,SWT.NORMAL)); lblNewLabel_3.setBounds(26,179,48,21); lblNewLabel_3.setText("\u72B6\u6001\uFF1A"); text=newText(shell,SWT.BORDER); text.setBounds(80,42,104,23); text_1=newText(shell,SWT.BORDER); text_1.setBounds(80,78,104,23); text_2=newText(shell,SWT.BORDER); text_2.setBounds(80,114,104,23); text_3=newText(shell,SWT.BORDER); text_3.setEditable(false); text_3.setBounds(80,177,274,23); Buttonbutton=newButton(shell,
溫馨提示
- 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZJBS 001-2024 城市公共標識系統(tǒng)設計規(guī)范
- T-ZSA 230-2024 虛擬數(shù)字人多模態(tài)交互信息規(guī)范
- 二零二五年度荒山土地流轉(zhuǎn)與林業(yè)碳匯項目合作合同
- 二零二五年度企業(yè)合規(guī)管理體系法律服務合同范本
- 二零二五年度離婚協(xié)議書模板及婚姻關系終止及財產(chǎn)分配
- 二零二五年度診所醫(yī)療事故處理及責任劃分合同
- 二零二五年度養(yǎng)老護理型住宅正規(guī)房屋出租合同
- 二零二五年度混凝土班組勞務合作施工安全責任合同
- 二零二五年度銷售信息保密管理承諾書
- 2025年度耕地租賃與農(nóng)業(yè)科技推廣合同
- 兒童文學概論(譚旭東第二版) 課件全套 第1-5章 兒童文學的基本內(nèi)涵- 兒童文學的各種文體
- 學習新思想做好接班人演講稿(5篇)
- 【甲醇液相催化法生產(chǎn)一氯甲烷的工藝設計13000字(論文)】
- DB32T3916-2020建筑地基基礎檢測規(guī)程
- 高壓電工證題庫電工培訓試題及答案
- 部編人教版六年級語文下冊全冊單元教材分析
- 新生兒單純皰疹病毒性腦炎的臨床診治要點詳解
- 5草船借箭 課件
- 鋅離子電池原理和應用教學教案
- 2023廣東省深圳市各區(qū)中考數(shù)學模擬題-壓軸題
- 社區(qū)衛(wèi)生服務站設置基本標準
評論
0/150
提交評論