Chinaunix首页 | 论坛 | 博客
  • 博客访问: 268231
  • 博文数量: 111
  • 博客积分: 1591
  • 博客等级: 上尉
  • 技术积分: 877
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-20 15:14
文章分类

全部博文(111)

文章存档

2014年(1)

2012年(41)

2011年(69)

分类:

2011-12-11 20:51:27

  1. package servlet;
  2. ervlet;

  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;

  10. import javax.servlet.ServletContext;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.http.HttpServlet;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;

  15. public class ListServlet extends HttpServlet
  16. {
  17.     private String url;
  18.     private String user;
  19.     private String password;
  20.     
  21.     public void init() throws ServletException
  22.     {
  23.         ServletContext sc=getServletContext();
  24.         String driverClass=sc.getInitParameter("driverClass");
  25.         url=sc.getInitParameter("url");
  26.         user=sc.getInitParameter("user");
  27.         password=sc.getInitParameter("password");
  28.         try
  29.         {
  30.             Class.forName(driverClass);
  31.             
  32.         }
  33.         catch(ClassNotFoundException ce)
  34.         {
  35.             throw new ServletException("加载数据库驱动失败!");
  36.         }
  37.     }
  38.     
  39.     public void doGet(HttpServletRequest req, HttpServletResponse resp)
  40.                throws ServletException,IOException
  41.     {
  42.         Connection conn=null;
  43.         Statement stmt=null;
  44.         ResultSet rs=null;
  45.         req.setCharacterEncoding("gb2312");
  46.         String condition=req.getParameter("cond");
  47.         if(null==condition || condition.equals(""))
  48.         {
  49.             resp.sendRedirect("search.html");
  50.             return;
  51.         }
  52.         resp.setContentType("text/html;charset=gb2312");
  53.         PrintWriter out=resp.getWriter();
  54.         try
  55.         {
  56.             conn=DriverManager.getConnection(url,user,password);
  57.             stmt=conn.createStatement();
  58.             if(condition.equals("all"))
  59.             {
  60.                 rs=stmt.executeQuery("select * from bookinfo");
  61.                 printBookInfo(out,rs);
  62.                 out.close();
  63.             }
  64.             else if(condition.equals("precision"))
  65.             {
  66.                 String title=req.getParameter("title");
  67.                 String author=req.getParameter("author");
  68.                 String bookconcern=req.getParameter("bookconcern");
  69.                 
  70.                 if((null==title || title.equals("")) &&
  71.                    (null==author || author.equals("")) &&
  72.                    (null==bookconcern || bookconcern.equals("")))
  73.                 {
  74.                     resp.sendRedirect("search.html");
  75.                     return;
  76.                 }
  77.                 
  78.                 StringBuffer sb=new StringBuffer("select * from bookinfo where ");
  79.                 boolean bFlag=false;
  80.                 
  81.                 if(!title.equals(""))
  82.                 {
  83.                     sb.append("title = "+"'"+title+"'");
  84.                     bFlag=true;
  85.                 }
  86.                 if(!author.equals(""))
  87.                 {
  88.                     if(bFlag)
  89.                         sb.append("and author = "+"'"+author+"'");
  90.                     else
  91.                     {
  92.                        sb.append("author = "+"'"+author+"'");
  93.                        bFlag=true;
  94.                     }
  95.                 }
  96.                 if(!bookconcern.equals(""))
  97.                 {
  98.                     if(bFlag)
  99.                         sb.append("and bookconcern = "+"'"+bookconcern+"'");
  100.                     else
  101.                         sb.append("bookconcern = "+"'"+bookconcern+"'");
  102.                 }
  103.                 rs=stmt.executeQuery(sb.toString());
  104.                 printBookInfo(out,rs);
  105.                 out.close();
  106.             }
  107.             else if(condition.equals("keyword"))
  108.             {
  109.                 String keyword=req.getParameter("keyword");
  110.                 if(null==keyword || keyword.equals(""))
  111.                 {
  112.                     resp.sendRedirect("search.html");
  113.                     return;
  114.                 }
  115.                 String strSQL="select * from bookinfo where title like '%"+keyword+"%'";
  116.                 
  117.                 rs=stmt.executeQuery(strSQL);
  118.                 printBookInfo(out,rs);
  119.                 out.close();
  120.             }
  121.             else
  122.             {
  123.                 resp.sendRedirect("search.html");
  124.                 return;
  125.             }
  126.         }
  127.         catch(SQLException se)
  128.         {
  129.             se.printStackTrace();
  130.         }
  131.         finally
  132.         {
  133.             closeResultSet(rs);
  134.             closeStatement(stmt);
  135.             closeConnection(conn);
  136.         }
  137.     }
  138.     
  139.     public void doPost(HttpServletRequest req, HttpServletResponse resp)
  140.                throws ServletException,IOException
  141.     {
  142.         doGet(req,resp);
  143.     }
  144.     
  145.     private void printBookInfo(PrintWriter out,ResultSet rs)
  146.                  throws SQLException
  147.     {
  148.         out.println("");
  149.         out.println("图书信息");
  150.         out.println("");
  151.         out.println("");
  152.         out.println("
  153. ");
  154.         while(rs.next())
  155.         {
  156.             out.println("
  157. ");
  158.             out.println("
  159. ");
  160.             out.println("
  161. ");
  162.             out.println("
  163. ");
  164.             out.println("
  165. ");
  166.             out.println("
  167. ");
  168.             out.println("
  169. ");
  170.         }
  171.         out.println("
  172. 图书信息
    书名作者出版社价格发行日期
    "+rs.getString("title")+" "+rs.getString("author")+" "+rs.getString("bookconcern")+" "+rs.getFloat("price")+" "+rs.getDate("publish_date")+"
    "
    );
  173.     }
  174.     
  175.     private void closeResultSet(ResultSet rs)
  176.     {
  177.         if(rs!=null)
  178.         {
  179.             try
  180.             {
  181.                 rs.close();
  182.             }
  183.             catch(SQLException se)
  184.             {
  185.                 se.printStackTrace();
  186.             }
  187.             rs=null;
  188.         }
  189.     }
  190.     
  191.     private void closeStatement(Statement stmt)
  192.     {
  193.         if(stmt!=null)
  194.         {
  195.             try
  196.             {
  197.                 stmt.close();
  198.             }
  199.             catch(SQLException se)
  200.             {
  201.                 se.printStackTrace();
  202.             }
  203.             stmt=null;
  204.         }
  205.     }
  206.     
  207.     private void closeConnection(Connection conn)
  208.     {
  209.         if(conn!=null)
  210.         {
  211.             try
  212.             {
  213.                 conn.close();
  214.             }
  215.             catch(SQLException se)
  216.             {
  217.                 se.printStackTrace();
  218.             }
  219.             conn=null;
  220.         }
  221.     }
  222. }
1  函数printBookInfo() 负责以网页表格的相识输出结果集中的数据
 
2  客户端提交的请求参数可能包含中文字符,所以调用请求对象的setCharacter Encoding()方法指定请求正文使用的字符编码为gb2312
 
3 getParameter()方法得到查询条件
 
4 判断条件为空的话,调用响应对象的sendRedirect()方法将客户端重定向到search.html页面,然后调用return语句 结束doGet()方法 这是为了避免后面的语句被执行。
 
5 构造一个StringBuffer对象,用来准备一个SQL语句。定义一个boolean类型的变量,用来判断前面的参数是否作为where子句中的一部分,是否添加"and"
 
6 关键字搜索,模糊查询 通过在查询语句中使用关键字 like
阅读(862) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~