ManagerDAO.java~103~
上传用户: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.         }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 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 ManagerForm query_pwd(ManagerForm managerForm) {
  90.         ManagerForm managerForm1 = null;
  91.         String sql = "SELECT * FROM tb_manager WHERE name='" +managerForm.getName() + "'";
  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.             }
  100.         } catch (SQLException ex) {}
  101.         return managerForm1;
  102.     }
  103.     //添加数据
  104.     public int insert(ManagerForm managerForm) {
  105.         String sql1="SELECT * FROM tb_manager WHERE name='"+managerForm.getName()+"'";
  106.         ResultSet rs = conn.executeQuery(sql1);
  107.         String sql = "";
  108.         int falg = 0;
  109.             try {
  110.                 if (rs.next()) {
  111.                     falg=2;
  112.                 } else {
  113.                     sql = "INSERT INTO tb_manager (name,pwd) values('" +
  114.                                  managerForm.getName() + "','" +
  115.                                  managerForm.getPwd() +
  116.                                  "')";
  117.                     falg = conn.executeUpdate(sql);
  118.                     System.out.println("添加管理员信息的SQL:" + sql);
  119.                     conn.close();
  120.                 }
  121.             } catch (SQLException ex) {
  122.                 falg=0;
  123.             }
  124.         return falg;
  125.     }
  126.     //修改数据
  127.     public int update(ManagerForm managerForm) {
  128.         String sql1="SELECT * FROM tb_purview WHERE id="+managerForm.getId()+"";
  129.         ResultSet rs=conn.executeQuery(sql1);
  130.         String sql="";
  131.         int falg=0;
  132.         try {
  133.             if (rs.next()) {
  134.                 sql = "Update tb_purview set sysset=" + managerForm.getSysset() +
  135.                              ",readerset=" + managerForm.getReaderset() + ",bookset="+managerForm.getBookset()+",borrowback="+managerForm.getBorrowback()+",sysquery="+managerForm.getSysquery()+" where id=" +
  136.                      managerForm.getId() + "";
  137.             }else{
  138.                 sql="INSERT INTO tb_purview values("+managerForm.getId()+","+managerForm.getSysset()+","+managerForm.getReaderset()+","+managerForm.getBookset()+","+managerForm.getBorrowback()+","+managerForm.getSysquery()+")";
  139.             }
  140.             falg = conn.executeUpdate(sql);
  141.             System.out.println("修改数据时的SQL:" + sql);
  142.             conn.close();
  143.         } catch (SQLException ex) {
  144.             falg=0;
  145.         }
  146.         return falg;
  147.     }
  148.     //修改管理员密码
  149. public int updatePwd(ManagerForm managerForm){
  150.     String sql="UPDATE tb_manager SET pwd='"+managerForm.getPwd()+"' where name='"+managerForm.getName()+"'";
  151.     int ret=conn.executeUpdate(sql);
  152.     System.out.println("修改管理员密码时的SQL:"+sql);
  153.     conn.close();
  154.     return ret;
  155. }
  156. //删除数据
  157.     public int delete(ManagerForm managerForm) {
  158.         String sql = "DELETE FROM tb_manager where id=" + managerForm.getId() +
  159.                      "";
  160.         int falg = conn.executeUpdate(sql);
  161.         System.out.println("删除管理员信息的SQL:" + sql);
  162.         conn.close();
  163.         return falg;
  164.     }
  165. }