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

Jsp/Servlet

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.util.*;
  4. import java.sql.*;
  5. import com.actionForm.ReaderForm;
  6. public class ReaderDAO {
  7.     private ConnDB conn=new ConnDB();
  8. //查询数据
  9. public Collection query(String strif){
  10.     ReaderForm readerForm=null;
  11.     Collection readerColl=new ArrayList();
  12.     String sql="";
  13.     if(strif!="all" && strif!=null && strif!=""){
  14.         sql="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id where "+strif+"";
  15.     }else{
  16.         sql="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id";
  17.     }
  18.     ResultSet rs=conn.executeQuery(sql);
  19.     try {
  20.         while (rs.next()) {
  21.             readerForm=new ReaderForm();
  22.             readerForm.setId(Integer.valueOf(rs.getString(1)));
  23.             readerForm.setName(rs.getString(2));
  24.             readerForm.setSex(rs.getString(3));
  25.             readerForm.setBarcode(rs.getString(4));
  26.             readerForm.setVocation(rs.getString(5));
  27.             readerForm.setBirthday(rs.getString(6));
  28.             readerForm.setPaperType(rs.getString(7));
  29.             readerForm.setPaperNO(rs.getString(8));
  30.             readerForm.setTel(rs.getString(9));
  31.             readerForm.setEmail(rs.getString(10));
  32.             readerForm.setCreateDate(rs.getString(11));
  33.             readerForm.setOperator(rs.getString(12));
  34.             readerForm.setRemark(rs.getString(13));
  35.             readerForm.setTypeid(rs.getInt(14));
  36.             readerForm.setTypename(rs.getString(15));
  37.             readerForm.setNumber(rs.getInt(16));
  38.             readerColl.add(readerForm);
  39.         }
  40.     } catch (SQLException ex) {
  41.     }
  42.     conn.close();
  43.     return readerColl;
  44. }
  45. //用于修改的查询
  46. public ReaderForm queryM(ReaderForm readerForm){
  47.     ReaderForm readerForm1=null;
  48.     String sql="";
  49.     if(readerForm.getId()!=null){
  50.             sql="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id where r.id="+readerForm.getId()+"";
  51.     }else if(readerForm.getBarcode()!=null){
  52.             sql="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id where r.barcode="+readerForm.getBarcode()+"";
  53.         }
  54.     System.out.println("修改时的SQL:"+sql);
  55.     ResultSet rs=conn.executeQuery(sql);
  56.     try {
  57.         while (rs.next()) {
  58.             readerForm1=new ReaderForm();
  59.             readerForm1.setId(Integer.valueOf(rs.getString(1)));
  60.             readerForm1.setName(rs.getString(2));
  61.             readerForm1.setSex(rs.getString(3));
  62.             readerForm1.setBarcode(rs.getString(4));
  63.             readerForm1.setVocation(rs.getString(5));
  64.             readerForm1.setBirthday(rs.getString(6));
  65.             readerForm1.setPaperType(rs.getString(7));
  66.             readerForm1.setPaperNO(rs.getString(8));
  67.             readerForm1.setTel(rs.getString(9));
  68.             readerForm1.setEmail(rs.getString(10));
  69.             readerForm1.setCreateDate(rs.getString(11));
  70.             readerForm1.setOperator(rs.getString(12));
  71.             readerForm1.setRemark(rs.getString(13));
  72.             readerForm1.setTypeid(rs.getInt(14));
  73.             readerForm1.setTypename(rs.getString(15));
  74.             readerForm1.setNumber(rs.getInt(16));
  75.             System.out.println(rs.getString(2));
  76.         }
  77.     } catch (SQLException ex) {
  78.     }
  79.     conn.close();
  80.     return readerForm1;
  81. }
  82. //添加数据
  83. public int insert(ReaderForm readerForm){
  84. String sql1="SELECT * FROM tb_reader WHERE barcode='"+readerForm.getBarcode()+"'";
  85. ResultSet rs = conn.executeQuery(sql1);
  86. String sql = "";
  87. int falg = 0;
  88. try {
  89.     if (rs.next()) {
  90.         falg = 2;
  91.     } else {
  92.         sql ="Insert into tb_reader (name,sex,barcode,vocation,birthday,paperType,paperNO,tel,email,createDate,operator,remark,typeid) values('"+readerForm.getName()+"','"+readerForm.getSex()+"','"+readerForm.getBarcode()+"','"+readerForm.getVocation()+"','"+readerForm.getBirthday()+"','"+readerForm.getPaperType()+"','"+readerForm.getPaperNO()+"','"+readerForm.getTel()+"','"+readerForm.getEmail()+"','"+readerForm.getCreateDate()+"','"+readerForm.getOperator()+"','"+readerForm.getRemark()+"',"+readerForm.getTypeid()+")";
  93.         falg = conn.executeUpdate(sql);
  94.         System.out.println("添加读者信息的SQL:" + sql);
  95.         conn.close();
  96.     }
  97. } catch (SQLException ex) {
  98.     falg = 0;
  99. }
  100. System.out.println("falg:"+falg);
  101. return falg;
  102. }
  103. //修改数据
  104. public int update(ReaderForm readerForm){
  105. String sql="Update tb_reader set sex='"+readerForm.getSex()+"',barcode='"+readerForm.getBarcode()+"',vocation='"+readerForm.getVocation()+"',birthday='"+readerForm.getBirthday()+"',paperType='"+readerForm.getPaperType()+"',paperNO='"+readerForm.getPaperNO()+"',tel='"+readerForm.getTel()+"',email='"+readerForm.getEmail()+"',remark='"+readerForm.getRemark()+"',typeid="+readerForm.getTypeid()+" where id="+readerForm.getId()+"";
  106. int falg=conn.executeUpdate(sql);
  107. System.out.println("修改数据时的SQL:"+sql);
  108. conn.close();
  109. return falg;
  110. }
  111. //删除数据
  112. public int delete(ReaderForm readerForm){
  113. String sql="Delete from tb_reader where id="+readerForm.getId()+"";
  114. int falg=conn.executeUpdate(sql);
  115. System.out.println("删除时的SQL:"+sql);
  116. return falg;
  117. }
  118. }