学习进度条101

package Bean;

public class cvpr {
    private int id;
    private String title,authors,abstract_text,original_link,keywords,abstract_text1;
    private int year;

    public cvpr(int id, String title, String authors, String abstract_text, String original_link, String keywords, int year,String abstract_text1) {
        this.id = id;
        this.title = title;
        this.authors = authors;
        this.abstract_text = abstract_text;
        this.original_link = original_link;
        this.keywords = keywords;
        this.year = year;
        this.abstract_text1=abstract_text1;

    }

    public int getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

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

    public String getAuthors() {
        return authors;
    }

    public void setAuthors(String authors) {
        this.authors = authors;
    }

    public String getAbstract_text() {
        return abstract_text;
    }
    public String getOriginal_link() {
        return original_link;
    }

    public void setOriginal_link(String original_link) {
        this.original_link = original_link;
    }

    public String getKeywords() {
        return keywords;
    }

    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
    public String getAbstract_text1() {
        return abstract_text;
    }
}
package Bean;


import java.util.List;

public class pager<T> {
    //总条数
    //当前页
    //总页数
    //上一页
    //下一页
    //页大小
    private int totalCount;
    private int currentPage;
    private int totalPage;
    private boolean hasPrevPage;
    private boolean hasNextPage;
    private int pageSize;
    private List<T> data;

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        this.totalPage=(int) Math.ceil(((double)totalCount/pageSize));
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public boolean isHasPrevPage() {
        return hasPrevPage;
    }

    public void setHasPrevPage(boolean hasPrevPage) {
        this.hasPrevPage = hasPrevPage;
    }

    public boolean isHasNextPage() {
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
package Bean;
import java.sql.*;
//select  count(*)  from  papers
public class test {
    static String driver = "com.mysql.cj.jdbc.Driver";
    static String url = "jdbc:mysql://gongyunlong.mysql.rds.aliyuncs.com/cvpr1?&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";

    // 数据库的用户名与密码,需要根据自己的设置
    static String user = "g2431";
    static String password = "Gg12512544";
    static Connection con;
    public static int getcount()
    {
        int i=0;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();
            ResultSet rs = null;
            // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入???
            String sql = "select  count(*)  from  papers";
            PreparedStatement pst = con.prepareStatement(sql);//用来执行SQL语句查询,对sql语句进行预编译处理
            rs=pst.executeQuery();
            if(rs.next()) {
                i=rs.getInt(1);
                System.out.println("count:"+i);
            }
            pst.executeUpdate();
            stmt.executeUpdate(sql);
            con.close();
            System.out.println("insert success!");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return i;
    }
    public static int cx(cvpr[] cvprs,int page){
        int i=0;
        Connection con;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();

//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String page1=String.valueOf((page-1)*30);
            //String page2=String.valueOf((page-1)*30+30);
            String sql = "select id,title,authors,abstract_text,original_link,keywords,year from papers limit "+page1+","+30;
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                int id=rs.getInt("id");
                String title=rs.getNString("title");
                String authors=rs.getString("authors");
                String abstract_text=rs.getNString("abstract_text");
                String original_link=rs.getNString("original_link");
                String keywords=rs.getString("keywords");
                int year=rs.getInt("year");
                cvprs[i]=new cvpr(id,title,authors,abstract_text,original_link,keywords,year,abstract_text);
                i++;
            }
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return i;
    }
    public static cvpr cx(int id){
        cvpr c=null;
        int i=0;
        Connection con;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();

//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";

            String sql = "select id,title,authors,abstract_text,original_link,keywords,year from papers WHERE id= '"+id+"';";
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                int id1=rs.getInt("id");
                String title=rs.getNString("title");
                String authors=rs.getString("authors");
                String abstract_text=rs.getNString("abstract_text");
                String original_link=rs.getNString("original_link");
                String keywords=rs.getString("keywords");
                int year=rs.getInt("year");
                c=new cvpr(id,title,authors,abstract_text,original_link,keywords,year,abstract_text);
            }
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return c;
    }
    public static int cx(cvpr[] cvprs,int page,String s){
        String[] str=new String[100];
        int i=0;
        int si=0;
        for (String retval: s.split(" ")){
            str[si]=retval;
            si++;
        }
        Connection con;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();

//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String page1=String.valueOf((page-1)*30);
            //String page2=String.valueOf((page-1)*30+30);
            String sql = "select  *  from  papers where papers.keywords LIKE ";
            for(int k=0;k<si;k++)
            {
                str[k]=captureName(str[k]);
                sql=sql+"'%"+str[k]+"%'";
                if(k!=si-1)
                {
                    sql=sql+" or papers.keywords LIKE ";
                }
                else{
                    sql=sql+"limit "+page1+","+30+";";
                }
                System.out.println(sql);
            }

            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                int id=rs.getInt("id");
                String title=rs.getNString("title");
                String authors=rs.getString("authors");
                String abstract_text=rs.getNString("abstract_text");
                String original_link=rs.getNString("original_link");
                String keywords=rs.getString("keywords");
                int year=rs.getInt("year");
                cvprs[i]=new cvpr(id,title,authors,abstract_text,original_link,keywords,year,abstract_text);
                i++;
            }
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return i;
    }
    private static String captureName(String s) {
        // 进行字母的ascii编码前移,效率要高于截取字符串进行转换的操作
            if (Character.isUpperCase(s.charAt(0))) {
                return s;
            } else {
                return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
            }

    }
    public static int cx1(cvpr[] cvprs,int page){
        int i=0;
        Connection con;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();

//            String sql = "INSERT INTO tab1 (title,author,submission_date) VALUES ('li','xiao','2020-07028')";
            String page1=String.valueOf((page-1)*30);
            //String page2=String.valueOf((page-1)*30+30);
            String sql = "select id,title,authors,abstract_text,original_link,keywords,year from papers limit "+page1+","+30;
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()){
                int id=rs.getInt("id");
                String title=rs.getNString("title");
                String authors=rs.getString("authors");
                String abstract_text=rs.getNString("abstract_text");
                String original_link=rs.getNString("original_link");
                String keywords=rs.getString("keywords");
                int year=rs.getInt("year");
                cvprs[i]=new cvpr(id,title,authors,abstract_text,original_link,keywords,year,abstract_text);
                i++;
            }
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return i;
    }
    public static int getcount1(String s)
    {
        String[] str=new String[100];
        int si=0;
        for (String retval: s.split(" ")){
            str[si]=retval;
            si++;
        }
        int i=0;
        int j=0;
        try {
            //注册JDBC驱动程 序
            Class.forName(driver);
            //建立连接
            con = DriverManager.getConnection(url, user, password);
            if (!con.isClosed()) {
                System.out.println("数据库连接成功");
            }

            Statement stmt = con.createStatement();
            ResultSet rs = null;
            //System.out.println("select * from papers where papers.keywords LIKE '%Learning%' or papers.keywords LIKE '%Asking%';");
            // 必须要写入id值,否则插入错误,建立数据库时,不是设置id值自动,为何还必须写入???
            String sql = "select  count(*)  from  papers where papers.keywords LIKE ";
            for(int k=0;k<si;k++)
            {
                str[k]=captureName(str[k]);
                sql=sql+"'%"+str[k]+"%'";
                if(k!=si-1)
                {
                    sql=sql+" or papers.keywords LIKE ";
                }
                else{
                    sql=sql+";";
                }
                //System.out.println(sql);
            }
            PreparedStatement pst = con.prepareStatement(sql);//用来执行SQL语句查询,对sql语句进行预编译处理
            rs=pst.executeQuery();
            if(rs.next()) {
                i=rs.getInt(1);
                System.out.println("count:"+i);
            }
            pst.executeUpdate();
            stmt.executeUpdate(sql);
            con.close();
            System.out.println("insert success!");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动没有安装");

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
        return i;
    }
}

 

posted @ 2021-06-12 18:01  独倚高楼凭栏醉  阅读(30)  评论(0编辑  收藏  举报