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

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