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

Jsp/Servlet

开发平台:

Java

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