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

图形图象

开发平台:

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.     System.out.println("falg:"+falg);
  67.     return falg;
  68. }
  69.     //修改数据
  70.     public int update(BookCaseForm bookCaseForm){
  71.     String sql="Update tb_bookcase set name='"+bookCaseForm.getName()+"' where id="+bookCaseForm.getId()+"";
  72.     int falg=conn.executeUpdate(sql);
  73.     System.out.println("修改数据时的SQL:"+sql);
  74.     conn.close();
  75.     return falg;
  76. }
  77.     //删除数据
  78.     public int delete(BookCaseForm bookCaseForm){
  79.     String sql="Delete from tb_bookcase where id="+bookCaseForm.getId()+"";
  80.     int falg=conn.executeUpdate(sql);
  81.     System.out.println("删除时的SQL:"+sql);
  82.     return falg;
  83. }
  84. }