清華大學張思民Java課件第10章.ppt_第1頁
清華大學張思民Java課件第10章.ppt_第2頁
清華大學張思民Java課件第10章.ppt_第3頁
清華大學張思民Java課件第10章.ppt_第4頁
清華大學張思民Java課件第10章.ppt_第5頁
已閱讀5頁,還剩30頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

Java語言程序設(shè)計,第10章 Java數(shù)據(jù)庫連接,主講:張思民,仰恩大學Java課程組,圖15.1 學生數(shù)據(jù)庫的組成及相關(guān)名詞,10.1 建立ODBC數(shù)據(jù)源,理解ODBC數(shù)據(jù)源,圖15.3 ODBC數(shù)據(jù)源管理器對話框,圖15.7 安裝完成后的“ODBC數(shù)據(jù)源管理器”對話框,10.2 使用JDBC連接數(shù)據(jù)庫,JDBC(Java DataBase Connectivity的縮寫),意思是Java程序連接數(shù)據(jù)庫的應(yīng)用程序接口(API)。 JDBC由一群類和接口組成,通過調(diào)用這些類和接口所提供的成員方法,我們可以連接各種不同的數(shù)據(jù)庫,進而使用標準的SQL命令對數(shù)據(jù)庫進行查詢、插入、刪除、更新等操作。,10.2.1 JDBC結(jié)構(gòu),JDBC的基本結(jié)構(gòu)由Java程序、JDBC管理器、驅(qū)動程序和數(shù)據(jù)庫四部分組成,如圖所示。,圖 JDBC結(jié)構(gòu),數(shù)據(jù)庫,ODBC,JDBC,橋接器,Java 應(yīng)用程序,1Java應(yīng)用程序 Java應(yīng)用程序根據(jù)JDBC方法實現(xiàn)對數(shù)據(jù)庫的訪問和操作。完成的主要任務(wù)有:請求與數(shù)據(jù)庫建立連接;向數(shù)據(jù)庫發(fā)送SQL請求;查詢結(jié)果;處理錯誤;控制傳輸、提交及關(guān)閉連接等操作。,2、 JDBC編程要點 (1)引用java.sql包: import java.sql.*; (2) 使用Class.forName( )方法加載相應(yīng)數(shù)據(jù)庫的JDBC驅(qū)動程序: class.forName(“sun.jdbc.odbc.JdbcOdbcDriver“);,(3) 定義JDBC的URL對象。例如: String conURL=“jdbc:odbc:TestDB“; 其中TestDB是我們設(shè)置的數(shù)據(jù)源。 (4) 連接數(shù)據(jù)庫。 Connection s=DriverManager.getConnection(conURL);,(5) 使用SQL語句對數(shù)據(jù)庫進行操作。 (6) 解除Java與數(shù)據(jù)庫的連接并關(guān)閉數(shù)據(jù)庫。例如: s.close( );,10.3 JDBC編程實例,10.3.1 創(chuàng)建數(shù)據(jù)表 【示例程序1】 創(chuàng)建學生表student。此表有三個字段:學號(id)、姓名(name)及成績(score)。 import java.sql.*; /引入java.sql包 public class c1 public static void main(String args) String JDriver = “sun.jdbc.odbc.JdbcOdbcDriver“; /聲明JDBC驅(qū)動程序?qū)ο?String conURL=“jdbc:odbc:TestDB“; /定義JDBC的URL對象 try Class.forName(JDriver); /加載JDBC-ODBC橋驅(qū)動程序 ,catch(java.lang.ClassNotFoundException e) System.out.println(“ForName :“ + e.getMessage( ); try Connection con=DriverManager.getConnection(conURL); /連接數(shù)據(jù)庫URL Statement s=con.createStatement( ); /建立Statement類對象 String query = “create table student ( “ + “id char(10),“ + “name char(15),“ + “score integer“ + “)“; /創(chuàng)建一個含有三個字段的學生表student s.executeUpdate(query); /執(zhí)行SQL命令,s.close( ); /釋放Statement所連接的數(shù)據(jù)庫及JDBC資源 con.close( ); /關(guān)閉與數(shù)據(jù)庫的連線 catch(SQLException e) System.out.println(“SQLException: “ +e.getMessage( ); ,其中, create table student(id char(10),name char(15),score integer); 這個SQL語句表示建立一個名為student的表,包含id(字符型,寬度為10)、name(字符型,寬度為15)與 score(數(shù)字型)三個字段。 這段程序的操作結(jié)果是創(chuàng)建了一個數(shù)據(jù)庫中student表的結(jié)構(gòu),表中還沒有任何記錄。,10.3.2 向數(shù)據(jù)表中插入數(shù)據(jù) 【示例程序2】 在上例創(chuàng)建的數(shù)據(jù)表student中插入三個學生的記錄。 import java.sql.*; public class c2 public static void main(String args) String JDriver = “sun.jdbc.odbc.JdbcOdbcDriver“; String conURL=“jdbc:odbc:TestDB“; try Class.forName(JDriver); ,catch(java.lang.ClassNotFoundException e) System.out.println(“ForName :“ + e.getMessage( ); try Connection con=DriverManager.getConnection(conURL); Statement s=con.createStatement( ); String r1=“insert into student values(“+“0001,王明,80)“; String r2=“insert into student values(“+“0002,高強,94)“; String r3=“insert into student values(“+“0003,李莉,82)“; /使用SQL 命令insert插入三條學生記錄到表中 s.executeUpdate(r1);,s.executeUpdate(r2); s.executeUpdate(r3); s.close( ); con.close( ); catch(SQLException e) System.out.println(“SQLException: “ +e.getMessage( ); ,圖 程序2的運行結(jié)果,10.3.3 更新數(shù)據(jù) 【示例程序3.java】 修改上例數(shù)據(jù)表中的第二條和第三條記錄的學生成績字段值,并把修改后的數(shù)據(jù)表的內(nèi)容輸出到屏幕上。 import java.sql.*; public class c3 public static void main(String args) String JDriver=“sun.jdbc.odbc.JdbcOdbcDriver“; String conURL=“jdbc:odbc:TestDB“; String id=“0002“,“0003“; int score=89,60;,try Class.forName(JDriver); catch(java.lang.ClassNotFoundException e) System.out.println(“ForName :“ + e.getMessage( ); try Connection con=DriverManager.getConnection(conURL); /修改數(shù)據(jù)庫中數(shù)據(jù)表的內(nèi)容 PreparedStatement ps=con.prepareStatement( “UPDATE student set score=? where id=? “); int i=0,idlen=id.length;,do ps.setInt(1,scorei); ps.setString(2,idi); ps.executeUpdate( );/執(zhí)行SQL修改命令 +i; while(iid.length); ps.close( ); /查詢數(shù)據(jù)庫并把數(shù)據(jù)表的內(nèi)容輸出到屏幕上 Statement s=con.createStatement( ); ResultSet rs=s.executeQuery(“select * from student“); while(rs.next( ) System.out.println(rs.getString(“id“) + “t“ + rs.getString(“name“)+ “t“ + rs.getInt(“score“);, s.close( ); con.close( ); catch(SQLException e) System.out.println(“SQLException: “ +e.getMessage( ); ,在這個程序中使用了PreparedStatement類,它提供了一系列的set方法來設(shè)定位置。請注意程序中PreparedStatement( )方法中的參數(shù)“?”。程序中的語句: PreparedStatement ps=con.prepareStatement(“UPDATE student set score=? where id=? “); ps.setInt(1,scorei); /將scorei的值作為SQL語句中第一個問號所代表參數(shù)的值 ps.executeUpdate( );,其中“UPDATE student set score=? where id=? “這個SQL語句中各字段的值并沒指定,而是以“?”表示。程序必須在執(zhí)行ps.executeUpdate( )語句之前指定各個問號位置的字段值。例如,用ps.setInt(1,scorei)語句中的參數(shù)1指出這里的scorei的值是SQL語句中第一個問號位置的值。當前面兩條語句執(zhí)行完后,才可執(zhí)行ps.executeUpdate( )語句,完成對一條記錄的修改。,程序中用到的查詢數(shù)據(jù)庫并把數(shù)據(jù)表的內(nèi)容輸出到屏幕的語句是: ResultSet rs=s.executeQuery(“select * from student“); while(rs.next( ) System.out.println(rs.getString(“id“) + “t“ + rs.getString(“name“)+ “t“ + rs.getInt(“score“); ,其中,executeQuery( )返回一個ResultSet類的對象rs,代表執(zhí)行SQL查詢語句后所得到的結(jié)果集,之后再在while循環(huán)中使用對象rs的next( )方法將返回的結(jié)果一條一條地取出,直到next( )為false。 運行結(jié)果如下: 0001 王明 80 0002 高強 89 0003 李莉 60,10.3.4 刪除記錄 【示例程序4.java】 刪除表中第二條記錄,然后把數(shù)據(jù)表的內(nèi)容輸出。 import java.sql.*; public class c4 public static void main(String args) String JDriver=“sun.jdbc.odbc.JdbcOdbcDriver“; String conURL=“jdbc:odbc:TestDB“; try Class.forName(JDriver); ,catch(java.lang.ClassNotFoundException e) System.out.println(“ForName :“ + e.getMessage( ); try Connection con=DriverManager.getConnection(conURL); Statement s=con.createStatement( ); /刪除第二條記錄 PreparedStatement ps=con.prepareStatement( “delete from student where id=?“); ps.setString(1,“0002“); ps.executeUpdate( ); /執(zhí)行刪除 /查詢數(shù)據(jù)庫并把數(shù)據(jù)表的內(nèi)容輸出到屏幕上,ResultSet rs=s.executeQuery(“select * from student“); while(rs.next( ) System.out.println(rs.getString(“id“)+“t“+ rs.getString(“name“)+“t“ + rs.getString(“score“); s.close( ); con.close( ); catch(SQLException e) System.out.println(“SQLException: “ +e.getMessage( ); ,數(shù)據(jù)庫驅(qū)動程序連接數(shù)據(jù)庫,1、下載數(shù)據(jù)庫驅(qū)動程序 2、連接URL,連接Access數(shù)據(jù)庫,String conURL=“jdbc:odbc:Driver=MicroSoft Access Driver (*.mdb); “ +“DBQ=TestDB.mdb“; Connection Ex1Con=DriverManager.getConnection(conURL); 【見P116 例10-5】,連接SQL Server數(shù)據(jù)庫,【見P325 例10-8】,連接 Oracle 數(shù)據(jù)庫,Class.forName(“oracle.jdbc.Orac

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論