Mydb我的数据库功能包(自己写的)没有结束时,只有进行时

版权声明:转载请指明出处 https://blog.csdn.net/weixin_42321963/article/details/82468354

Mydb我的数据库功能包(自己写的)没有结束时,只有进行时。

尽量写的完美,一点点增加功能,借鉴网上,借鉴老师,借鉴他人

package com.xing;

import java.sql.*;

public class MyDb {
    private Connection conn;
    private String user;
    private String password;
    private PreparedStatement pst;
    private ResultSet rs;


    public MyDb() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/dt1?user=root&password=&serverTimezone=PRC&useSSL=false");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MyDb(String db, String user, String password) {
        this.user = user;
        this.password = password;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //String url = "jdbc:mysql://localhost/dt5?serverTimezone=PRC&useSSL=false";
            String url = "jdbc:mysql://localhost/" + db + "?serverTimezone=PRC&useSSL=false";
            conn = DriverManager.getConnection(url, this.user, this.password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 通用型方法,可以放置任何sql语句,只是会有很多限制,需要自己注意
     *
     * @param sql
     */
    public void common(String sql) {
        try {
            Statement st = conn.createStatement();
            st.execute(sql);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    /**
     * 用于创建数据库
     *
     * @param database
     */
    public void createdatabase(String database) {
        String sql = "create database if not exists  " + database + ";";
        try {
            Statement st = conn.createStatement();
            st.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 分页
     *
     * @param countNumber 数据数量总数
     * @param pageCount   每页的数量
     */
    public int page(int countNumber, int pageCount) {
        //得到总页数
        int pageMax = countNumber % pageCount == 0 ? countNumber / pageCount : (countNumber / pageCount + 1);
        return pageMax;
    }

    /**
     * 用来获取数据库中数据的总量
     *
     * @param table
     * @return
     */
    public int getCount(String table) {
        int count = 0;
        try {
            String sql = "select * from " + table;
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                count++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 用来查询信息
     * 返回的String[][]是所有的内容
     *
     * @param sql   sql:查询语句
     * @param paras paras:想要查询的变量
     *              paras[0]自动当成是数据库中的主键了
     */
    public String[][] selectInfo(String table, String sql, String... paras) {
        //得到数据库的总量
        int len1 = getCount(table);
        //得到每一个行的数据信息
        int len2 = paras.length;
        String[][] str = new String[len1][len2];
        System.out.println(len1);
        System.out.println(len2);
        try {
            //执行,向stu里面存值
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            int add = 0;
            String s = "";
            while (rs.next()) {
                for (int i = 0; i < len2; i++) {
                    s = paras[i];
                    str[add][i] = rs.getString(s);
                    // System.out.println(str[add][i]);
                }
                //add用来测验数组的长度设置的刚刚好
                add++;
                //System.out.println(add);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        //测验所得数组是否正确
      /*  for(int i=0;i<str.length;i++){
            for(int j=0;j<str[i].length;j++){
                System.out.print(str[i][j]+" ");
            }
            System.out.println();
        }*/
        return str;
    }
    /**
     * 用来查询信息
     * @param sql   sql:查询语句
     * @param paras paras:想要查询的变量
     */
     /*
    public void selectInfo(String sql, String... paras) {
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            int len = paras.length;
            while (rs.next()) {
                for (int i = 0; i < len; i++) {
                    System.out.print(rs.getString(paras[i]) + " ");
                }
                System.out.println();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    /**
     * 用来插入信息内容
     *
     * @param sql
     */
    public void insertInfo(String sql) {
        try {
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getConn() {
        return conn;
    }

    public String getUser() {
        return user;
    }

    public String getPassword() {
        return password;
    }

    public PreparedStatement getPst() {
        return pst;
    }

    public void setPst(PreparedStatement pst) {
        this.pst = pst;
    }

    public ResultSet getRs() {
        return rs;
    }

    public void setRs(ResultSet rs) {
        this.rs = rs;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42321963/article/details/82468354