Web數(shù)據(jù)庫學生實驗報告JDBC部分_第1頁
Web數(shù)據(jù)庫學生實驗報告JDBC部分_第2頁
Web數(shù)據(jù)庫學生實驗報告JDBC部分_第3頁
Web數(shù)據(jù)庫學生實驗報告JDBC部分_第4頁
Web數(shù)據(jù)庫學生實驗報告JDBC部分_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、Web數(shù)據(jù)庫技術學生實驗報告 院 系: 信息科學與技術學院專 業(yè): 信息管理與信息系統(tǒng)班 級: 信A1321/22任課教師: 張海 實驗報告(一)院系:信息學院 課程名稱:Web數(shù)據(jù)庫技術 日期:班 級A1321姓 名黃偉峰專 業(yè)信息管理與信息系統(tǒng)學 號19實 驗 室607實驗名稱Jdbc應用成 績 評 定教 師 簽 名實驗目的1、 掌握數(shù)據(jù)庫驅(qū)動的加載方式2、 掌握connection對象的使用方法3、 掌握statement對象使用方法4、 掌握事務的處理機制5、掌握數(shù)據(jù)持久層的設計實驗內(nèi)容connection對象、statement對象等應用1、 請設計一個工程類通過配置文件如下db.p

2、roperties來獲得數(shù)據(jù)庫連接的相關信息,并通過該配置文件獲得數(shù)據(jù)庫連接對象。url=jdbc:mysql://u5B66u751Fu5E93userName=adminpwd=adminpublic class connectionFactory public static Connection getConnection() throws SQLException請把getConnection()方法補全。要求設計合理規(guī)范,必須有截圖。答案:public class connectionFactory private static Properties pros =

3、new Properties() ;private static String driver ;private static String url ;private static String userName ;private static String pwd ;static InputStream is = connectionFactory.class.getClassLoader().getResourceAsStream("perties") ;try pros.load(is) ;driver = pros.getProperty("dr

4、iver") ;url = pros.getProperty("url") ;user = pros.getProperty("userName") ;pwd = pros.getProperty("pwd") ;Class.forName(driver) ; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();public static Connection getConnection() throws SQLExceptionC

5、onnection conn = null ;try conn = DriverManager.getConnection(url, user, pwd) ;conn.setAutoCommit(false) ; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();return conn ;2、 已知學生定義如下:public class student private int id; private String stuId; private String name; private String

6、domCard;/樓棟宿舍號“31-507”private String bedNo;/床鋪號public student(String stuId, String name, String domCard, String bedNo) super();Id = stuId; = name;this.domCard = domCard;this.bedNo = bedNo;public String toString()return "id="+id+"學號="+stuId+”;姓名=”+name+”;宿舍號=”+domCard+”;床

7、鋪號=”+bedNo;/相應get、set方法省略有一class studentDataspublic static ArrayList<student> students=new ArrayList<student>();staticstudent stu=new student("會計A001121","張三","31棟908",1); students.add(stu); stu=new student("會計A001166","李四","31棟908&q

8、uot;,2); students.add(stu); stu=new student("會計A001177","王五","31棟807",4); students.add(stu); 現(xiàn)要求 (1) 根據(jù)student類建立一個學生表用來保存student類的相關屬性。(2) 通過jdbc,將studentDatas的students集合中的所有學生保持到學生表中; public class StuAdd /* * param args * throws SQLException */public static void main(

9、String args) throws SQLException studentDatas stus = new studentDatas() ;Connection conn = connectionFactory.getConnection() ;PreparedStatement prStatement = null ;try for(student stu:stus.students)/增加String sql="insert into lianxi(學號,姓名,宿舍號,床鋪號) values(?,?,?,?)"prStatement = conn.prepareS

10、tatement(sql) ;prStatement.setString(1, stu.getStuId() ;prStatement.setString(2, stu.getName() ;prStatement.setString(3, stu.getDomCard() ;dNo() ;prStatement.executeUpdate(); conn mit() ; catch (Exception e) try conn.rollback() ; catch (SQLException e1) / TODO Auto-generated catch blocke1.printStack

11、Trace();/ TODO Auto-generated catch blocke.printStackTrace();finallyif (prStatement != null) try prStatement.cancel() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if (conn != null) try conn.close() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStac

12、kTrace();class studentDataspublic static ArrayList<student> students=new ArrayList<student>();static student stu=new student("會計A001121","張三","31棟908","1"); students.add(stu); stu=new student("會計A001166","李四","31棟908",

13、"2"); students.add(stu); stu=new student("會計A001177","王五","31棟807","4"); students.add(stu); (3) 通過jdbc,將學生表中所有的宿舍是” 31棟908”學生全部調(diào)整到“20棟371”宿舍;public class StuAlter /* * param args * throws SQLException */public static void main(String args) throws SQL

14、Exception / TODO Auto-generated method stubConnection conn = connectionFactory.getConnection() ;PreparedStatement prStatement = null ;try /修改String sql = "update lianxi set 宿舍號 =? where 宿舍號=?" ;prStatement = conn.prepareStatement(sql) ;prStatement.setString(1, "20棟371") ;prStatem

15、ent.setString(2, "31棟908") ;prStatement.executeUpdate() ;conn mit() ; catch (Exception e) try conn.rollback() ; catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();/ TODO Auto-generated catch blocke.printStackTrace();finallyif (prStatement != null) try prStatement

16、.cancel() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if (conn != null) try conn.close() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();(4)通過jdbc,刪除"31棟807"床鋪號是4的學生。public class StuDeletion /* * param args * throws SQLExcepti

17、on */public static void main(String args) throws SQLException / TODO Auto-generated method stubList<student> students=new ArrayList<student>();Connection conn = connectionFactory.getConnection() ;ResultSet rs = null ;PreparedStatement prStatement = null ;try /刪除String sql = "delete

18、from lianxi where 宿舍號='31棟807' and 床鋪號='4'" ;prStatement=conn.prepareStatement(sql);prStatement.executeUpdate();conn mit() ; catch (Exception e) try conn.rollback() ; catch (SQLException e1) / TODO Auto-generated catch blocke1.printStackTrace();/ TODO Auto-generated catch blocke

19、.printStackTrace();finallyif (prStatement != null) try prStatement.cancel() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();if (conn != null) try conn.close() ; catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();3、 ResultSet對象操作請將上題中宿舍號最后一位是”8”的

20、學生信息全部顯示出來。public class StuQuery /* * param args * throws SQLException */public static void main(String args) throws SQLException / TODO Auto-generated method stubList<student> students=new ArrayList<student>();Connection conn = connectionFactory.getConnection() ;ResultSet rs = null ;Pre

21、paredStatement prStatement = null ;student stu = null ;try String sql = "select lianxi.* from lianxi where 宿舍號 like '%8'" ;prStatement = conn.prepareStatement(sql) ;rs = prStatement.executeQuery(sql) ;while (rs.next() stu=new student(sql, sql, sql, sql);stu.setId(rs.getInt("id");stu.setStuId(rs.getString("學號");stu.setName(rs.getString("姓名");stu.setDomCard(rs.getString("宿舍號");stu.setBedNo(rs.getString("床鋪號");students.add(stu);System.out

溫馨提示

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

評論

0/150

提交評論