论文查询系统(一)

 

 

 

 bean.java

package Bean;

public class bean {
    private String id;
    private String name;
    private String title;
    private String booktitle;
    private String author;
    private String pdf;
    private String date;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPdf() {
        return pdf;
    }

    public void setPdf(String pdf) {
        this.pdf = pdf;
    }

    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;
    }
}

dao.java

package Dao;
import Bean.bean;
import DBUtil.dbutil;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;

public class dao {

    public List<bean> searchBytitle(String title){
        List<bean> list = new ArrayList<bean>();
        try {
            Connection conn = dbutil.getConn();
            Statement state = null;
            String sql="select * from test1 where title REGEXP '"+title+"'";
            PreparedStatement  pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            System.out.println("以title为搜索对象运行中");
            while(rs.next()){
                bean lu = new bean();

                lu.setId(rs.getString("id"));
                lu.setAuthor(rs.getString("author"));
                lu.setTitle(rs.getString("title"));
                lu.setPdf(rs.getString("pdf"));
                lu.setBooktitle(rs.getString("booktitle"));
                lu.setDate(rs.getString("date"));


                list.add(lu);

            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch(SQLException e) {

            System.out.println("发生错误");
            e.printStackTrace();


        }


        return list;

    }


    public List<bean> search(String author,String title){
        List<bean> list = new ArrayList<bean>();
        try {
            Connection conn = dbutil.getConn();
            Statement state = null;



            String sql = "select * from test1 where title REGEXP '"+title+"' and author REGEXP '"+author+"'";

            PreparedStatement  pstmt = conn.prepareStatement(sql);

            ResultSet rs = pstmt.executeQuery();
            System.out.println("多项条件搜索运行中");
            while(rs.next()){

                bean lu = new bean();

                lu.setId(rs.getString("id"));
                lu.setAuthor(rs.getString("author"));
                lu.setTitle(rs.getString("title"));
                lu.setPdf(rs.getString("pdf"));
                lu.setBooktitle(rs.getString("booktitle"));
                lu.setDate(rs.getString("date"));

                list.add(lu);

            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch(SQLException e) {

            System.out.println("发生错误");
            e.printStackTrace();


        }


        return list;

    }

    public static bean getbean(String id){
        bean lu = new bean();
        try {
            Connection conn = dbutil.getConn();
            Statement state = null;
            String sql="select * from test1 where id=?";
            PreparedStatement  pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,id);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()){

                lu.setId(rs.getString("id"));
                lu.setAuthor(rs.getString("author"));
                lu.setTitle(rs.getString("title"));
                lu.setPdf(rs.getString("pdf"));
                lu.setBooktitle(rs.getString("booktitle"));
                lu.setDate(rs.getString("date"));

            }
            rs.close();
            pstmt.close();
            conn.close();
        }
        catch(SQLException e) {

            e.printStackTrace();
        }
        return lu;

    }




    public boolean delete(String  id) {
        boolean flag = false;
        Connection conn = dbutil.getConn();
        Statement state = null;

        try {
            String sql = "delete from test1 where id = '"+id+"'";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            int i = pstmt.executeUpdate();
            pstmt.close();
            conn.close();
            if(i>0) flag = true;
        } catch (SQLException e) {
            System.out.println("删除失败!");
            e.printStackTrace();
        }

        return flag;
    }


    public static void update(String title, String author, String date, String pdf, String id) {
        try {
            PreparedStatement preparedStatement = null;
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/py?&useSSL=false&serverTimezone=UTC", "root", "123456");
            preparedStatement = connect.prepareStatement("Update test1 set title=?,author=?,date=? ,pdf=? where id=?");
            preparedStatement.setString(1, title);
            preparedStatement.setString(2, author);
            preparedStatement.setString(3, date);
            preparedStatement.setString(4, pdf);
            preparedStatement.setString(5, id);
            preparedStatement.executeUpdate();
            preparedStatement.execute();
            connect.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static boolean add(String title, String author, String date, String pdf)
    {
        boolean flag=false;
        try {
            PreparedStatement preparedStatement=null;
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/py?&useSSL=false&serverTimezone=UTC","root","123456");
            preparedStatement=connect.prepareStatement("insert into test1 (title,author,date,pdf) values (?,?,?,?)");
            preparedStatement.setString(1,title);
            preparedStatement.setString(2,author);
            preparedStatement.setString(3,date);
            preparedStatement.setString(4,pdf);

            preparedStatement.executeUpdate();
            connect.close();
            preparedStatement.close();
            flag=true;
        }catch(SQLException e){
            e.printStackTrace();
        }
        return flag;
    }

}

dbutil.java

package DBUtil;
import java.sql.*;
public class dbutil {


    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; //数据库驱动名
    static final String url = "jdbc:mysql://localhost:3306/py";//数据库地址
    static final String user = "root";
    static final String password = "123456";



    //连接数据库
    public static Connection getConn () {
        Connection conn = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }


    //关闭连接
    public static void close (PreparedStatement preparedState, Connection conn) {
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //测试是否连接成功
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        String sql ="select * from lunwen";//数据库名称
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        if(rs.next()){
            System.out.println("数据库不为空");
        }
        else{
            System.out.println("数据库为空");
        }
    }







}

 

posted @ 2022-05-14 16:29  zrswheart  阅读(57)  评论(0编辑  收藏  举报