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

Jsp/Servlet

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.util.*;
  4. import java.sql.*;
  5. import com.actionForm.ReaderTypeForm;
  6. public class ReaderTypeDAO {
  7.     private ConnDB conn=new ConnDB();
  8. //查询数据
  9. public Collection query(String strif){
  10.     ReaderTypeForm readerTypeForm=null;
  11.     Collection readerTypeColl=new ArrayList();
  12.     String sql="";
  13.     if(strif!="all" && strif!=null && strif!=""){
  14.         sql="select * from tb_readerType where "+strif+"";
  15.     }else{
  16.         sql="select * from tb_readerType";
  17.     }
  18.     ResultSet rs=conn.executeQuery(sql);
  19.     try {
  20.         while (rs.next()) {
  21.             readerTypeForm=new ReaderTypeForm();
  22.             readerTypeForm.setId(Integer.valueOf(rs.getString(1)));
  23.             readerTypeForm.setName(rs.getString(2));
  24.             readerTypeForm.setNumber(rs.getInt(3));
  25.             readerTypeColl.add(readerTypeForm);
  26.         }
  27.     } catch (SQLException ex) {
  28.     }
  29.     conn.close();
  30.     return readerTypeColl;
  31. }
  32. //用于修改的查询
  33. public ReaderTypeForm queryM(ReaderTypeForm readerTypeForm){
  34.     ReaderTypeForm readerTypeForm1=null;
  35.     String sql="select * from tb_readerType where id="+readerTypeForm.getId()+"";
  36.     System.out.println("修改时的SQL:"+sql);
  37.     ResultSet rs=conn.executeQuery(sql);
  38.     try {
  39.         while (rs.next()) {
  40.             readerTypeForm1=new ReaderTypeForm();
  41.             readerTypeForm1.setId(Integer.valueOf(rs.getString(1)));
  42.             readerTypeForm1.setName(rs.getString(2));
  43.             readerTypeForm1.setNumber(rs.getInt(3));
  44.         }
  45.     } catch (SQLException ex) {
  46.     }
  47.     conn.close();
  48.     return readerTypeForm1;
  49. }
  50. //添加数据
  51. public int insert(ReaderTypeForm readerTypeForm){
  52. String sql1="SELECT * FROM tb_readerType WHERE name='"+readerTypeForm.getName()+"'";
  53. ResultSet rs = conn.executeQuery(sql1);
  54. String sql = "";
  55. int falg = 0;
  56. try {
  57.     if (rs.next()) {
  58.         falg = 2;
  59.     } else {
  60.         sql ="Insert into tb_readerType (name,number) values('"+readerTypeForm.getName()+"',"+readerTypeForm.getNumber()+")";
  61.         falg = conn.executeUpdate(sql);
  62.         System.out.println("添加读者类型的SQL:" + sql);
  63.         conn.close();
  64.     }
  65. } catch (SQLException ex) {
  66.     falg = 0;
  67. }
  68. System.out.println("falg:"+falg);
  69. return falg;
  70. }
  71. //修改数据
  72. public int update(ReaderTypeForm readerTypeForm){
  73. String sql="Update tb_readerType set name='"+readerTypeForm.getName()+"',number="+readerTypeForm.getNumber()+" where id="+readerTypeForm.getId()+"";
  74. int falg=conn.executeUpdate(sql);
  75. System.out.println("修改数据时的SQL:"+sql);
  76. conn.close();
  77. return falg;
  78. }
  79. //删除数据
  80. public int delete(ReaderTypeForm readerTypeForm){
  81. String sql="Delete from tb_readerType where id="+readerTypeForm.getId()+"";
  82. int falg=conn.executeUpdate(sql);
  83. System.out.println("删除时的SQL:"+sql);
  84. return falg;
  85. }
  86. }