![實(shí)驗(yàn)8-jdbc數(shù)據(jù)庫訪問[驕陽教育]_第1頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/1/7d12bc4f-238c-4aac-a687-6eea0ce6395c/7d12bc4f-238c-4aac-a687-6eea0ce6395c1.gif)
![實(shí)驗(yàn)8-jdbc數(shù)據(jù)庫訪問[驕陽教育]_第2頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/1/7d12bc4f-238c-4aac-a687-6eea0ce6395c/7d12bc4f-238c-4aac-a687-6eea0ce6395c2.gif)
![實(shí)驗(yàn)8-jdbc數(shù)據(jù)庫訪問[驕陽教育]_第3頁](http://file1.renrendoc.com/fileroot_temp2/2021-2/1/7d12bc4f-238c-4aac-a687-6eea0ce6395c/7d12bc4f-238c-4aac-a687-6eea0ce6395c3.gif)
下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、實(shí)驗(yàn)8 JDBC訪問數(shù)據(jù)庫一、實(shí)驗(yàn)?zāi)康?. 掌握使用傳統(tǒng)的方法訪問數(shù)據(jù)庫;2. 掌握使用數(shù)據(jù)源的方法訪問數(shù)據(jù)庫。二、實(shí)驗(yàn)原理數(shù)據(jù)庫應(yīng)用是Web應(yīng)用開發(fā)的一個(gè)重要應(yīng)用。Web應(yīng)用程序訪問數(shù)據(jù)庫有兩種方法:傳統(tǒng)的方法和使用JNDI數(shù)據(jù)源的方法。傳統(tǒng)方法訪問數(shù)據(jù)庫的步驟是:加載數(shù)據(jù)庫驅(qū)動(dòng)程序;建立連接對(duì)象;創(chuàng)建語句對(duì)象;獲得結(jié)果集;關(guān)閉有關(guān)連接對(duì)象。使用數(shù)據(jù)源訪問數(shù)據(jù)庫的步驟是:配置數(shù)據(jù)源(局部數(shù)據(jù)源或全局?jǐn)?shù)據(jù)源);通過JNDI機(jī)制查找命名數(shù)據(jù)源;通過數(shù)據(jù)源對(duì)象創(chuàng)建連接對(duì)象;其他與傳統(tǒng)方法一致。三、實(shí)驗(yàn)內(nèi)容與步驟(一)使用傳統(tǒng)方法通過JSP頁面訪問數(shù)據(jù)庫【步驟1】創(chuàng)建數(shù)據(jù)庫。假設(shè)在PostgreSQ
2、L建立了一個(gè)名為bookstore的數(shù)據(jù)庫,在其中建立books表,代碼如下:CREATE TABLE books ( bookid character(5) PRIMARY KEY, -書號(hào) title varchar2(80), -書名author character varying(20), -作者 publisher character varying (40), -出版社 price real -價(jià)格);向books表中插入幾條記錄,代碼如下:INSERT INTO books VALUES (204,Head First Servlets & JSP, Bryan Basham,
3、中國電力出版社,98.00);INSERT INTO books VALUES (201, Servlets 與JSP 核心教程, Hall Marty,清華大學(xué)出版社,45);INSERT INTO books VALUES (202, Tomcat與Java Web 開發(fā)技術(shù)祥解, 孫衛(wèi)琴, 機(jī)械工業(yè)出版社,45);INSERT INTO books VALUES (203, JSP 應(yīng)用開發(fā)技術(shù), 柳永坡,人民郵電出版社,52);INSERT INTO books VALUES (205, J2EE 1.4 編程指南, Spielman Sue,電子工業(yè)出版社,68);注意:需要將數(shù)據(jù)庫
4、的JDBC驅(qū)動(dòng)程序安裝到應(yīng)用程序的WEB-INFlib目錄中?!静襟E2】使用下面JSP頁面displayBooks.jsp訪問books表中的數(shù)據(jù)。 Database Access Test%try Class.forName(org.postgresql.Driver);String dburl = jdbc:postgresql:/localhost:5432/bookstore;Connection conn = DriverManager.getConnection(dburl, bookstore, bookstore); Statement stmt = conn.createSt
5、atement();String sql = SELECT * FROM books ;ResultSet rs = stmt.executeQuery(sql);out.println();out.println(書號(hào)書名作者價(jià)格);while (rs.next()out.println(+ rs.getString(1)+ rs.getString(2)+ rs.getString(3)+ rs.getString(5)+);out.println();rs.close();stmt.close();conn.close();catch (Exception e) out.println(
6、e.getMessage();%運(yùn)用mysql的代碼如下:Database Access Test%try Class.forName(com.mysql.jdbc.Driver);String dburl = jdbc:mysql:/localhost:3306/bookstore;Connection conn = DriverManager.getConnection(dburl, root, );Statement stmt = conn.createStatement();String sql = SELECT * FROM books;ResultSet rs = stmt.exe
7、cuteQuery(sql);out.println();out.println(書號(hào)書名作者價(jià)格);while (rs.next() out.println( + rs.getString(1) + + rs.getString(2) + + rs.getString(3)+ + rs.getString(5) + );out.println();rs.close();stmt.close();conn.close(); catch (Exception e) out.println(e.getMessage();% 圖1 displayBooks.jsp(二)通過數(shù)據(jù)源訪問數(shù)據(jù)庫注意:需要
8、將數(shù)據(jù)庫的JDBC驅(qū)動(dòng)程序安裝到Tomcat安裝目錄的lib目錄中,并重新啟動(dòng)Tomcat服務(wù)器。【步驟1】建立局部數(shù)據(jù)源在Web應(yīng)用程序中建立一個(gè)META-INF目錄,在其中建立一個(gè)context.xml文件,內(nèi)容如下:【步驟2】使用下面的JSP頁面displayBooks.jsp訪問數(shù)據(jù)庫 DataSource Test%try Context context = new InitialContext();DataSource ds = (DataSource) context.lookup(java:comp/env/jdbc/bookDS);Connection conn = ds.g
9、etConnection();Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery(SELECT * FROM books);out.println();out.println(書號(hào)書名作者價(jià)格);while (rs.next()out.println(+ rs.getString(1)+ rs.getString(2)+ rs.getString(3)+ rs.getString(5)+);out.println();rs.close();stmt.close();conn.close();catch
10、 (Exception e) out.println(e.getMessage();%圖2 displayBooks.jsp(三)綜合應(yīng)用。本實(shí)驗(yàn)采用MVC設(shè)計(jì)模式,通過數(shù)據(jù)源和DAO對(duì)象訪問數(shù)據(jù)庫。其中JavaBeans實(shí)現(xiàn)模型,訪問數(shù)據(jù)庫,Servlet實(shí)現(xiàn)控制器,JSP頁面實(shí)現(xiàn)視圖。 模型包括2個(gè)JavaBean:BookBean用于存放圖書信息,BookDAO用于訪問數(shù)據(jù)庫。 控制器包括2個(gè)Servlet:BookQueryServlet根據(jù)請(qǐng)求參數(shù)查詢圖書信息、BookInsertServlet用來向數(shù)據(jù)庫中插入數(shù)據(jù)。 視圖包括4個(gè)JSP頁面:bookQuery.jsp顯示查詢頁面、
11、bookInsert.jsp顯示插入頁面、display.jsp顯示查詢結(jié)果頁面和errorPage.jsp顯示錯(cuò)誤頁面。【步驟1】存放圖書信息的JavaBeans代碼BookBean.java,它也是一個(gè)傳輸對(duì)象。package com.beans;import java.io.*;public class BookBean implements Serializable private String bookid = null; private String title = null; private String author = null; private String publish
12、er = null; private float price = 0.0F; public BookBean() public BookBean(String bookId, String author, String title, String publisher, float price) this.bookid = bookId; this.title = title; this.author = author; this.publisher = publisher;this.price = price; public String getBookid() return this.boo
13、kid; public String getTitle() return title; public String getAuthor() return this.author; public float getPrice() return price; public String getPublisher () return publisher; public void setBookid(String bookid) this.bookid=bookid; public void setTitle(String title)this.title=title; public void set
14、Author(String author) this. author = author; public void setPrice(float price)this.price=price; public void setPublisher (String publisher) this.publisher = publisher;【步驟2】下面的BookDAO是一個(gè)簡(jiǎn)單的JavaBeans,它實(shí)現(xiàn)數(shù)據(jù)庫的訪問。package com.beans;import java.sql.*;import javax.sql.*;import javax.naming.*;import java.uti
15、l.ArrayList;import com.beans.BookBean;public class BookDAO private static InitialContext context= null; private DataSource dataSource = null; public BookDAO() try if(context = null) context = new InitialContext(); dataSource = (DataSource)context.lookup(java:comp/env/jdbc/bookDS); catch(NamingExcept
16、ion e2) / 根據(jù)書號(hào)查詢圖書信息 public BookBean searchBook(String bookid) Connection conn = null; PreparedStatement pstmt = null; ResultSet rst = null; BookBean book = new BookBean(); try conn = dataSource.getConnection(); pstmt = conn.prepareStatement(SELECT * FROM books WHERE bookid=?); pstmt.setString(1,boo
17、kid); rst = pstmt.executeQuery(); if(rst.next() book.setBookid(rst.getString(bookid); book.setTitle(rst.getString(title); book.setAuthor(rst.getString(author); book.setPublisher(rst.getString(publisher); book.setPrice(rst.getFloat(price); return book; else return null; catch(SQLException se) return
18、null; finally try conn.close(); catch(SQLException se) / 插入一本圖書記錄 public boolean insertBook(BookBean book) Connection conn = null; PreparedStatement pstmt = null; try conn = dataSource.getConnection(); pstmt = conn.prepareStatement( INSERT INTO books VALUES(?,?,?,?,?); pstmt.setString(1,book.getBook
19、id(); pstmt.setString(2,book.getTitle(); pstmt.setString(3,book.getAuthor(); pstmt.setString(4,book.getPublisher(); pstmt.setFloat(3,book.getPrice(); pstmt.executeUpdate(); pstmt.close(); return true; catch(SQLException se) return false; finally try conn.close(); catch(SQLException se) 【步驟3】下面的JSP頁面
20、bookQuery.jsp實(shí)現(xiàn)根據(jù)書號(hào)查詢圖書信息 Book Query請(qǐng)輸入一個(gè)書號(hào):圖3 bookQuery.jsp【步驟4】下面的JSP頁面bookInsert.jsp實(shí)現(xiàn)向數(shù)據(jù)庫中插入數(shù)據(jù) Book Insert請(qǐng)輸入圖書信息: 書號(hào) 書名 作者 出版社 單價(jià) 圖4 bookInsert.jsp 圖5 插入成功【步驟5】顯示查詢結(jié)果的JSP頁面display.jsp: 書號(hào): 書名: 作者: 出版社: 價(jià)格:圖6 顯示查詢結(jié)果【步驟6】錯(cuò)誤頁面errorPage.jsp代碼如下: 對(duì)不起,您查的圖書不存在!圖7 顯示錯(cuò)誤頁面【步驟7】下面的Servlet實(shí)現(xiàn)從請(qǐng)求參數(shù)獲得書號(hào),然后從數(shù)
21、據(jù)庫中查找該書,最后根據(jù)查詢結(jié)果將請(qǐng)求轉(zhuǎn)發(fā)到顯示頁面(display.jsp)或錯(cuò)誤頁面(errorPage.jsp)。package com.control;import java.io.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;import com.beans.BookBean;import com.beans.BookDAO;public class BookQueryServlet extends HttpServlet public void doPost(HttpServletReq
22、uest request,HttpServletResponse response) throws ServletException,IOException String bookid = request.getParameter(bookid); BookDAO bookdao = new BookDAO(); BookBean book = bookdao.searchBook(bookid); if(book!=null) request.getSession().setAttribute(book, book); RequestDispatcher view = request.get
23、RequestDispatcher(/display.jsp); view.forward(request, response); else RequestDispatcher view = request.getRequestDispatcher(/errorPage.jsp); view.forward(request, response); 【步驟8】下面的Servlet實(shí)現(xiàn)向數(shù)據(jù)庫插入數(shù)據(jù),并將控制請(qǐng)求的轉(zhuǎn)發(fā)到bookInsert.jsp頁面。package com.control;import java.io.*;import java.sql.*;import javax.serv
24、let.*;import javax.servlet.http.*;import com.beans.BookBean;import com.beans.BookDAO;public class BookInsertServlet extends HttpServlet public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException request.setCharacterEncoding(gb2312); String message = null; BookBean book = new BookBean( request.getParameter(bookid),request.getParameter(title), request.getParameter(author),request.getParameter(publisher), Float.parseFloat(request.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 提高響應(yīng)速度與客戶滿意度計(jì)劃
- 探秘小宇宙幼兒園教學(xué)工作計(jì)劃文檔
- 寓教于樂的品牌推廣方法計(jì)劃
- 2025年獨(dú)立運(yùn)行風(fēng)力發(fā)電機(jī)組控制器及逆變器項(xiàng)目合作計(jì)劃書
- 會(huì)議紀(jì)要與決策執(zhí)行要點(diǎn)梳理
- 2025年體外診斷儀器產(chǎn)品項(xiàng)目合作計(jì)劃書
- 辦公室日常行為規(guī)范及規(guī)章制度解讀
- Zinc-sulfide-生命科學(xué)試劑-MCE
- racemic-Dunnione-SL-11010-生命科學(xué)試劑-MCE
- 新員工入職流程與職責(zé)說明
- 提升辦公室工作效能的經(jīng)驗(yàn)交流發(fā)言模板
- 人教版鄂教版二年級(jí)下冊(cè)科學(xué)教案(全)
- 男孩的青春期性教育
- 胃癌影像診斷課件
- 建筑工程勞務(wù)作業(yè)服務(wù)方案
- 探究水垢的主要成份
- (完整版)小學(xué)生心理健康教育課件
- 軍隊(duì)文職專用簡(jiǎn)歷(2023年)
- 建筑裝飾工程施工總平面布置圖
- 鐵路基本建設(shè)工程設(shè)計(jì)概(預(yù))算編制辦法-國鐵科法(2017)30號(hào)
- 顏真卿《勸學(xué)》ppt課件1
評(píng)論
0/150
提交評(píng)論