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