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

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