JSP中使用数据库

  • 格式:doc
  • 大小:618.00 KB
  • 文档页数:14

__________________________________________________

__________________________________________________ 实验报告

实验名称:JSP中使用数据库

实验目的:

1、掌握使用JDBC查询数据库中表的记录

2、掌握使用JDBC更新数据库中表的记录

3、掌握使用JDBC删除数据库中表的记录

实验环境:Windows平台,tomcat7,jdk,Microsoft

Access 2003

实验内容:

使用Microsoft Access 2003创建一个数据库Book,在数据库中创建表bookForm。

1、编写一个JSP页面inputMess.jsp和一个Tag文件FindBook.tag。

2、编写一个JSP页面updateRecord.jsp和两个Tag文件UpdateRecord.tag、GetRecord.tag。updateRecord.jsp负责调用GetRecord.tag文件,显示GetRecord.tag返回待更新的记录的有关字段的值,updateRecord.jsp调用UpdateRecord.tag文件,并显__________________________________________________

__________________________________________________ 示UpdateRecord.tag返回的有关更新记录是否成功的信息。

3、编写一个JSP页面inputDeletedISBN.jsp和一个Tag文件DelRecord.tag。

实验结果:

见附页

小结:

本次试验中学习了如何连接数据库,如何查询、更新、删除信息等重要知识。

评定成绩: 批阅教师: 年 月 日

实验前准备

使用Microsoft Access 2003创建数据库Book,然后在数据库中再创建名为bookForm的表,设置表的字段和属性,如下图所示:

在表bookForm中按格式要求输入内容,如下图所示: __________________________________________________

__________________________________________________

使用JDBC-ODBC桥接器方式和数据库建立连接,将数据库Book设置为名字为information的数据源,如下图所示:

实验一:

编写一个JSP页面inputMess.jsp和一个Tag文件FindBook.tag,使用JDBC查询数据库中表的记录,结果如下:

inputMess.jsp的代码如下所示: __________________________________________________

__________________________________________________ <%@ page contentType="text/html;charset=GB2312" %>

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

输入查询内容:

ISBN

书名

作者

出版社

出版时间

内容摘要

前方一致

后方一致

包含

<%

String findContent = request.getParameter("findContent");

String condition = request.getParameter("condition");

String findMethod = request.getParameter("findMethod");

if(findContent==null){

findContent="";

}

if(condition==null){

condition="";

}

if(findMethod==null){

findMethod="";

}

%>

查询到的图书:

tableName="bookForm"

findContent="<%=findContent%>"

condition="<%=condition%>"

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

<%=giveResult%>

__________________________________________________

__________________________________________________ FindBook.tag的代码如下所示:

<%@ tag import="java.sql.*" %>

<%@ tag pageEncoding="gb2312" %>

<%@ attribute name="dataSource" required="true" %>

<%@ attribute name="tableName" required="true" %>

<%@ attribute name="findContent" required="true" %>

<%@ attribute name="condition" required="true" %>

<%@ attribute name="findMethod" required="true" %>

<%@ variable name-given="giveResult" variable-class="ng.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.print(e);

}

Connection con;

Statement sql;

ResultSet rs;

StringBuffer queryResult=new StringBuffer(); //查询结果

String uri="jdbc:odbc:"+dataSource;

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

sql=con.createStatement();

String s="";

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("

出版时间");