BookTypeDAO.java~11~
上传用户: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.         }
  27.     } catch (SQLException ex) {
  28.     }
  29.     conn.close();
  30.     return bookTypeColl;
  31. }
  32. //用于修改的查询
  33. public BookTypeForm queryM(BookTypeForm bookTypeForm){
  34.     BookTypeForm bookTypeForm1=null;
  35.     String sql="select * from tb_bookType where id="+bookTypeForm.getId()+"";
  36.     System.out.println("修改时的SQL:"+sql);
  37.     ResultSet rs=conn.executeQuery(sql);
  38.     try {
  39.         while (rs.next()) {
  40.             bookTypeForm1=new BookTypeForm();
  41.             bookTypeForm1.setId(Integer.valueOf(rs.getString(1)));
  42.             bookTypeForm1.setTypeName(rs.getString(2));
  43.             bookTypeForm1.setDays(rs.getInt(3));
  44.         }
  45.     } catch (SQLException ex) {
  46.     }
  47.     conn.close();
  48.     return bookTypeForm1;
  49. }
  50. //添加数据
  51. public int insert(BookTypeForm bookTypeForm){
  52. String sql1="SELECT * FROM tb_bookType WHERE typename='"+bookTypeForm.getTypeName()+"'";
  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_bookType (typename,days) values('"+bookTypeForm.getTypeName()+"',"+bookTypeForm.getDays()+")";
  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(BookTypeForm bookTypeForm){
  73. String sql="Update tb_bookType set typename='"+bookTypeForm.getTypeName()+"',days="+bookTypeForm.getDays()+" where id="+bookTypeForm.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(BookTypeForm bookTypeForm){
  81. String sql="Delete from tb_bookType where id="+bookTypeForm.getId()+"";
  82. int falg=conn.executeUpdate(sql);
  83. System.out.println("删除时的SQL:"+sql);
  84. return falg;
  85. }
  86. }