ReaderTypeDAO.java
上传用户:ycdfsj258
上传日期:2022-08-04
资源大小:1094k
文件大小:3k
源码类别:

图形图象

开发平台:

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.             System.out.println(rs.getInt(3));
  45.         }
  46.     } catch (SQLException ex) {
  47.     }
  48.     conn.close();
  49.     return readerTypeForm1;
  50. }
  51. //添加数据
  52. public int insert(ReaderTypeForm readerTypeForm){
  53. String sql1="SELECT * FROM tb_readerType WHERE name='"+readerTypeForm.getName()+"'";
  54. ResultSet rs = conn.executeQuery(sql1);
  55. String sql = "";
  56. int falg = 0;
  57. try {
  58.     if (rs.next()) {
  59.         falg = 2;
  60.     } else {
  61.         sql ="Insert into tb_readerType (name,number) values('"+readerTypeForm.getName()+"',"+readerTypeForm.getNumber()+")";
  62.         falg = conn.executeUpdate(sql);
  63.         System.out.println("添加读者类型的SQL:" + sql);
  64.         conn.close();
  65.     }
  66. } catch (SQLException ex) {
  67.     falg = 0;
  68. }
  69. System.out.println("falg:"+falg);
  70. return falg;
  71. }
  72. //修改数据
  73. public int update(ReaderTypeForm readerTypeForm){
  74. String sql="Update tb_readerType set name='"+readerTypeForm.getName()+"',number="+readerTypeForm.getNumber()+" where id="+readerTypeForm.getId()+"";
  75. int falg=conn.executeUpdate(sql);
  76. System.out.println("修改数据时的SQL:"+sql);
  77. conn.close();
  78. return falg;
  79. }
  80. //删除数据
  81. public int delete(ReaderTypeForm readerTypeForm){
  82. String sql="Delete from tb_readerType where id="+readerTypeForm.getId()+"";
  83. int falg=conn.executeUpdate(sql);
  84. System.out.println("删除时的SQL:"+sql);
  85. return falg;
  86. }
  87. }