ReaderDAO.java~13~
上传用户: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="select r.*,t.name as typename,t.number from tb_reader r left join tb_readerType t on r.typeid=t.id r.id="+readerForm.getId()+"";
  49.     System.out.println("修改时的SQL:"+sql);
  50.     ResultSet rs=conn.executeQuery(sql);
  51.     try {
  52.         while (rs.next()) {
  53.             readerForm1=new ReaderForm();
  54.             readerForm1.setId(Integer.valueOf(rs.getString(1)));
  55.             readerForm1.setName(rs.getString(2));
  56.             readerForm1.setSex(rs.getString(3));
  57.             readerForm1.setBarcode(rs.getString(4));
  58.             readerForm1.setVocation(rs.getString(5));
  59.             readerForm1.setBirthday(rs.getString(6));
  60.             readerForm1.setPaperType(rs.getString(7));
  61.             readerForm1.setPaperNO(rs.getString(8));
  62.             readerForm1.setTel(rs.getString(9));
  63.             readerForm1.setEmail(rs.getString(10));
  64.             readerForm1.setCreateDate(rs.getString(11));
  65.             readerForm1.setOperator(rs.getString(12));
  66.             readerForm1.setRemark(rs.getString(13));
  67.             readerForm1.setTypeid(rs.getInt(14));
  68.             readerForm.setTypename(rs.getString(15));
  69.             readerForm.setNumber(rs.getInt(16));
  70.         }
  71.     } catch (SQLException ex) {
  72.     }
  73.     conn.close();
  74.     return readerForm1;
  75. }
  76. //添加数据
  77. public int insert(ReaderForm readerForm){
  78. String sql1="SELECT * FROM tb_reader WHERE barcode='"+readerForm.getBarcode()+"'";
  79. ResultSet rs = conn.executeQuery(sql1);
  80. String sql = "";
  81. int falg = 0;
  82. try {
  83.     if (rs.next()) {
  84.         falg = 2;
  85.     } else {
  86.         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()+")";
  87.         falg = conn.executeUpdate(sql);
  88.         System.out.println("添加读者信息的SQL:" + sql);
  89.         conn.close();
  90.     }
  91. } catch (SQLException ex) {
  92.     falg = 0;
  93. }
  94. System.out.println("falg:"+falg);
  95. return falg;
  96. }
  97. //修改数据
  98. public int update(ReaderForm readerForm){
  99. String sql="Update tb_reader set name='"+readerForm.getName()+"',sex='"+readerForm.getSex()+"',barcode='"+readerForm.getBarcode()+"',vocation='"+readerForm.getVocation()+"',birthday='"+readerForm.getBirthday()+",paperType='"+readerForm.getPaperType()+"',paperNO='"+readerForm.getPaperNO()+"',tel='"+readerForm.getTel()+"',email='"+readerForm.getEmail()+"',operator='"+readerForm.getOperator()+"',remark='"+readerForm.getRemark()+"',typeid="+readerForm.getTypeid()+" where id="+readerForm.getId()+"";
  100. int falg=conn.executeUpdate(sql);
  101. System.out.println("修改数据时的SQL:"+sql);
  102. conn.close();
  103. return falg;
  104. }
  105. //删除数据
  106. public int delete(ReaderForm readerForm){
  107. String sql="Delete from tb_reader where id="+readerForm.getId()+"";
  108. int falg=conn.executeUpdate(sql);
  109. System.out.println("删除时的SQL:"+sql);
  110. return falg;
  111. }
  112. }