JSP第6章实验报告.docx

上传人:b****7 文档编号:11297630 上传时间:2023-02-26 格式:DOCX 页数:17 大小:20.26KB
下载 相关 举报
JSP第6章实验报告.docx_第1页
第1页 / 共17页
JSP第6章实验报告.docx_第2页
第2页 / 共17页
JSP第6章实验报告.docx_第3页
第3页 / 共17页
JSP第6章实验报告.docx_第4页
第4页 / 共17页
JSP第6章实验报告.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

JSP第6章实验报告.docx

《JSP第6章实验报告.docx》由会员分享,可在线阅读,更多相关《JSP第6章实验报告.docx(17页珍藏版)》请在冰豆网上搜索。

JSP第6章实验报告.docx

JSP第6章实验报告

肇庆学院计算机学院

JSP中使用数据库(实验)实验报告

实验名称

JSP中使用数据库(实验)

班级

姓名

学号

实验地点

日期

成绩

一、实验目的:

实验1的主要目的是让学生掌握使用JDBC查询数据库中表的记录。

实验2的目的是让学生掌握使用JDBC更新数据库中表的记录。

实验3的目的是掌握掌握使用JDBC删除数据库中表的记录。

二、实验环境:

WindowsXP操作系统的计算机,apache-tomcat-6.0.29(还要安装好jdk),Dreamweaver或者eclipse。

三、实验内容、步骤、实验结果与分析:

见附页

5、实验总结:

熟练掌握了怎么使用在JSP中使用数据库

实验1查询记录

编写一个JSP页面inputMess.jsp。

编写一个Tag文件FindBook.tag。

1)inputMess.jsp的具体要求

inputMess.jsp页面提供表单。

表单允许用户输入要查询的内容,以及选择针对该内容的查询条件,比如,输入“实用”,那么可以指定该内容是书名的一部分或作者姓名等。

inputMess.jsp页面查询条件提交给当前页面,当前页面调用FineBook.tag文件完成查询操作。

2)FindBook.tag的具体要求

FindBook.tag文件使用attribute指令获得inputMess.jsp页面传递过来的字段的查询条件,然后与数据源information建立连接、根据得到的查询条件查询bookForm表。

FindBook.tag文件使用variable指令将查询结果返回给inputMess.jsp页面。

JSP页面效果示例

inputMess.jsp的效果如图所示。

 

参考代码

可以按照实验要求,参考本代码编写代码。

1)JSP页面参考代码

inputMess.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%@taglibtagdir="/WEB-INF/tags"prefix="findBook"%>

输入查询内容:

ISBN

书名

作者

出版社

出版时间

内容摘要


前方一致

后方一致

包含

<%

StringfindContent=request.getParameter("findContent");

Stringcondition=request.getParameter("condition");

StringfindMethod=request.getParameter("findMethod");

if(findContent==null){

findContent="";

}

if(condition==null){

condition="";

}

if(findMethod==null){

findMethod="";

}

%>


查询到的图书:

FindBookdataSource="information"

tableName="bookForm"

findContent="<%=findContent%>"

condition="<%=condition%>"

findMethod="<%=findMethod%>"/>


<%=giveResult%>

2)Tag文件参考代码

FindBook.tag

<%@tagimport="java.sql.*"%>

<%@tagpageEncoding="gb2312"%>

<%@attributename="dataSource"required="true"%>

<%@attributename="tableName"required="true"%>

<%@attributename="findContent"required="true"%>

<%@attributename="condition"required="true"%>

<%@attributename="findMethod"required="true"%>

<%@variablename-given="giveResult"variable-class="java.lang.StringBuffer"scope="AT_END"%>

<%

byteb[]=findContent.getBytes("iso-8859-1");

findContent=newString(b);

try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(ClassNotFoundExceptione){

out.print(e);

}

Connectioncon;

Statementsql;

ResultSetrs;

StringBufferqueryResult=newStringBuffer();//查询结果

Stringuri="jdbc:

odbc:

"+dataSource;

try{con=DriverManager.getConnection(uri,"","");

sql=con.createStatement();

Strings="";

if(findMethod.equals("start"))

s="select*from"+tableName+"where"+condition+"Like'"+findContent+"%'";

if(findMethod.equals("end"))

s="select*from"+tableName+"where"+condition+"Like'%"+findContent+"'";

if(findMethod.equals("contains"))

s="select*from"+tableName+"where"+condition+"Like'%"+findContent+"%'";

rs=sql.executeQuery(s);

queryResult.append("");

queryResult.append("");

queryResult.append("ISBN");

queryResult.append("图书名称");

queryResult.append("作者");

queryResult.append("价格");

queryResult.append("出版社");

queryResult.append("出版时间");

queryResult.append("摘要");

queryResult.append("");

int字段个数=7;

while(rs.next()){

queryResult.append("");

StringbookISBN="";

for(intk=1;k<=字段个数;k++){

if(k==7){

StringbookAbstract=rs.getString(k);

Stringabs=""+bookAbstract+"";

queryResult.append(""+abs+"");

}

else{

queryResult.append(""+rs.getString(k)+"");

}

}

}

queryResult.append("");

jspContext.setAttribute("giveResult",queryResult);

con.close();

}

catch(SQLExceptionexp){

jspContext.setAttribute("giveResult",newStringBuffer("请给出查询条件"));

}

%>

实验2更新记录

实验要求

编写一个JSP页面updsteRecord.jsp和两个Tag文件UpdateRecord.tag、GetRecord.tag。

updateRecord.jsp负责调用GetRecord.tag文件,显示GetRecord.tag返回待更新的记录的有关字段的值;upateRecord.jsp调用UpdateRecord.tag文件,并显示UpdateRecord.tag返回的有关更新记录是否成功的信息。

1)updateRecord.jsp的具体要求

updateRecord.jsp页面提供一个表单,该表单允许用户输入某种图书的ISBN号以及针对该ISBN号的图书的更新信息;该页面负责将图书的ISBN号传递给GeRecord.tag,将图书的更新信息传递给UpdateRecord.tag。

2)GetRecord.tag的具体要求

GetRecord.tag文件使用attribute指令获得updateRecord.jsp页面传递过来的图书的ISBN号,并显示和该图书对应的记录。

3)UpdateRecord.tag具体要求

UpdateRecord.tag文件使用attribute指令获得updateRecord.jsp页面传递过来的图书更新信息,然后更新数据库中bookForm表中的记录,并返回更新是否成功的消息给updateRecord.jsp页面。

JSP页面效果示例

updateRecord.jsp的效果如图所示。

 

参考代码

可以按照实验要求,参考本代码编写代码。

1)JSP页面参考代码

updateRecord.jsp

<%@pagecontentType="text/html;charset=GB2312"%>

<%@taglibtagdir="/WEB-INF/tags"prefix="getRecord"%>

<%@taglibtagdir="/WEB-INF/tags"prefix="updateRecord"%>

输入图书的ISBN号,然后更新该图书的有关信息:

<%

StringbookISBN=request.getParameter("bookISBN");

if(bookISBN!

=null){

%>

GetRecordbookISBN="<%=bookISBN%>"/>

输入<%=bookISBN%>的新信息>:

">

图书名称:

">
图书作者:

">
图书价格:

">
出版社:

">
出版社时间(yy-mm-dd):

">
图书摘要:

<%=oldBookAbstract%>

<%}

%>

<%

bookISBN=request.getParameter("bookISBN");

Stringenter=request.getParameter("enter");

StringbookName=request.getParameter("bookName");

StringbookAuthor=request.getParameter("bookAuthor");

StringbookPrice=request.getParameter("bookPrice");

StringbookPublish=request.getParameter("bookPublish");

StringbookTime=request.getParameter("bookTime");

StringbookAbstract=request.getParameter("bookAbstract");

booleanok=(enter!

=null);

if(ok){

%>

UpdateRecordbookISBN="<%=bookISBN%>"bookName="<%=bookName%>"bookAuthor="<%=bookAuthor%>"

bookPrice="<%=bookPrice%>"bookPublish="<%=bookPublish%>"

bookTime="<%=bookTime%>"bookAbstract="<%=bookAbstract%>"/>

<%=giveResult%>

<%}

%>

2)Tag文件参考代码

GetRecord.tag

<%@tagimport="java.sql.*"%>

<%@tagpageEncoding="gb2312"%>

<%@attributename="bookISBN"required="true"%>

<%@variablename-given="oldBookName"scope="AT_END"%>

<%@variablename-given="oldBookAuthor"scope="AT_END"%>

<%@variablename-given="oldBookPrice"scope="AT_END"%>

<%@variablename-given="oldBookPublish"scope="AT_END"%>

<%@variablename-given="oldBookTime"scope="AT_END"%>

<%@variablename-given="oldBookAbstract"scope="AT_END"%>

<%

try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}

catch(ClassNotFoundExceptione){

out.print(e);

}

StringBufferstr=newStringBuffer();

Connectioncon;

Statementsql;

ResultSetrs;

Stringuri="jdbc:

odbc:

information";

try{

con=DriverManager.getConnection(uri,"","");

Stringquery=

"selectbookName,bookAuthor,bookPrice"+

",bookPublish,bookTime,bookAbstractfrombookFormWHEREbookISBN='"+bookISBN+"'";

sql=con.createStatement();

rs=sql.executeQuery(query);

if(rs.next()){

jspContext.setAttribute("oldBookName",rs.getString("bookName"));

jspContext.setAttribute("oldBookAuthor",rs.getString("bookAuthor"));

jspContext.setAttribute("oldBookPrice",rs.getString("bookPrice"));

jspContext.setAttribute("oldBookPublish",rs.getString("bookPublish"));

jspContext.setAttribute("oldBookTime",rs.getString("bookTime"));

jspContext.setAttribute("oldBookAbstract",rs.getString("bookAbstract"));

}

else{

jspContext.setAttribute("oldBookName","");

jspContext.setAttribute("oldBookAuthor","");

jspContext.setAttribute("oldBookPrice","");

jspContext.setAttribute("oldBookPublish","");

jspContext.setAttribute("oldBookTime","");

jspContext.setAttribute("oldBookAbstract","");

}

con.close();

}

catch(SQLExceptionexp){

jspContext.setAttribute("oldBookName","");

jspContext.setAttribute("oldBookAuthor","");

jspContext.setAttribute("oldBookPrice","");

jspContext.setAttribute("oldBookPublish","");

jspContext.setAttribute("oldBookTime","");

jspContext.setAttribute("oldBookAbstract","");

}

jspContext.setAttribute("bookISBN",bookISBN);

%>

UpdateRecord.tag

<%@tagimport="java.sql.*"%>

<%@tagpageEncoding="gb2312"%>

<%@attributename="bookISBN"required="true"%>

<%@attributename="bookName"required="true"%>

<%@attributename="bookAuthor"required="true"%>

<%@attributename="bookPrice"required="true"%>

<%@attributename="bookPublish"required="true"%>

<%@attributename="bookTime"required="true"%>

<%@attributename="bookAbstract"required="true"%>

<%@variablename-given="giveResult"variable-class=

"java.lang.StringBuffer"scope="AT_END"%>

<%

byte[]c=bookName.getBytes("iso-8859-1");

bookName=newString(c);

c=bookAuthor.getBytes("iso-8859-1");

bookAuthor=newString(c);

c=bookPublish.getBytes("iso-8859-1");

bookPublish=newString(c)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1