论文查询系统(二)

bean
package Bean;

public class bean {

     private int id;
     private String name;
     private String pdf;
     private String author;
     private String title;
     private String booktitle;
     private String date;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPdf() {
        return pdf;
    }
    public void setPdf(String pdf) {
        this.pdf = pdf;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getBooktitle() {
        return booktitle;
    }
    public void setBooktitle(String booktitle) {
        this.booktitle = booktitle;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public bean(int id,String name,String pdf,String author,String title,String booktitle,String date) {
        this.id=id;
        this.name=name;
        this.pdf=pdf;
        this.author=author;
        this.title=title;
        this.booktitle=booktitle;
        this.date=date;
    }
    public bean(String name,String pdf,String author,String title,String booktitle,String date) {
        this.name=name;
        this.pdf=pdf;
        this.author=author;
        this.title=title;
        this.booktitle=booktitle;
        this.date=date;
    }
}

Dao

package Dao;

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import DBUtil.DBUtil;

import Bean.bean;


public class Dao {

     public boolean insert(bean bean)
     {
           Connection conn = DBUtil.getConn();
           PreparedStatement pstmt = null;
           boolean f = false;
           int a=0;
           try {
               String sql = "insert into pachong(id,name,pdf,author,title,booktitle,date) value(?,?,?,?,?,?,?)";
               pstmt = conn.prepareStatement(sql);
               pstmt.setInt(1, bean.getId());
               pstmt.setString(2, bean.getName());
               pstmt.setString(3, bean.getPdf());
               pstmt.setString(4, bean.getAuthor());
               pstmt.setString(5, bean.getTitle());
               pstmt.setString(6, bean.getBooktitle());
               pstmt.setString(7, bean.getDate());
              a = pstmt.executeUpdate();
           }
           catch(SQLException e) {
               e.printStackTrace();
           }
           finally {
               DBUtil.close(pstmt, conn); 
           }
           if(a>0)
           f=true;
           
           return f;
     }
    public boolean delete(int id ) {//删除方法
        String sql="delete from pachong where id='"+id+"'";
         boolean f=false;
          Connection conn =DBUtil.getConn();
         Statement st=null;
         try {
             st=conn.createStatement();
              st.executeUpdate(sql);
              f=true;
         } catch (SQLException e) {
              // TODO Auto-generated catch block
             e.printStackTrace();
         }
         finally{
             DBUtil.close(st, conn);
          }
          return f;
     }
   
    public boolean update(bean bean) {
        String sql="update pachong set name='"+bean.getName()+"',pdf='"+bean.getPdf()+"',author='"+bean.getAuthor()+ "',title='"+bean.getTitle()+
                "',booktitle='"+bean.getBooktitle()+"',date='"+bean.getDate()+"'where id='"+bean.getId()+"'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 0;
    try {
        state = conn.createStatement();
        System.out.println("看看是不是执行了");
        a = state.executeUpdate(sql);
        System.out.println(a);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(state, conn);
    }
    
    if (a > 0) {
        f = true;
    }
    System.out.println(f);
    return f;
    }
    
    /*public List<bean> chaxun(String id){
        List<bean> list = new ArrayList<bean>();
        try {
            Connection conn = DBUtil.getConn();
            Statement state = null; 
          String sql="select * from pachong where id=?";
          PreparedStatement  pstmt = conn.prepareStatement(sql);
          pstmt.setString(1,id);
          ResultSet rs = pstmt.executeQuery();            
          System.out.println("Dao成功运行************************");
          while(rs.next()){
             
              lu.setId(rs.getInt("id"));
              lu.setName(rs.getString("name"));
              lu.setPdf(rs.getString("pdf"));
              lu.setAuthor(rs.getString("author"));
              lu.setTitle(rs.getString("title"));
              lu.setBooktitle(rs.getString("booktitle"));
              lu.setDate(rs.getString("date"));
              bean lu = new bean(id,name,pdf,author,title,booktitle,date);
              
             list.add(lu);
              
          }
          rs.close();
          pstmt.close();
          conn.close();
        }catch(SQLException e) {
            
              System.out.println("发生错误");
              e.printStackTrace();
            
            
        }
        
    
        return list;
        
    }*/
   
    /*public List<bean> chaxun() {
        String sql = "select * from pachong";
        List<bean> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            bean bean = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String pdf = rs.getString("pdf");
                String author = rs.getString("author");
                String title = rs.getString("title");
                String booktitle = rs.getString("booktitle");
                String date = rs.getString("date");
                bean = new bean(id,name, pdf, author ,title,booktitle,date);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }*/
    
    public List<bean> select(String minzi) {
         List<bean> list=new ArrayList<bean>(); 
            Connection con;
            try {
                String sql="select * from pachong where name = ?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,minzi );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     bean L = new bean(0, sql, sql, sql, sql, sql, sql);
                     int id = rs.getInt("id");
                     String name= rs.getString("name");
                     String pdf = rs.getString("pdf");
                     String author = rs.getString("author");
                     String title = rs.getString("title");
                     String booktitle = rs.getString("booktitle");
                     String date = rs.getString("date");
                    L=new bean(id,name,pdf,author,title,booktitle,date);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
     
     public List<bean> select1(String zz) {
         List<bean> list=new ArrayList<bean>(); 
            Connection con;
            try {
                String sql="select * from pachong where author = ?";
                con = DBUtil.getConn();
                PreparedStatement pa=con.prepareStatement(sql);
                pa.setString(1,zz );
                ResultSet rs=pa.executeQuery();
                while(rs.next()) {
                     bean L = new bean(0, sql, sql, sql, sql, sql, sql);
                     int id = rs.getInt("id");
                     String name= rs.getString("name");
                     String pdf = rs.getString("pdf");
                     String author = rs.getString("author");
                     String title = rs.getString("title");
                     String booktitle = rs.getString("booktitle");
                     String date = rs.getString("date");
                    L=new bean(id,name,pdf,author,title,booktitle,date);
                    list.add(L);
                    
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return list;
        }
     
    



    }
   
  
   
    
   

 

posted @ 2022-05-13 23:03  清梦韶华  阅读(14)  评论(0编辑  收藏  举报