1 / 23

JSP與JDBC建立網頁資料庫

JSP與JDBC建立網頁資料庫. JSP與JDBC建立網頁資料庫. 1 JDBC 的基礎 2 建立 MySQL 的資料庫連結 3 JSP 的資料庫基本存取. JDBC. 用 於 執行 SQL 的 Java API 處理程式與資料庫之間的 連結 具備 java 跨平台特性 ( 以 java 撰寫 ) 提供執行 SQL 指令. JDBC 的基礎 - 圖例. Java 應用程式. JDBC API. JDBC 把 driver manager 和資料庫 driver 分開. JDBC 驅動程式管理員 (JDBC Driver Manager).

tobit
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的基礎-圖例 Java應用程式 JDBCAPI JDBC把driver manager和資料庫driver分開 JDBC驅動程式管理員 (JDBCDriver Manager) JDBC驅動程式介面 (JDBCDriver Interface) JDBC驅動程式介面 (JDBCDriver Interface) 資料庫管理系統 DBMS 中介軟體(Middleware) 資料庫管理系統 DBMS

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

  6. My Sql & Workbench安裝 • 1.Google搜尋my sql,進入官網下載頁面 點擊畫面左方紅色方框處

  7. My Sql & Workbench安裝 • 2. 點擊Download下載

  8. My Sql & Workbench安裝 • 3.紅色方框處,上方為網路安裝,會在安裝時再下載檔案 下方為完整安裝檔,這邊我們選鑿完整安裝檔案

  9. My Sql & Workbench安裝 • 4.點擊畫面左下角,直接下載即可,不須註冊

  10. Workbench 執行Workbench後 可在畫面左上方看到MSQLConnections 按下+號,新增一個連結

  11. 1.輸入自訂的 connection名稱 3.確定 2.測試是否可成功連結

  12. 1.新增資料庫

  13. 1. 輸入自訂名稱 2. 選擇utf8 default collation 3.確認

  14. 針對指定的資料庫按右鍵 建立資料表

  15. PK : Primary Key [主鍵] NN: not null [非空]UQ: unique[唯一] BIN:Binary [二進制] UN: Usigned [整數]AI: auto increment [自動遞增]

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

  17. 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」資料夾

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

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

  20. 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);

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

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

  23. 連結範例程式 <%@ 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/manhoef?user=root&password=12345"; 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