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