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