ManagerDAO.java~100~
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:6k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.util.*;
  4. import com.actionForm.ManagerForm;
  5. import java.sql.*;
  6. import java.*;
  7. public class ManagerDAO {
  8.     private ConnDB conn = new ConnDB();
  9.     //查询数据
  10.     public Collection query(String queryif) {
  11.         ManagerForm managerForm = null;
  12.         Collection managercoll = new ArrayList();
  13.         String sql = "";
  14.         if (queryif == null || queryif == "" || queryif == "all") {
  15.             //此处需要应用左连接
  16.             sql = "select m.*,p.sysset,p.readerset,p.bookset,p.borrowback,p.sysquery from tb_manager m left join tb_purview p on m.id=p.id";
  17.         } else {
  18.             sql = "select * from tb_manger where " + queryif;
  19.         }
  20.         ResultSet rs = conn.executeQuery(sql);
  21.         int a = 0;
  22.         try {
  23.             while (rs.next()) {
  24.                 managerForm = new ManagerForm();
  25.                 managerForm.setId(Integer.valueOf(rs.getString(1)));
  26.                 managerForm.setName(rs.getString(2));
  27.                 managerForm.setPwd(rs.getString(3));
  28.                 managerForm.setSysset(rs.getInt(4));
  29.                 managerForm.setReaderset(rs.getInt(5));
  30.                 managerForm.setBookset(rs.getInt(6));
  31.                 managerForm.setBorrowback(rs.getInt(7));
  32.                 managerForm.setSysquery(rs.getInt(8));
  33.                 managercoll.add(managerForm);
  34.                 a++;
  35.             }
  36.             System.out.println("记录总数:" + a);
  37.         } catch (SQLException e) {}
  38.         return managercoll;
  39.     }
  40.     //管理员身份验证
  41.     public int checkManager(ManagerForm managerForm) {
  42.         int flag = 0;
  43.         String sql = "SELECT * FROM tb_manager where name='" +
  44.                      managerForm.getName() + "'";
  45.         ResultSet rs = conn.executeQuery(sql);
  46.         try {
  47.             if (rs.next()) {
  48.                 String pwd = managerForm.getPwd();
  49.                 if (pwd.equals(rs.getString(3))) {
  50.                     flag = 1;
  51.                 } else {
  52.                     flag = 0;
  53.                 }
  54.             }
  55.             rs.last();
  56.             int rowSum = rs.getRow();
  57.             rs.first();
  58.             if (rowSum != 1) {
  59.                 flag = 0;
  60.                 System.out.print("获取的row的值:" + sql + rowSum);
  61.             }
  62.         } catch (SQLException ex) {
  63.             flag = 0;
  64.         }
  65.         return flag;
  66.     }
  67.     //修改时应用的查询方法
  68.     public ManagerForm query_update(ManagerForm managerForm) {
  69.         ManagerForm managerForm1 = null;
  70.         String sql = "select m.*,p.sysset,p.readerset,p.bookset,p.borrowback,p.sysquery from tb_manager m left join tb_purview p on m.id=p.id where m.id=" +
  71.                      managerForm.getId() + "";
  72.         ResultSet rs = conn.executeQuery(sql);
  73.         try {
  74.             while (rs.next()) {
  75.                 managerForm1 = new ManagerForm();
  76.                 managerForm1.setId(Integer.valueOf(rs.getString(1)));
  77.                 managerForm1.setName(rs.getString(2));
  78.                 managerForm1.setPwd(rs.getString(3));
  79.                 managerForm1.setSysset(rs.getInt(4));
  80.                 managerForm1.setReaderset(rs.getInt(5));
  81.                 managerForm1.setBookset(rs.getInt(6));
  82.                 managerForm1.setBorrowback(rs.getInt(7));
  83.                 managerForm1.setSysquery(rs.getInt(8));
  84.             }
  85.         } catch (SQLException ex) {}
  86.         return managerForm1;
  87.     }
  88.     //添加数据
  89.     public int insert(ManagerForm managerForm) {
  90.         String sql1="SELECT * FROM tb_manager WHERE name='"+managerForm.getName()+"'";
  91.         ResultSet rs = conn.executeQuery(sql1);
  92.         String sql = "";
  93.         int falg = 0;
  94.             try {
  95.                 if (rs.next()) {
  96.                     falg=2;
  97.                 } else {
  98.                     sql = "INSERT INTO tb_manager (name,pwd) values('" +
  99.                                  managerForm.getName() + "','" +
  100.                                  managerForm.getPwd() +
  101.                                  "')";
  102.                     falg = conn.executeUpdate(sql);
  103.                     System.out.println("添加管理员信息的SQL:" + sql);
  104.                     conn.close();
  105.                 }
  106.             } catch (SQLException ex) {
  107.                 falg=0;
  108.             }
  109.         return falg;
  110.     }
  111.     //修改数据
  112.     public int update(ManagerForm managerForm) {
  113.         String sql1="SELECT * FROM tb_purview WHERE id="+managerForm.getId()+"";
  114.         ResultSet rs=conn.executeQuery(sql1);
  115.         String sql="";
  116.         int falg=0;
  117.         try {
  118.             if (rs.next()) {
  119.                 sql = "Update tb_purview set sysset=" + managerForm.getSysset() +
  120.                              ",readerset=" + managerForm.getReaderset() + ",bookset="+managerForm.getBookset()+",borrowback="+managerForm.getBorrowback()+",sysquery="+managerForm.getSysquery()+" where id=" +
  121.                      managerForm.getId() + "";
  122.             }else{
  123.                 sql="INSERT INTO tb_purview values("+managerForm.getId()+","+managerForm.getSysset()+","+managerForm.getReaderset()+","+managerForm.getBookset()+","+managerForm.getBorrowback()+","+managerForm.getSysquery()+")";
  124.             }
  125.             falg = conn.executeUpdate(sql);
  126.             System.out.println("修改数据时的SQL:" + sql);
  127.             conn.close();
  128.         } catch (SQLException ex) {
  129.             falg=0;
  130.         }
  131.         return falg;
  132.     }
  133.     //修改管理员密码
  134. public int updatePwd(ManagerForm managerForm){
  135.     String sql="UPDATE tb_manager SET pwd='"+managerForm.getPwd()+"' where name='"+managerForm.getName()+"'";
  136.     int ret=conn.executeUpdate(sql);
  137.     System.out.println("修改管理员密码时的SQL:"+sql);
  138.     conn.close();
  139.     return ret;
  140. }
  141. //删除数据
  142.     public int delete(ManagerForm managerForm) {
  143.         String sql = "DELETE FROM tb_manager where id=" + managerForm.getId() +
  144.                      "";
  145.         int falg = conn.executeUpdate(sql);
  146.         System.out.println("删除管理员信息的SQL:" + sql);
  147.         conn.close();
  148.         return falg;
  149.     }
  150. }