jdbc,数据库案例:客户信息管理系统:jsp部分,数据库数据封装类,dao接口和实现...

jsp部分:

默认主页:

index.jsp:

<body>
  <jsp:forward page="/servlet/Controller">
   <jsp:param value="showAllCustomers" name="op"/>
  </jsp:forward>
  </body>

显示客户信息:

listCustomer.jsp:

<body>
   <div>
    <a href="${pageContext.request.contextPath }/addCustomer.jsp">添加</a>
    <a href="javascript:delMulti()">删除</a>
   </div>
    <c:if test="${empty page.records}">
     没有客户信息
    </c:if>
    <c:if test="${!empty page.records}">
     <form action="${pageContext.request.contextPath}/servlet/Controller?op=delMulti" method="post">
     <table bordercolor="1" border="1" width="80%">
      <tr>
       <th>选择</th>
       <th>客户姓名</th>
       <th>性别</th>
       <th>出生日期</th>
       <th>联系电话</th>
       <th>邮箱</th>
       <th>爱好</th>
       <th>类型</th>
       <th>描述</th>
       <th>操作</th>
      </tr>
      <c:forEach items="${page.records}" var="c" varStatus="vs">
       <table bordercolor="1" border="1" width="80%">
        <tr class="${vs.index%2==0?'order':'even'}">
         <td>
          <input type="checkbox" name="ids" value="${ c.id}"/>
         </td>
         <td>${c.name}</td>
         <td>${c.gender}</td>
         <td>${c.birthday}</td>
         <td>${c.cellphone}</td>
         <td>${c.email}</td>
         <td>${c.preference}</td>
         <td>${c.type}</td>
         <td>${c.description}</td>
      
         <td>
          <a href="${pageContext.request.contextPath }/servlet/Controller?op=editCustomerUI&customerId=${c.id}">修改</a>
          <a href="javascript:delOne('${c.id}')">删除</a>
         </td>
        </tr>
       </table>
      </c:forEach>
     </table>
     </form>
     <%@ include file="/common/page.jsp" %>
    </c:if>
  
    <script type="text/javascript">
     function delMulti(){
      var select=false;
      var idsArray=document.getElementsByName("ids");
      for(var i=0;i<idsArray.length;i++){
       if(idsArray[i].checked){
        select=true;
       }
      }
      if(select){
       var sure=window.confirm("是否选择删除");
       if(sure){
        document.forms[0].submit();
       }
      }else{
       alert("请先选择要删除的客户");
      }
     }
     function delOne(customerId){
      var sure=window.confirm("确认要删除吗");
      if(sure){
       window.location.href="${pageContext.request.contextPath }/servlet/Controller?op=delOneCustomer&customerId="+customerId;
      }
     }
    </script>
  </body>

增加客户信息页面:

addCustomer.jsp:

<form action="${pageContext.request.contextPath}/servlet/Controller?op=addCustomer" method="post">
     <table border="1">
      <tr>
       <td>
        姓名:<input type="text" name="name"/>
       </td>
      </tr>
      <tr>
       <td>
        性别:<input type="radio" name="gender" value="1"/>男
        <input type="radio" name="gender" value="0"/>女
       </td>
      </tr>
      <tr>
       <td>
        出生日期:<input type="text" name="birthday" value="1991-06-11"/>
       </td>
      </tr>
      <tr>
       <td>
        联系电话:<input type="text" name="cellphone" value="187"/>
       </td>
      </tr>
      <tr>
       <td>
        邮箱:<input type="text" name="email"/>
       </td>
      </tr>
      <tr>
       <td>
        兴趣爱好:<input type="checkbox" name="preferences" value="吃饭" checked="checked"/>吃饭
        <input type="checkbox" name="preferences" value="睡觉"/>睡觉
        <input type="checkbox" name="preferences" value="学java"/>学java
       </td>
      </tr>
      <tr>
       <td>
        用户类型:<input type="radio" name="type" value="普通客户" checked="checked"/>普通用户
        <input type="radio" name="type" value="vip" />vip用户
       </td>
      </tr>
      <tr>
       <td>
        描述:<textarea rows="3" cols="38" name="description"></textarea>
       </td>
      </tr>
      <tr>
       <td>
        <input type="submit" value="保存"/>
       </td>
      </tr>
     </table>
    </form>
  </body>

修改客户信息页面:

<body>
        <form action="${pageContext.request.contextPath}/servlet/Controller?op=editCustomer" method="post">
        <input type="hidden" name="id" value="${c.id}"/>
     <table border="1">
      <tr>
       <td>
        
        姓名:<input type="text" name="name" value="${c.name}"/>
       </td>
      </tr>
      <tr>
       <td>
        性别:<input type="radio" name="gender" value="1" ${c.gender=='1'?'checked="checked"':''}}"/>男
        <input type="radio" name="gender" value="0" ${c.gender=='0'?'checked="checked"':''}/>女
       </td>
      </tr>
      <tr>
       <td>
        出生日期:<input type="text" name="birthday" value="${c.birthday}"/>
       </td>
      </tr>
      <tr>
       <td>
        联系电话:<input type="text" name="cellphone" value="${c.cellphone}"/>
       </td>
      </tr>
      <tr>
       <td>
        邮箱:<input type="text" name="email" value="${c.email}"/>
       </td>
      </tr>
      <tr>
       <td>
        兴趣爱好:<input type="checkbox" name="preferences" value="吃饭" ${fn:contains(c.preference,'吃饭')?'checked="checked"':' ' }/>吃饭
        <input type="checkbox" name="preferences" value="睡觉" ${fn:contains(c.preference,'睡觉')?'checked="checked"':' '}/>睡觉
        <input type="checkbox" name="preferences" value="学java" ${fn:contains(c.preference,'学java')?'checked="checked"':' '}/>学java
       </td>
      </tr>
      <tr>
       <td>
        用户类型:<input type="radio" name="type" value="普通客户" ${c.type=='普通用户'?'checked="checked"':'' }/>普通用户
        <input type="radio" name="type" value="vip" ${c.type=='普通用户'?'checked="checked"':'' }/>vip用户
       </td>
      </tr>
      <tr>
       <td>
        描述:<textarea rows="3" cols="38" name="description" value="${c.description}"></textarea>
       </td>
      </tr>
      <tr>
       <td>
        <input type="submit" value="保存"/>
       </td>
      </tr>
     </table>
    </form>
  </body>

数据库信息封装类:

Customer:

public class Customer implements Serializable{
 private String id;
 private String name;
 private String gender;
 private Date birthday;
 private String cellphone;
 private String email;
 private String preference;
 private String type;
 private String description;
 
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public Date getBirthday() {
  return birthday;
 }
 public void setBirthday(Date birthday) {
  this.birthday = birthday;
 }
 public String getCellphone() {
  return cellphone;
 }
 public void setCellphone(String cellphone) {
  this.cellphone = cellphone;
 }
 
 public String getPreference() {
  return preference;
 }
 public void setPreference(String preference) {
  this.preference = preference;
 }
 public String getType() {
  return type;
 }
 public void setType(String type) {
  this.type = type;
 }
 public String getEmail() {
  return email;
 }
 public void setEmail(String email) {
  this.email = email;
 }
 public String getDescription() {
  return description;
 }
 public void setDescription(String description) {
  this.description = description;
 }
}

dao层:

dao接口:

public interface CustomerDao {
 @Deprecated
 List<Customer> findAll();

 void add(Customer c);

 void delete(String customerId);

 Customer findById(String customerId);

 void update(Customer c);
 int getTotalRecordsNum();
 List<Customer> findPageCustomers(int offset,int size);
}

dao接口实现:

public class CustomerDaoImpl implements CustomerDao {
 @Deprecated
 public List<Customer> findAll() {
  Connection conn=null;
  PreparedStatement stmt=null;
  ResultSet rs=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("select *from customer");
   rs=stmt.executeQuery();
   List<Customer> list=new ArrayList<Customer>();
   while(rs.next()){
    Customer c =new Customer();
    c.setId(rs.getString("id"));
    c.setName(rs.getString("name"));
    c.setGender(rs.getString("gender"));
    c.setBirthday(rs.getDate("birthday"));
    c.setEmail(rs.getString("email"));
    c.setCellphone(rs.getString("cellphone"));
    c.setPreference(rs.getString("preference"));
    c.setType(rs.getString("type"));
    c.setDescription(rs.getString("description"));
    list.add(c);
   }
   return list;
  }catch(Exception e){
   throw new RuntimeException();
  }finally{
   JdbcUtil.release(rs, stmt, conn);
  }
 }

 public void add(Customer c) {
  Connection conn=null;
  PreparedStatement stmt=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("insert into customer values(?,?,?,?,?,?,?,?,?)");
   stmt.setString(1, c.getId());
   stmt.setString(2, c.getName());
   stmt.setString(3, c.getGender());
   stmt.setDate(4, new java.sql.Date(c.getBirthday().getTime()));
   stmt.setString(5, c.getCellphone());
   stmt.setString(6, c.getEmail());
   stmt.setString(7, c.getPreference());
   stmt.setString(8, c.getType());
   stmt.setString(9, c.getDescription());
   stmt.executeUpdate();
  }catch(Exception e){
   throw new RuntimeException(e);
  }finally{
   JdbcUtil.release(null, stmt, conn);
  }
 }

 public void delete(String customerId) {
  Connection conn=null;
  PreparedStatement stmt=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("delete from customer where id=?");
   stmt.setString(1, customerId);
   stmt.executeUpdate();
  }catch(Exception e){
   throw new RuntimeException();
  }finally{
   JdbcUtil.release(null, stmt, conn);
  }
 }

 public Customer findById(String customerId) {
  Connection conn=null;
  PreparedStatement stmt=null;
  ResultSet rs=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("select *from customer where id=?");
   stmt.setString(1, customerId);
   rs=stmt.executeQuery();
   if(rs.next()){
    Customer c =new Customer();
    c.setId(rs.getString("id"));
    c.setName(rs.getString("name"));
    c.setGender(rs.getString("gender"));
    c.setBirthday(rs.getDate("birthday"));
    c.setEmail(rs.getString("email"));
    c.setCellphone(rs.getString("cellphone"));
    c.setType(rs.getString("type"));
    c.setDescription(rs.getString("description"));
    return c;
   } 
   return null;
  }catch(Exception e){
   throw new RuntimeException();
  }finally{
   JdbcUtil.release(rs, stmt, conn);
  }
 }

 public void update(Customer c) {
  Connection conn=null;
  PreparedStatement stmt=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("update customer set id=?, name=?,gender=?,birthday=?,cellphone=?,email=?,preference=?,type=?,description=?");
   stmt.setString(1, c.getId());
   stmt.setString(2, c.getName());
   stmt.setString(3, c.getGender());
   stmt.setDate(4, new java.sql.Date(c.getBirthday().getTime()));
   stmt.setString(5, c.getCellphone());
   stmt.setString(6, c.getEmail());
   stmt.setString(7, c.getPreference());
   stmt.setString(8, c.getType());
   stmt.setString(9, c.getDescription());
   stmt.executeUpdate();
  }catch(Exception e){
   throw new RuntimeException(e);
  }finally{
   JdbcUtil.release(null, stmt, conn);
  }
 }

 public int getTotalRecordsNum() {
  Connection conn=null;
  PreparedStatement stmt=null;
  ResultSet rs=null;
  try{
   conn=JdbcUtil.getConnection();
   stmt=conn.prepareStatement("select count(*) from customer");
   rs=stmt.executeQuery();
   if(rs.next()){
    return rs.getInt(1);
   }
   return 0;
  }catch(Exception e){
   throw new RuntimeException();
  }finally{
   JdbcUtil.release(rs, stmt, conn);
  }
 }

 public List<Customer> findPageCustomers(int offset, int size) {
  Connection conn=null;
  PreparedStatement stmt=null;
  ResultSet rs=null;
  try{
  conn=JdbcUtil.getConnection();
  stmt=conn.prepareStatement("select *from customer limit ?,?");
  stmt.setInt(1, offset);
  stmt.setInt(2, size);
  rs=stmt.executeQuery();
  List<Customer> list=new ArrayList<Customer>();
  while(rs.next()){
   Customer c =new Customer();
   c.setId(rs.getString("id"));
   c.setName(rs.getString("name"));
   c.setGender(rs.getString("gender"));
   c.setBirthday(rs.getDate("birthday"));
   c.setEmail(rs.getString("email"));
   c.setCellphone(rs.getString("cellphone"));
   c.setPreference(rs.getString("preference"));
   c.setType(rs.getString("type"));
   c.setDescription(rs.getString("description"));
   list.add(c);
  }
   return list;
  }catch(Exception e){
   throw new RuntimeException();
  }finally{
   JdbcUtil.release(rs, stmt, conn);
  }
 }
}

转载于:https://my.oschina.net/u/1589656/blog/285182

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值