zoukankan      html  css  js  c++  java
  • Javaweb前台界面代码复用总结

    servlet声明定义message信息传给前天界面判断输出message;
    if(booknamelist.size()==0) {
        message="根据书名查询没有结果!";
    }
    <%
      String message = (String)request.getAttribute("message");
      if(message!=null){
    %>
    <%} %>
    查询方法可以这么写:
    前台:
    复制代码
    1
    2
    3
    4
    5
    6
    13
    16
    17
    14 查询
    15
    18
    19
    复制代码
    不同的查询方式对应着不同option的不同value值,这样可以在servlet里面更加清晰地定义不同SQL语句进行一次模糊查询,只需要写一个查询函数就可以了!
    servlet:
    复制代码
    1 public ArrayList select(String content,String way){
    2 ArrayList list=new ArrayList<>();
    3 Connection con=null;
    4 Statement state=null;
    5 ResultSet rs=null;
    6 String sql="select * from student";
    7 if(content!=""&&"1".equals(way)){
    8 sql +=" where sno='"+content+"'";
    9 }else if(content!=""&&"2".equals(way)){
    10 sql += " where sname like'%" + content + "%'";
    11 }else if(content!=""&&"3".equals(way)){
    12 sql += " where ssex='"+content+"'";
    13 }else if(content!=""&&"4".equals(way)){
    14 sql += " where saddress='"+content+"'";
    15 }else{
    16 sql ="select * from student";
    17 }
    18 try {
    19 con=DBUtil.getConn();
    20 state=con.createStatement();
    21 rs=state.executeQuery(sql);
    22 while(rs.next())
    23 {
    24 User bean=new User();
    25 bean.setSno(rs.getString("sno"));
    26 bean.setSname(rs.getString("sname"));
    27 bean.setSsex(rs.getString("ssex"));;
    28 bean.setSbirthday(rs.getString("sbirthday"));
    29 bean.setSaddress(rs.getString("saddress"));
    30 list.add(bean);
    31 }
    32
    33 } catch (SQLException e) {
    34 // TODO Auto-generated catch block
    35 e.printStackTrace();
    36 }
    37 DBUtil.close(rs, state, con);
    38 return list;
    39 }

  • 相关阅读:
    Tomcat6 一些调优设置内存和连接数
    【原创】使用c3p0数据库连接池时出现com.mchange.v2.resourcepool.TimeoutException
    JVM内存的设置
    JBOSS以及tomcat最大连接数配置和jvm内存配置
    摘抄python __init__
    Python中__init__方法介绍
    Python 绝对简明手册
    python中eval, exec, execfile,和compile [转载]
    extern、static、auto、register 定义变量的不同用法
    Python 网络编程说明
  • 原文地址:https://www.cnblogs.com/clearlove007/p/14198753.html
Copyright © 2011-2022 走看看