ManagerDAO.java
上传用户:ycdfsj258
上传日期:2022-08-04
资源大小:1094k
文件大小:8k
源码类别:

图形图象

开发平台:

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