• java mysql jdbc连接数据库


    1.获取一个连接

    /**
         * 获取数据库连接
         * @author songmin
         * @param generateData:
         * @date 2021/6/12 14:57
         * @return: java.sql.Connection
         */
        public static Connection getConnection(GenerateData generateData){
            String url = generateData.getDbUrl() ;
            String username = generateData.getDbUserName();
            String password = generateData.getDbPassword();
            Connection con = null;
            try{ con = DriverManager.getConnection(url , username , password ) ;
            }catch(SQLException se){
                System.out.println("数据库连接失败!");
                se.printStackTrace() ;
            }
            return con;
        }

    2.获取连接之后查询

    /**
         * 获取所有表
         * @author songmin
         * @param generateData:
         * @date 2021/6/12 15:07
         * @return: java.lang.String
         */
        public static Set<String> getAllTable(GenerateData generateData){
            Connection conn = getConnection(generateData);
            PreparedStatement pstmt = null;
            String sql = "select table_name from information_schema.tables where table_schema=?";
            Set<String> tableNames = new HashSet<>();
            try {
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,generateData.getDbName());
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()){
                     tableNames.add(rs.getString("TABLE_NAME"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                close(pstmt,conn);
            }
            return tableNames;
        }

    3.关闭连接的方法

    /**
         * 关闭连接
         * @author songmin
         * @param statement:
         * @param conn:
         * @date 2021/6/12 15:08
         * @return: void
         */
        public static void close(Statement statement, Connection conn) {
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

     4.功能测试

    public static void main(String[] args) {
            GenerateData generateData = new GenerateData();
            generateData.setDbName("generate_code_test");
            generateData.setDbUrl("jdbc:mysql://localhost:3306/generate_code_test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useAffectedRows=true");
            generateData.setDbUserName("root");
            generateData.setDbPassword("123456");
            Set<String> tables = getAllTable(generateData);
            for (String str : tables) {
                logger.info("表名 {}",str);
            }
        }
  • 相关阅读:
    Python全栈开发:socket
    Python全栈开发:线程、进程和协程
    Python全栈开发:基本数据类型
    Python全栈开发:运算符
    使用pyplot和seaborn进行画图
    数据预处理之缺失值的处理
    数据预处理之标准化
    数据预处理之离散化
    集成学习中的 stacking 以及python实现
    使用sklearn进行交叉验证
  • 原文地址:https://www.cnblogs.com/ushowtime/p/14880544.html
Copyright © 2020-2023  润新知