1 / 16

JSP與JDBC建立網頁資料庫

JSP與JDBC建立網頁資料庫. JSP與JDBC建立網頁資料庫. 1 JDBC 的基礎 2 建立 MySQL 的資料庫連結 3 JSP 的資料庫基本存取. JDBC. 用 於 執行 SQL 的 Java API 處理程式與資料庫之間的 連結 具備 java 跨平台特性 ( 以 java 撰寫 ) 提供執行 SQL 指令. JDBC 的基礎 - 圖例. JDBC 把 driver manager 和資料庫 driver 分開. 事前準備 - 相關軟體. 資料庫部份 需安裝 MySQL 資料庫軟體 安裝資料庫編輯視窗介面. 安裝步驟.

chinue
Télécharger la présentation

JSP與JDBC建立網頁資料庫

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP與JDBC建立網頁資料庫

  2. JSP與JDBC建立網頁資料庫 • 1 JDBC的基礎 • 2 建立MySQL的資料庫連結 • 3 JSP的資料庫基本存取

  3. JDBC • 用於執行SQL的Java API • 處理程式與資料庫之間的連結 • 具備java跨平台特性(以java撰寫) • 提供執行SQL指令

  4. JDBC的基礎-圖例 JDBC把driver manager和資料庫driver分開

  5. 事前準備-相關軟體 • 資料庫部份 • 需安裝MySQL資料庫軟體 • 安裝資料庫編輯視窗介面

  6. 安裝步驟 • STEP1 (MySQL資料庫) • 先安裝 mysql-4.1.7-win • 安裝過程需要設定密碼時請設為: • STEP2 (資料庫控制介面) • 再安裝 mysqlcc-0.9.4-win32 • STEP3 (資料庫驅動程式) • 將 mysql-connector-java-3.0.16-ga-bin.jar 直接複製到下列資料夾 • C:\apache-tomcat-7.0.37\lib

  7. 資料庫儲存位置 • MySQL資料庫儲存位置 • C:\Program Files\MySQL\MySQL Server 4.1\data

  8. Example

  9. 建立MySQL的資料庫連結 • 2-1 安裝MySQL的JDBC驅動程式 • 2-2 使用JDBC連結MySQL資料庫

  10. 2-1 安裝MySQL的JDBC驅動程式 • MySQL資料庫系統支援JDBC的Java原生通訊協定驅動程式,稱為MySQL Connector/J • 可以在MySQL網站免費下載,目前版本是5.1.18版 • 在JSP的Web應用程式安裝MySQL Connector/J,其步驟如下所示: • 1. 使用解壓縮工具從壓縮檔取出JAR檔案:mysql-connector-java-3.0.16-ga-bin.jar。 • 2. 將JAR檔案複製Web應用程式的「WEB-INF\lib」資料夾

  11. 2-2 使用JDBC連結MySQL資料庫-說明 • 在安裝好MySQL的JDBC驅動程式後,JSP程式就可以使用JDBC建立資料庫連結,然後透過JDBC API執行SQL指令來存取資料庫的記錄資料。 • 四步驟: • 步驟1: JSP程式首先需要載入JDBC驅動程式 • 步驟2: 建立Connection連結物件 • 步驟3: 建立JDBC的Statement物件 • 步驟4: 關閉連結的Connection和Statement物件

  12. 2-2-1使用JDBC連結MySQL資料庫-步驟一 • 步驟一:載入驅動程式 • 在JSP程式首先需要載入JDBC驅動程式,如下所示: String sDriver = "com.mysql.jdbc.Driver"; Class.forName(sDriver); • 上述程式碼 • 字串sDriver是驅動程式名稱com.mysql.jdbc.Driver • 接著使用Class類別方法forName()方法載入驅動程式。

  13. 2-2-2 使用JDBC連結MySQL資料庫-步驟二 • 步驟二:建立Connection連結物件 • 在載入JDBC驅動程式後,就可以使用DriverManager類別的getConnection()類別方法建立Connection物件dbCon,如下所示: String sCon; Connection dbCon = null; sCon = "jdbc:mysql://localhost:3306/school?user=root&password=0000"; dbCon=DriverManager.getConnection(sCon);

  14. 2-2-3 使用JDBC連結MySQL資料庫-步驟三 • 步驟三:建立JDBC的Statement物件 • Statement物件的目的是執行SQL指令 • 在建立好Connection物件後,就可以使用createStatement()方法建立Statement物件 • 如下所示: stmt = dbCon.createStatement();

  15. 2-2-4 使用JDBC連結MySQL資料庫-步驟四 • 步驟四:關閉連結的Connection和Statement物件 • 在處理完資料庫的查詢或操作後,JSP程式需要關閉Connection和Statement物件,使用的都是close()方法,如下所示: stmt.close(); dbCon.close();

  16. <!– JSP程式:Ch3_5.jsp  <%@ page contentType=“text/html;charset=big5” import=“java.sql.*” %> <html> <body> <h2>建立JDBC資料庫連結</h2><hr> <% Connection dbCon = null; // 宣告物件變數 Statement stmt = null; // 驅動程式參數 String sDriver = “com.mysql.jdbc.Driver”; String sCon = “jdbc:mysql://localhost:3306/school?user=root&password=root”; Try { Class.forName(sDriver); // 載入 JDBC driver dbCon = DriverManager.getConnection(sCon); // 建立資料連結 if ( dbCon != null ) out.print(“建立資料來源連結成功!<br>”); stmt = dbCon.createStatement(); // 建立Statement物件 if ( stmt != null ) out.print(“建立Statement物件成功!<br>”); stmt.close(); // 關閉Statement物件 dbCon.close(); // 關閉Connection連結物件 }catch(SQLException e) { out.print(e); } %> </body> </html>

More Related