Jsp&Servlet入门级项目全程实录第6讲

惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧!

1、建立数据表及数据(略)

2、创建student model
     package com.java1234.model;

import java.sql.Date;

public class Student {

     private int id;
     private String stuNo;
     private String stuName;
     private String sex;
     private Date birthday;
     private int gradeId;
     private String email;
     private String stuDesc;
    
     public int getId() {
          return id;
     }
     public void setId(int id) {
          this.id = id;
     }
     public String getStuNo() {
          return stuNo;
     }
     public void setStuNo(String stuNo) {
          this.stuNo = stuNo;
     }
     public String getStuName() {
          return stuName;
     }
     public void setStuName(String stuName) {
          this.stuName = stuName;
     }
     public String getSex() {
          return sex;
     }
     public void setSex(String sex) {
          this.sex = sex;
     }
     public Date getBirthday() {
          return birthday;
     }
     public void setBirthday(Date birthday) {
          this.birthday = birthday;
     }
     public int getGradeId() {
          return gradeId;
     }
     public void setGradeId(int gradeId) {
          this.gradeId = gradeId;
     }
     public String getEmail() {
          return email;
     }
     public void setEmail(String email) {
          this.email = email;
     }
     public String getStuDesc() {
          return stuDesc;
     }
     public void setStuDesc(String stuDesc) {
          this.stuDesc = stuDesc;
     }
    
    
}

3、查询dao
     public ResultSet studentList(Connection con,PageBean pageBean)throws Exception{
          StringBuffer sb=new StringBuffer("select * from t_student s,t_grade g where s.gradeId=g.id");
          if(pageBean!=null){
               sb.append(" limit "+pageBean.getStart()+","+pageBean.getRows());              
          }
          PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
          return pstmt.executeQuery();
     }
    
     public int studentCount(Connection con)throws Exception{
          StringBuffer sb=new StringBuffer("select count(*) as total from t_student s,t_grade g where s.gradeId=g.id");
          PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
          ResultSet rs=pstmt.executeQuery();
          if(rs.next()){
               return rs.getInt("total");
          }else{
               return 0;
          }
     }     

4、日期报错
public static String formatDate(Date date,String format){
          String result="";
          SimpleDateFormat sdf=new SimpleDateFormat(format);
          if(date!=null){
               result=sdf.format(date);
          }
          return result;
     }

if(o instanceof Date){
                         mapOfColValues.put(md.getColumnName(i),DateUtil.formatDate((Date)o, "yyyy-MM-dd"));
                    }else{
                         mapOfColValues.put(md.getColumnName(i),rs.getObject(i));
                    }

5、查询表单
<div>
                    &nbsp;学号:&nbsp;<input type="text" name="s_stuNo" id="s_stuNo" size="10" />
                    &nbsp;姓名:&nbsp;<input type="text" name="s_stuNo" id="s_stuNo" size="10" />
                    &nbsp;性别:&nbsp;<select class="easyui-combobox" id="s_sex" name="s_sex" editable="false" panelHeight="auto" >
                                   <option value="" >请选择</option>
                                   <option value="男" >男</option>
                                   <option value="女" >女</option>
                    </select>
                    &nbsp;出生日期:&nbsp;<input type="text" class="easyui-datebox" name="s_bbirthday" id="s_bbirthday" size="11" editable="false" />->
                                             <input type="text" class="easyui-datebox" name="s_ebirthday" id="s_ebirthday" size="11" editable="false" />
                    &nbsp;所属班级:&nbsp;<input class="easyui-combobox" id="s_gradeId" name="s_gradeId" size="11" data-options="editable:false,valueField:'id',textField:'gradeName',url:'gradeComboList'" />
                    <a href="javascript:searchStudent()" class="easyui-linkbutton" iconCls="icon-search" plain="true" >搜索</a>
               </div>

6、ComboList
Connection con=null;
          try {
               con=dbUtil.getCon();
               JSONArray jsonArray=new JSONArray();
               JSONObject jsonObject=new JSONObject();
               jsonObject.put("id", "");
               jsonObject.put("gradeName", "请选择");
               jsonArray.add(jsonObject);
               jsonArray.addAll(JsonUtil.formatRsToJsonArray(gradeDao.gradeList(con, null,null)));
               ResponseUtil.write(response, jsonArray);
          } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }finally{
               try {
                    dbUtil.closeCon(con);
               } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
          }

 

你可能感兴趣的:(servlet)