JSP中使用數(shù)據(jù)庫_第1頁
JSP中使用數(shù)據(jù)庫_第2頁
JSP中使用數(shù)據(jù)庫_第3頁
JSP中使用數(shù)據(jù)庫_第4頁
JSP中使用數(shù)據(jù)庫_第5頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上實(shí)驗(yàn)報(bào)告實(shí)驗(yàn)名稱:JSP中使用數(shù)據(jù)庫實(shí)驗(yàn)?zāi)康模?、 掌握使用JDBC查詢數(shù)據(jù)庫中表的記錄2、 掌握使用JDBC更新數(shù)據(jù)庫中表的記錄3、 掌握使用JDBC刪除數(shù)據(jù)庫中表的記錄實(shí)驗(yàn)環(huán)境:Windows平臺(tái),tomcat7,jdk,Microsoft Access 2003實(shí)驗(yàn)內(nèi)容:使用Microsoft Access 2003創(chuàng)建一個(gè)數(shù)據(jù)庫Book,在數(shù)據(jù)庫中創(chuàng)建表bookForm。1、 編寫一個(gè)JSP頁面inputMess.jsp和一個(gè)Tag文件FindBook.tag。2、 編寫一個(gè)JSP頁面updateRecord.jsp和兩個(gè)Tag文件UpdateRecord.t

2、ag、GetRecord.tag。updateRecord.jsp負(fù)責(zé)調(diào)用GetRecord.tag文件,顯示GetRecord.tag返回待更新的記錄的有關(guān)字段的值,updateRecord.jsp調(diào)用UpdateRecord.tag文件,并顯示UpdateRecord.tag返回的有關(guān)更新記錄是否成功的信息。3、 編寫一個(gè)JSP頁面inputDeletedISBN.jsp和一個(gè)Tag文件DelRecord.tag。實(shí)驗(yàn)結(jié)果: 見附頁小結(jié):本次試驗(yàn)中學(xué)習(xí)了如何連接數(shù)據(jù)庫,如何查詢、更新、刪除信息等重要知識(shí)。評(píng)定成績(jī): 批閱教師: 年 月 日實(shí)驗(yàn)前準(zhǔn)備使用Microsoft Access 20

3、03創(chuàng)建數(shù)據(jù)庫Book,然后在數(shù)據(jù)庫中再創(chuàng)建名為bookForm的表,設(shè)置表的字段和屬性,如下圖所示:在表bookForm中按格式要求輸入內(nèi)容,如下圖所示:使用JDBC-ODBC橋接器方式和數(shù)據(jù)庫建立連接,將數(shù)據(jù)庫Book設(shè)置為名字為information的數(shù)據(jù)源,如下圖所示:實(shí)驗(yàn)一:編寫一個(gè)JSP頁面inputMess.jsp和一個(gè)Tag文件FindBook.tag,使用JDBC查詢數(shù)據(jù)庫中表的記錄,結(jié)果如下:inputMess.jsp的代碼如下所示:<% page contentType="text/html;charset=GB2312" %><%

4、taglib tagdir="/WEB-INF/tags" prefix="findBook"%><HTML><Body bgcolor=cyan><center> <form action=""> 輸入查詢內(nèi)容:<Input type=text name="findContent" value="JSP"> <Select name="condition" size=1> <Option S

5、elected value="bookISBN">ISBN <Option value="bookName">書名 <Option value="bookAuthor">作者 <Option value="bookPublish">出版社 <Option value="bookTime">出版時(shí)間 <Option value="bookAbstract">內(nèi)容摘要 </Select> <Br&

6、gt; <INPUT type="radio" name="findMethod" value="start">前方一致 <INPUT type="radio" name="findMethod" value="end">后方一致 <INPUT type="radio" name="findMethod" value="contains">包含 <Input type=sub

7、mit value="提交"> </form><% String findContent = request.getParameter("findContent"); String condition = request.getParameter("condition"); String findMethod = request.getParameter("findMethod"); if(findContent=null) findContent="" if(condi

8、tion=null) condition="" if(findMethod=null) findMethod="" %> <BR>查詢到的圖書: <findBook:FindBook dataSource="information" tableName="bookForm" findContent="<%=findContent%>" condition="<%=condition%>" findMethod="<

9、;%=findMethod%>"/> <BR><%=giveResult%> </form></BODY></HTML>FindBook.tag的代碼如下所示:<% tag import="java.sql.*" %><% tag pageEncoding="gb2312" %><% attribute name="dataSource" required="true" %><% attrib

10、ute name="tableName" required="true" %><% attribute name="findContent" required="true" %><% attribute name="condition" required="true" %><% attribute name="findMethod" required="true" %><% variabl

11、e name-given="giveResult" variable-class="java.lang.StringBuffer" scope="AT_END" %><% byte b=findContent.getBytes("iso-8859-1"); findContent=new String(b); try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); catch(ClassNotFoundException e) out.p

12、rint(e); Connection con; Statement sql; ResultSet rs; StringBuffer queryResult=new StringBuffer(); /查詢結(jié)果 String uri="jdbc:odbc:"+dataSource; try con=DriverManager.getConnection(uri,"",""); sql=con.createStatement(); String s="" if(findMethod.equals("start

13、") s= "select * from "+tableName+" where "+condition+" Like'"+findContent+"%'" if(findMethod.equals("end") s= "select * from "+tableName+" where "+condition+" Like'%"+findContent+"'" if(fin

14、dMethod.equals("contains") s= "select * from "+tableName+" where "+condition+" Like'%"+findContent+"%'" rs=sql.executeQuery(s); queryResult.append("<table border=1>"); queryResult.append("<tr>"); queryResult.ap

15、pend("<th>ISBN</td>"); queryResult.append("<th>圖書名稱</td>"); queryResult.append("<th>作者</td>"); queryResult.append("<th>價(jià)格</td>"); queryResult.append("<th>出版社</td>"); queryResult.append("

16、;<th>出版時(shí)間</td>"); queryResult.append("<th>摘要</td>"); queryResult.append("</tr>"); int 字段個(gè)數(shù)=7; while(rs.next() queryResult.append("<tr>"); String bookISBN="" for(int k=1;k<=字段個(gè)數(shù);k+) if(k=7) String bookAbstract=rs.getS

17、tring(k); String abs="<textarea rows=6 colums=10/>"+bookAbstract+"</textarea>" queryResult.append("<td>"+abs+"</td>"); else queryResult.append("<td>"+rs.getString(k)+"</td>"); queryResult.append("<

18、;/table>"); jspContext.setAttribute("giveResult",queryResult); con.close(); catch(SQLException exp) jspContext.setAttribute("giveResult",new StringBuffer("請(qǐng)給出查詢條件"); %>實(shí)驗(yàn)結(jié)果如下所示:實(shí)驗(yàn)二:編寫一個(gè)JSP頁面updateRecord.jsp和兩個(gè)Tag文件UpdateRecord.tag、GetRecord.tag,使用JDBC更新數(shù)據(jù)庫中表的

19、記錄,結(jié)果如下:updateRecord.jsp的代碼如下所示:<<% page contentType="text/html;charset=GB2312" %><% taglib tagdir="/WEB-INF/tags" prefix="getRecord" %><% taglib tagdir="/WEB-INF/tags" prefix="updateRecord" %><HTML><BODY bgcolor=pink>

20、;<CENTER> <FORM action="" name=form> 輸入圖書的ISBN號(hào),然后更新該圖書的有關(guān)信息: <Input type=text name="bookISBN" /> <Input type=submit name="ok" value="提交"/></FORM><% String bookISBN=request.getParameter("bookISBN"); if(bookISBN!=null)

21、%> <getRecord:GetRecord bookISBN="<%=bookISBN%>" /> 輸入<%=bookISBN%>的新信息>: <Font size=2> <FORM action="" name=form> <table> <tr><td>圖書名稱:</td> <td><Input type=text name="bookName" value="<%=oldB

22、ookName%>"></td></tr> <tr><td>圖書作者:</td> <td><Input type=text name="bookAuthor" value="<%=oldBookAuthor%>"></td></tr> <tr><td>圖書價(jià)格: </td><td><Input type=text name="bookPrice&quo

23、t; value="<%=oldBookPrice%>"></td></tr> <tr><td>出版社: </td><td><Input type=text name="bookPublish" value="<%=oldBookPublish%>"></td></tr> <tr><td>出版社時(shí)間(yy-mm-dd):</td> <td><In

24、put type=text name="bookTime" value="<%=oldBookTime%>"></td></tr> <tr><td>圖書摘要:</td> <td><TextArea name="bookAbstract" rows=6,cols=12><%=oldBookAbstract%></textarea></td></tr> <input type=hidd

25、en name="bookISBN" value="<%=bookISBN %>" > <tr><td><Input type=submit name="enter" value="提交"></td> </tr> </table><Font></CENTER> </form><% %><% bookISBN=request.getParameter("bookIS

26、BN"); String enter=request.getParameter("enter"); String bookName=request.getParameter("bookName"); String bookAuthor=request.getParameter("bookAuthor"); String bookPrice=request.getParameter("bookPrice"); String bookPublish=request.getParameter("boo

27、kPublish"); String bookTime=request.getParameter("bookTime"); String bookAbstract=request.getParameter("bookAbstract"); boolean ok=(enter!=null); if(ok)%> <updateRecord:UpdateRecord bookISBN="<%=bookISBN%>" bookName="<%=bookName%>" bookA

28、uthor="<%=bookAuthor%>" bookPrice="<%=bookPrice%>" bookPublish="<%=bookPublish%>" bookTime="<%=bookTime%>" bookAbstract="<%=bookAbstract%>"/> <center><h2><%= giveResult %></h2></center> &

29、lt;% %> </BODY></HTML>UpdateRecord.tag的代碼如下:<% tag import="java.sql.*" %><% tag pageEncoding="gb2312" %><% attribute name="bookISBN" required="true" %><% attribute name="bookName" required="true" %><

30、;% attribute name="bookAuthor" required="true" %><% attribute name="bookPrice" required="true" %><% attribute name="bookPublish" required="true" %><% attribute name="bookTime" required="true" %><%

31、 attribute name="bookAbstract" required="true" %><% variable name-given="giveResult" variable-class= "java.lang.StringBuffer" scope="AT_END" %><% byte c=bookName.getBytes("iso-8859-1"); bookName=new String(c); c=bookAuthor.getBy

32、tes("iso-8859-1"); bookAuthor=new String(c); c=bookPublish.getBytes("iso-8859-1"); bookPublish=new String(c); c=bookAbstract.getBytes("iso-8859-1"); bookAbstract=new String(c); try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); catch(ClassNotFoundException e)

33、out.print(e); StringBuffer str=new StringBuffer(); Connection con; Statement sql; ResultSet rs; String uri="jdbc:odbc:information" try con=DriverManager.getConnection(uri,"",""); String updateCondition="UPDATE bookForm SET bookName='"+ bookName+"'

34、,bookAuthor='"+bookAuthor+"',bookPrice= "+ bookPrice+",bookPublish='"+bookPublish+ "',bookTime='"+bookTime+"',bookAbstract='"+bookAbstract+ "' WHERE bookISBN='"+bookISBN+"'" sql=con.createStateme

35、nt(); int m=sql.executeUpdate(updateCondition); if(m=1) str.append("修改信息成功"); else str.append("更新失敗"); con.close(); catch(SQLException exp) str.append("更新失敗"+exp); jspContext.setAttribute("giveResult",str);%>GetRecord.tag的代碼如下所示:<% tag import="java.

36、sql.*" %><% tag pageEncoding="gb2312" %><% attribute name="bookISBN" required="true" %><% variable name-given="oldBookName" scope="AT_END" %><% variable name-given="oldBookAuthor" scope="AT_END" %>&

37、lt;% variable name-given="oldBookPrice" scope="AT_END" %><% variable name-given="oldBookPublish" scope="AT_END" %><% variable name-given="oldBookTime" scope="AT_END" %><% variable name-given="oldBookAbstract" sco

38、pe="AT_END" %><% try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); catch(ClassNotFoundException e) out.print(e); StringBuffer str=new StringBuffer(); Connection con; Statement sql; ResultSet rs; String uri="jdbc:odbc:information" try con=DriverManager.getConnecti

39、on(uri,"",""); String query= "select bookName,bookAuthor,bookPrice"+ ",bookPublish,bookTime,bookAbstract from bookForm WHERE bookISBN='"+bookISBN+"'" sql=con.createStatement(); rs=sql.executeQuery(query); if(rs.next() jspContext.setAttribute(

40、"oldBookName",rs.getString("bookName"); jspContext.setAttribute("oldBookAuthor",rs.getString("bookAuthor"); jspContext.setAttribute("oldBookPrice",rs.getString("bookPrice"); jspContext.setAttribute("oldBookPublish",rs.getString(&q

41、uot;bookPublish"); jspContext.setAttribute("oldBookTime",rs.getString("bookTime"); jspContext.setAttribute("oldBookAbstract",rs.getString("bookAbstract"); else jspContext.setAttribute("oldBookName",""); jspContext.setAttribute("ol

42、dBookAuthor",""); jspContext.setAttribute("oldBookPrice",""); jspContext.setAttribute("oldBookPublish",""); jspContext.setAttribute("oldBookTime",""); jspContext.setAttribute("oldBookAbstract",""); con.clo

43、se(); catch(SQLException exp) jspContext.setAttribute("oldBookName",""); jspContext.setAttribute("oldBookAuthor",""); jspContext.setAttribute("oldBookPrice",""); jspContext.setAttribute("oldBookPublish",""); jspContext.s

44、etAttribute("oldBookTime",""); jspContext.setAttribute("oldBookAbstract",""); jspContext.setAttribute("bookISBN",bookISBN);%> 實(shí)驗(yàn)結(jié)果如下所示:修改信息,如下所示:重新查詢一下,可以發(fā)現(xiàn)信息已更新:實(shí)驗(yàn)三:編寫一個(gè)JSP頁面inputDeletedISBN.jsp和一個(gè)Tag文件DelRecord.tag,使用JDBC刪除數(shù)據(jù)庫中表的記錄,結(jié)果如下:inputDeletedISBN.jsp的代碼如下所示:<% page contentType="text/html;charset=GB2312" %><% taglib tagdir="/WEB-INF/tags" prefix="deleteRecord"%><HTML><BODY><FONT size=2><

溫馨提示

  • 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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論