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

Jsp/Servlet

开发平台:

Java

  1. package com.dao;
  2. import com.core.ConnDB;
  3. import java.sql.*;
  4. import java.util.*;
  5. import com.actionForm.BookTypeForm;
  6. public class BookTypeDAO {
  7.     private ConnDB conn=new ConnDB();
  8. //查询数据
  9. public Collection query(String strif){
  10.     BookTypeForm bookTypeForm=null;
  11.     Collection bookTypeColl=new ArrayList();
  12.     String sql="";
  13.     if(strif!="all" && strif!=null && strif!=""){
  14.         sql="select * from tb_bookType where "+strif+"";
  15.     }else{
  16.         sql="select * from tb_bookType";
  17.     }
  18.     ResultSet rs=conn.executeQuery(sql);
  19.     try {
  20.         while (rs.next()) {
  21.             bookTypeForm=new BookTypeForm();
  22.             bookTypeForm.setId(Integer.valueOf(rs.getString(1)));
  23.             bookTypeForm.setTypeName(rs.getString(2));
  24.             bookTypeForm.setDays(rs.getInt(3));
  25.             bookTypeColl.add(bookTypeForm);
  26.             System.out.print(bookTypeForm.getTypeName());
  27.         }
  28.     } catch (SQLException ex) {
  29.     }
  30.     conn.close();
  31.     return bookTypeColl;
  32. }
  33. //用于修改的查询
  34. public BookTypeForm queryM(BookTypeForm bookTypeForm){
  35.     BookTypeForm bookTypeForm1=null;
  36.     String sql="select * from tb_bookType where id="+bookTypeForm.getId()+"";
  37.     System.out.println("修改时的SQL:"+sql);
  38.     ResultSet rs=conn.executeQuery(sql);
  39.     try {
  40.         while (rs.next()) {
  41.             bookTypeForm1=new BookTypeForm();
  42.             bookTypeForm1.setId(Integer.valueOf(rs.getString(1)));
  43.             bookTypeForm1.setTypeName(rs.getString(2));
  44.             bookTypeForm1.setDays(rs.getInt(3));
  45.         }
  46.     } catch (SQLException ex) {
  47.     }
  48.     conn.close();
  49.     return bookTypeForm1;
  50. }
  51. //添加数据
  52. public int insert(BookTypeForm bookTypeForm){
  53. String sql1="SELECT * FROM tb_bookType WHERE typename='"+bookTypeForm.getTypeName()+"'";
  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_bookType (typename,days) values('"+bookTypeForm.getTypeName()+"',"+bookTypeForm.getDays()+")";
  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(BookTypeForm bookTypeForm){
  74. String sql="Update tb_bookType set typename='"+bookTypeForm.getTypeName()+"',days="+bookTypeForm.getDays()+" where id="+bookTypeForm.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(BookTypeForm bookTypeForm){
  82. String sql="Delete from tb_bookType where id="+bookTypeForm.getId()+"";
  83. int falg=conn.executeUpdate(sql);
  84. System.out.println("删除时的SQL:"+sql);
  85. return falg;
  86. }
  87. }