jsp+servlet+javabean实现简单的查询

时间:2022-09-16 19:03:53

使用到的还是access数据库,采用不用配置数据源的方法连接access数据库,无需配置数据源的方法在上一篇随笔中有提到

首先是查询界面cha.jsp,接受两个变量,查询类别(condition)和查询的关键字(name)

  
  
  
<% @ page contentType = " text/html; charset=gb2312 " language = " java " import = " java.sql.* " errorPage = "" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 无标题文档 </ title >
</ head >

< body bgcolor ="cyan" >
< center >
< h1 > 请输入要查询的关键字 </ h1 >
< form action ="/test5/Book1" method ="post" >
请选择查询条件
< select name ="condition" >
< option > 作者 </ option >
< option selected ="selected" > 书名 </ option >
</ select >
请输入
< input type ="text" name ="name" />< br />
< input type ="submit" value ="提交" />
</ form >
</ body >
</ html >

之后是servlet部分,主要是检查查询的关键字是否为空

jsp+servlet+javabean实现简单的查询jsp+servlet+javabean实现简单的查询代码
   
   
   
package test;
import javax.servlet. * ;
import javax.servlet.http. * ;
import java.io. * ;

public class Book1 extends HttpServlet
{
public void init(ServletConfig config) throws ServletException
{
super .init(config);
}

public void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out
= response.getWriter();
response.setContentType(
" text/html;charset=gb2312 " );
String condition
= request.getParameter( " condition " );
String name
= request.getParameter( " name " );
if (name.equals( "" ))
{
response.sendRedirect(
" cha.jsp " );
}
else
{
HttpSession session
= request.getSession( true );
session.setAttribute(
" condition " ,condition);
session.setAttribute(
" name " ,name);
response.sendRedirect(
" xian.jsp " );
}
}

public void destroy()
{

}
}

接下来是实现数据库查询功能的javabean类

  
  
  
package test;
import java.sql. * ;
import java.io. * ;
import java.util. * ;

public class BookCo
{

public String name,condition;
Connection con
= null ;
String strurl
= " jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\Program Files\\Apache Software Foundation\\tomcat 6.0\\webapps\\test5\\book.mdb " ;
Statement stmt
= null ;
ResultSet rs
= null ;

public BookCo()
{
name
= "" ;
condition
= "" ;
}

public void setKeyword(String s,String ss)
{
name
= s;
condition
= ss;
try
{
byte [] b = name.getBytes( " iso-8859-1 " );
name
= new String(b);
byte [] bb = condition.getBytes( " iso-8859-1 " );
condition
= new String(bb);
}
catch (Exception e){}
}

public ResultSet Boo()
{

String drivername
= " sun.jdbc.odbc.JdbcOdbcDriver " ;

String sql
= " select * from book " ;
try
{
Class.forName(drivername);
con
= DriverManager.getConnection(strurl);
stmt
= con.createStatement();

if (condition.equals( " 作者 " ))
{
sql
= " select * from book where book_author=' " + name + " ' " ;
}
if (condition.equals( " 书名 " ))
{
sql
= " select * from book where book_name=' " + name + " ' " ;
}
rs
= stmt.executeQuery(sql);
}
catch (Exception e)
{
System.out.println(
" connection fail " );
}
return rs;
}
}

最后是展示查询结果的jsp界面

  
  
  
<% @ page contentType = " text/html; charset=gb2312 " language = " java " import = " java.sql.* " errorPage = "" %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" />
< title > 无标题文档 </ title >
</ head >
<% @ page import = " test.BookCo " %>
<% @ page import = " java.sql.* " %>
< body >
< jsp:useBean id ="Co" class ="test.BookCo" scope ="application" ></ jsp:useBean >
< center >
< h1 > 图书信息 </ h1 >
< table border ="1" width ="80%" >
< th > 书号 </ th >< th > 书名 </ th >< th > 作者 </ th >< th > 出版社 </ th >< th > 图书价格 </ th >< th > 出版日期 </ th >
<%
String name = ( String )session.getAttribute( " name " );
String condition = ( String )session.getAttribute( " condition " );
Co.setKeyword(name,condition);
// out.print(Co.name + Co.condition);
// out.print(Co.Boo());
ResultSet rs
= Co.Boo();
while (rs.next())
{
out.print(
" <tr><td> " + rs.getInt( 1 ) + " </td> " );
out.print(
" <td> " + rs.getString( 2 ) + " </td> " );
out.print(
" <td> " + rs.getString( 3 ) + " </td> " );
out.print(
" <td> " + rs.getString( 4 ) + " </td> " );
out.print(
" <td> " + rs.getInt( 5 ) + " </td> " );
out.print(
" <td> " + rs.getString( 6 ) + " </td></tr> " );
}
rs.close();
%>
</ table >
</ center >
</ body >
</ html >

接下来的问题就是,如果我用<%=rs.getInt(1)%>来输出第一个字段的值时,会发生错误,我调试了很久也没有解决,

希望高手来帮我解决一下,谢谢

上面那个问题已经解决了,rs.getInt(1)这个函数是按顺序来取出数据库中的字段的,而且不能往回取,也就是只能取1次。

所以说如果<%=rs.getInt(1)%>这样的话。。前面out.print("<tr><td>"+rs.getInt(1)+"</td>");就不能写了,2个

只能选其一。