实验六:与数据库的连接
1.相关知识点
本实验使用JDBC-ODBC桥接器方式和数据库建立连接,那么查询数据库中表的记录的步骤是: ①与数据源redsun建立连接。
Connection con=DriverManager.getConnection(\②返回Statement对象。 Statement sql=con.createStatemet();
③向数据库发送关于查询记录的SQL语句,返回查询结果,即ResultSet对象。 ResultSet rs=aql.executeQuery(查询message表的SQL语句);
2.实验目的
本实验的目的是让读者掌握使用JDBC查询数据库中表的记录
3.实验要求
编写三个JSP页面:inputCondition,jsp、byNumber..jsp和byName.jsp页面。编写两个Tag文件:NumberCondition.tag和NameCondition.tag。 ⑴inputCondition.jsp的具体要求
inputCondition.jsp页面提供两个表单。其中一个表单允许用户输入要查询的学生的学号,即输入message表中number字段的查询条件,然后将查询条件提交给byNumber.jsp,另一个表单允许用户输入要查询的学生的姓名,即输入message表中name字段的查询条件,然后将查询条件提交给byName.jsp。inputCondition.jsp页面的效果如图6-38所示。 ⑵byNumber.jsp的具体要求
byNumber.jsp页面首先获得inputCondition.jsp页面提交的关于number字段的查询条件,然后使用Tag标记调用Tag文件ByNumber.tag,并将number字段的查询条件传递给ByNumber.tag。 ⑶byName.jsp的具体要求
byName.jsp页面首先获得inpputCondition.jsp页面提交的关于name字段的查询条件,然后使用Tag标记调用Tag文件ByName.tag,并将name字段的查询条件传递给ByName.tag。 ⑷NumberCondition.tag的具体要求
NumberCondition.tag文件使用attribute指令获得byNumber.jsp页面传递过来的number字段的查询条件,然后和数据源redsun建立连接,根据得到的查询条件查询message表。NumberCondition.tag文件使用variable指令将查询结果返回给byNumber.jsp页面。 ⑸NameCondition.tag的具体要求
NameCondiion.tag文件使用attribute指令获得byName.jsp页面传递过来的name字段的查询条件,然后和数据源redsun建立连接,根据得到的查询条件查询message表。NameCondition.tag文件使用variable指令将查询结果返回给byName.jsp页面。
4.参考代码
代码仅供参考,学生可按着实验要求,参考本代码编写代码。 JSP页面参考代码如下:
QueryTag.tag
<%@ tag pageEncoding=\<%@ tag import=\
<%@ attribute name=\<%@ attribute name=\<%@ attribute name=\
<%@ attribute name=\<%@ variable name-given=\
<%@ variable name-given=\<% StringBuffer result;
result=new StringBuffer();
try{ Class.forName(\}
catch(Exception e){} Connection con; Statement sql; ResultSet rs;
try{ result.append(\String uri=\
con=DriverManager.getConnection(uri,user,password); DatabaseMetaData metadata=con.getMetaData();
ResultSet rs1=metadata.getColumns(null,null,tableName,null); int 字段个数=0; result.append(\while(rs1.next()){ 字段个数++;
String clumnName=rs1.getString(4);
result.append(\}
result.append(\sql=con.createStatement();
rs=sql.executeQuery(\while(rs.next()){
result.append(\
for(int k=1;k<=字段个数;k++)
result.append(\result.append(\}
result.append(\con.close(); }
catch(SQLException e){
result.append(\请输入正确的用户名和密码\}
jspContext.setAttribute(\ //返回queryResult对象 jspContext.setAttribute(\ // 返回biao对象 %>
example-3.jsp
<%@ page contentType=\<%@ taglib tagdir=\
inquire.jsp
<%@ page contentType=\<%@ taglib tagdir=\
<% String dSource=request.getParameter(\String tName=request.getParameter(\String id=request.getParameter(\
String secret=request.getParameter(\%>
在<%=biao%>表查询到记录:<%--biao是Tag文件返回的对象--%>
<%=queryResult%><%--queryResult是Tag文件返回的对象--%>
example6-8.jsp
<%@ page contentType=\<%@ taglib tagdir=\
NewRecord.jsp
<%@ tag pageEncoding=\<%@ tag import=\
<%@ attribute name=\<%@ attribute name=\
<%@ attribute name=\<%@ attribute name=\<% float p=Float.parseFloat(price);
String condition1=\TE product SET name='\ condition2=\product SET madeTime='\
number=\
condition3=\ try{ Class.forName(\}
catch(Exception e){} Connection con; Statement sql; ResultSet rs;
try{ String uri=\
con=DriverManager.getConnection(uri,\ sql=con.createStatement(); sql.executeUpdate(condition1); sql.executeUpdate(condition2); sql.executeUpdate(condition3); con.close(); }
catch(Exception e){ out.print(\} %>
newResult.jsp
<%@ page contentType=\<%@ taglib tagdir=\<%@ taglib tagdir=\
<% String nu=request.getParameter(\ String na=request.getParameter(\String mT=request.getParameter(\ String pr=request.getParameter(\ byte bb[]=na.getBytes(\ na=new String(bb); %>
AddRecord.tag
<%@ tag pageEncoding=\<%@ tag import=\
<%@ attribute name=\<%@ attribute name=\
<%@ attribute name=\<%@ attribute name=\<% float p=Float.parseFloat(price);
String condition=\
VALUES\ try{ Class.forName(\}
catch(Exception e){} Connection con; Statement sql; ResultSet rs;
try{ String uri=\
con=DriverManager.getConnection(uri,\ sql=con.createStatement(); sql.executeUpdate(condition); con.close(); }
catch(Exception e){ out.print(\} %>
INTO product
example6-9.jsp
<%@ page contentType=\<%@ taglib tagdir=\
newDatabase.jsp
<%@ page contentType=\<%@ taglib tagdir=\
<% String nu=request.getParameter(\ String na=request.getParameter(\String mT=request.getParameter(\ String pr=request.getParameter(\ byte bb[]=na.getBytes(\