BookTypeDAO.java~4~
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:3k
- package com.dao;
- import com.core.ConnDB;
- import java.sql.*;
- import java.util.*;
- import com.actionForm.BookTypeForm;
- public class BookTypeDAO {
- private ConnDB conn=new ConnDB();
- //查询数据
- public Collection query(String strif){
- BookTypeForm bookTypeForm=null;
- Collection bookTypeColl=new ArrayList();
- String sql="";
- if(strif!="all" && strif!=null && strif!=""){
- sql="select * from tb_bookType where "+strif+"";
- }else{
- sql="select * from tb_bookType";
- }
- ResultSet rs=conn.executeQuery(sql);
- try {
- while (rs.next()) {
- bookTypeForm=new BookTypeForm();
- bookTypeForm.setId(Integer.valueOf(rs.getString(1)));
- bookTypeForm.setCode(rs.getString(2));
- bookTypeForm.setTypeName(rs.getString(3));
- bookTypeForm.setDays(rs.getInt(4));
- bookTypeColl.add(bookTypeForm);
- }
- } catch (SQLException ex) {
- }
- conn.close();
- return bookTypeColl;
- }
- //用于修改的查询
- public BookTypeForm queryM(BookTypeForm bookTypeForm){
- BookTypeForm bookTypeForm1=null;
- String sql="select * from tb_bookType where id="+bookTypeForm.getId()+"";
- System.out.println("修改时的SQL:"+sql);
- ResultSet rs=conn.executeQuery(sql);
- try {
- while (rs.next()) {
- bookTypeForm1=new BookTypeForm();
- bookTypeForm1.setId(Integer.valueOf(rs.getString(1)));
- bookTypeForm1.setCode(rs.getString(2));
- bookTypeForm1.setTypeName(rs.getString(3));
- bookTypeForm1.setDays(rs.getInt(4));
- }
- } catch (SQLException ex) {
- }
- conn.close();
- return bookTypeForm1;
- }
- //添加数据
- public int insert(BookTypeForm bookTypeForm){
- String sql1="SELECT * FROM tb_bookType WHERE name='"+bookTypeForm.getName()+"'";
- ResultSet rs = conn.executeQuery(sql1);
- String sql = "";
- int falg = 0;
- try {
- if (rs.next()) {
- falg = 2;
- } else {
- sql ="Insert into tb_bookType (name,number) values('"+bookTypeForm.getName()+"',"+bookTypeForm.getNumber()+")";
- falg = conn.executeUpdate(sql);
- System.out.println("添加图书类型的SQL:" + sql);
- conn.close();
- }
- } catch (SQLException ex) {
- falg = 0;
- }
- System.out.println("falg:"+falg);
- return falg;
- }
- //修改数据
- public int update(BookTypeForm bookTypeForm){
- String sql="Update tb_bookType set name='"+bookTypeForm.getName()+"',number="+bookTypeForm.getNumber()+" where id="+bookTypeForm.getId()+"";
- int falg=conn.executeUpdate(sql);
- System.out.println("修改数据时的SQL:"+sql);
- conn.close();
- return falg;
- }
- //删除数据
- public int delete(BookTypeForm bookTypeForm){
- String sql="Delete from tb_bookType where id="+bookTypeForm.getId()+"";
- int falg=conn.executeUpdate(sql);
- System.out.println("删除时的SQL:"+sql);
- return falg;
- }
- }