數(shù)據(jù)庫程序設計.ppt_第1頁
數(shù)據(jù)庫程序設計.ppt_第2頁
數(shù)據(jù)庫程序設計.ppt_第3頁
數(shù)據(jù)庫程序設計.ppt_第4頁
數(shù)據(jù)庫程序設計.ppt_第5頁
已閱讀5頁,還剩24頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、數(shù)據(jù)庫程序設計,第十二章,目標,掌握JDBC的概念 掌握JDBC的使用 了解PreparedStatement接口 了解CallableStatement接口,JDBC 概念 3-1,JDBC 是一個緊湊、簡單的軟件層 JDBC API 定義應用程序如何: 打開連接 與數(shù)據(jù)庫通信 執(zhí)行 SQL 語句 檢索查詢結果,JDBC 概念 3-2,特性 不限制傳遞到底層 DBMS 驅動程序的查詢類型 JDBC 機制易于理解和使用 提供與 Java 系統(tǒng)的其他部分保持一致的 Java 接口 JDBC 可以在常見 SQL 層 API 的頂層實現(xiàn),JDBC 概念 3-3,SQL 一致性 JDBC 在類 Jav

2、a.SQL.Types 中定義了一組通用 SQL 類型標識符 JDBC 通過以下幾種方式處理 SQL 一致性問題: JDBC API 允許將任何查詢字符串傳遞到底層 DBMS 驅動程序 提供內置功能,便于將包含轉義序列的 SQL 查詢轉換為數(shù)據(jù)庫可理解的格式 提供 DatabaseMetaData 接口,允許用戶檢索關于所使用的 DBMS 信息,JDBC 驅動程序模型,JDBC 支持兩種模型: 二層模型 Java applet/應用程序直接與數(shù)據(jù)庫交互 被稱為客戶端/服務器配置 三層模型 使用中間層 可以在不同語言中實現(xiàn)中間層,JDBC 驅動程序,JDBC的三個組件 應用程序 驅動程序管理器

3、驅動程序 JDBC驅動程序的類型 JDBC-ODBC 橋加 ODBC 驅動程序 本地 API JDBC 網(wǎng)絡純 Java 驅動程序 本地協(xié)議純 Java 驅動程序,四類JDBC驅動程序,第一類:JDBC-ODBC橋 + ODBC驅動程序,JDBC 驅動程序,四類JDBC驅動程序(續(xù)),第二類:本地API,JDBC 驅動程序,四類JDBC驅動程序(續(xù)),第三類:JDBC網(wǎng)絡純Java驅動程序,JDBC 驅動程序,四類JDBC驅動程序(續(xù)),第四類:本地協(xié)議純Java驅動程序,JDBC 驅動程序,注冊和加載JDBC驅動程序,注冊 DriverManager.registerDriver(drive

4、r); 加載 Class.forName(sun.jdbc.odbc.JdbcOdbcDriver);,建立連接,JDBC連接由數(shù)據(jù)庫 URL標識 jdbc: 建立連接 Connection conn = DriverManager.getConnection(URL,login_name, login_password);,構造 SQL 語句,Statement 對象將 SQL 語句發(fā)送到 DBMS 創(chuàng)建Statement對象 Statement stmt = conn.createStatement();,提交查詢 3-1,Statement 對象的方法: executeUpdate()

5、用來創(chuàng)建和更新表 stmt.executeUpdate(query); executeQuery() 對于SELECT 語句 stmt.executeQuery(query);,提交查詢 3-2,返回ResultSet ResultSet rs stmt.executeQuery(query); execute() stmt.execute(); 返回布爾值,用于執(zhí)行任何 SQL 語句,提交查詢 3-3,DDL 命令包括create、alter 和 drop stat.executeUpdate(create table Customer (CustId number(3),CustName

6、varchar2(15),Address varchar2(30); DML 命令包括select、insert、update和delete RecordSet recset = stat.executeQuery(select * from customer);,顯示結果,ResultSet 對象包含 SQL 語句的執(zhí)行結果 使用 getXXX 方法檢索數(shù)據(jù) getInt() 用于檢索整型值 getString() 用于檢索字符串值,關閉Statement和Connection,關閉 Statement 對象 stmt.close(); 關閉 Connection 對象 conn.close

7、();,演示,import java.sql.*; import java.io.*; public class ProductDos public static void main(String args) try Connection conn; Statement stmt; BufferedReader br = new BufferedReader(new InputStreamReader(System.in); String input; int prodID, prodPrice, discount; String prodName; System.out.print(Prod

8、uct ID:); input = br.readLine(); prodID = new Integer(input).intValue(); System.out.print(Product Name:); prodName = br.readLine(); System.out.print(Product Price:); input = br.readLine(); prodPrice = new Integer(input).intValue(); System.out.print(Discount:); input = br.readLine(); discount = new I

9、nteger(input).intValue();,System.out.println(Connecting to the database.); Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); conn = DriverManager.getConnection(jdbc:odbc:oradsn, scott, tiger); System.out.println(Connected to the database.); stmt = conn.createStatement(); String query = insert into produc

10、t values( + prodID + , + prodName + , + prodPrice + , + discount + ); stmt.execute(query); System.out.println(Database Updated.); stmt.close(); conn.close(); catch (Exception e) ,import .URL; import java.sql.*; import oracle.jdbc.driver.*; Class.forName(oracle.jdbc.driver.OracleDriver); Connection c

11、on = DriverManager.getConnection(jdbc:oracle:oci8:oradb, scott, tiger); Statement stmt = conn.createStatement(); ResultSet r = stmt.executeQuery(SELECT a,b,c FROM Table1); While(r.next() int i = r.getInt(a); String s = r.getString(b); float f = r.getFloat(c); System.out.println(ROW=+i+ +s+ +f); r.cl

12、ose(); stmt.close(); con.close();,演示,PreparedStatement接口 2-1,要多次執(zhí)行一個SQL語句,使用PreparedStatement SQL 語句在創(chuàng)建時提供參數(shù) 在執(zhí)行 PreparedStatement 時,只傳遞參數(shù)值 通過不同的參數(shù)值多次調用 PreparedStatement pStmt = conn.preparedStatement( insert into emp (empno , ename) values(?,?);,PreparedStatement接口,PreparedStatement對象表示預編譯SQL語句。 由

13、Connection的prepareStatement ()方法創(chuàng)建 public PreparedStatement prepareStatement(Stringsql) throws SQLException 在生成PreparedStatement對象的字符串中用“?”代表一個可以產(chǎn)生變化的參數(shù),在隨后的程序中可以用循環(huán)語句生成這一系列的語句,從而方便此類SQL語句的生成。,常用的方法,可以將第parameterIndex個參數(shù)賦值為x。 public void setInt(intparameterIndex, intx) throws SQLException 隨后可以用Prepa

14、redStatement對象的 public void addBatch() throws SQLException 方法將其加入到一個批次作業(yè)。 最后用PreparedStatement對象的 public int executeBatch() throws SQLException 方法一次執(zhí)行所有加入的批次作業(yè)。 關閉PreparedStatement對象 public void close() throws SQLException,PreparedStatement接口 2-2,在創(chuàng)建 PreparedStatement 對象之后,必須為參數(shù)賦值 使用setXXX方法 用 execu

15、teUpdate() 方法執(zhí)行SQL語句,CallableStatement接口,CallableStatement對象為數(shù)據(jù)庫提供了一種以標準形式調用儲存過程的方法。 由Connection對象的prepareCall()方法創(chuàng)建 public CallableStatement prepareCall(Stringsql) throws SQLException,常用的方法,OUT參數(shù)要使用registerOutParameter()方法進行注冊登記 public void registerOutParameter(intparameterIndex, intsqlType) throws

16、 SQLException IN參數(shù)使用setXXX()方法設置實際值,設置字符串參數(shù)為: public void setString(int parameterIndex, String x) throws SQLException 使用CallableStatement對象的execute()方法執(zhí)行該存儲過程。 public boolean execute() throws SQLException OUT參數(shù)使用getXXX()方法取得返回值,取得字符串的方法為: public String getString(int parameterIndex) throws SQLException 關閉PreparedStatement對象 public void close() throws SQLException,CallableStatem

溫馨提示

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

評論

0/150

提交評論