博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA获取MYSQL数据库表、字段、字段类型、字段注释
阅读量:5939 次
发布时间:2019-06-19

本文共 5389 字,大约阅读时间需要 17 分钟。

package com.lnjecit.generator.util;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.sql.*;import java.util.ArrayList;import java.util.List;public class DatabaseUtil {    private final static Logger LOGGER = LoggerFactory.getLogger(DatabaseUtil.class);    private static final String DRIVER = "com.mysql.jdbc.Driver";    private static final String URL = "jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEncoding=utf8";    private static final String USERNAME = "root";    private static final String PASSWORD = "mysql";    private static final String SQL = "SELECT * FROM ";// 数据库操作    static {        try {            Class.forName(DRIVER);        } catch (ClassNotFoundException e) {            LOGGER.error("can not load jdbc driver", e);        }    }    /**     * 获取数据库连接     *     * @return     */    public static Connection getConnection() {        Connection conn = null;        try {            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);        } catch (SQLException e) {            LOGGER.error("get connection failure", e);        }        return conn;    }    /**     * 关闭数据库连接     * @param conn     */    public static void closeConnection(Connection conn) {        if(conn != null) {            try {                conn.close();            } catch (SQLException e) {                LOGGER.error("close connection failure", e);            }        }    }    /**     * 获取数据库下的所有表名     */    public static List
getTableNames() { List
tableNames = new ArrayList<>(); Connection conn = getConnection(); ResultSet rs = null; try { //获取数据库的元数据 DatabaseMetaData db = conn.getMetaData(); //从元数据中获取到所有的表名 rs = db.getTables(null, null, null, new String[] { "TABLE" }); while(rs.next()) { tableNames.add(rs.getString(3)); } } catch (SQLException e) { LOGGER.error("getTableNames failure", e); } finally { try { rs.close(); closeConnection(conn); } catch (SQLException e) { LOGGER.error("close ResultSet failure", e); } } return tableNames; } /** * 获取表中所有字段名称 * @param tableName 表名 * @return */ public static List
getColumnNames(String tableName) { List
columnNames = new ArrayList<>(); //与数据库的连接 Connection conn = getConnection(); PreparedStatement pStemt = null; String tableSql = SQL + tableName; try { pStemt = conn.prepareStatement(tableSql); //结果集元数据 ResultSetMetaData rsmd = pStemt.getMetaData(); //表列数 int size = rsmd.getColumnCount(); for (int i = 0; i < size; i++) { columnNames.add(rsmd.getColumnName(i + 1)); } } catch (SQLException e) { LOGGER.error("getColumnNames failure", e); } finally { if (pStemt != null) { try { pStemt.close(); closeConnection(conn); } catch (SQLException e) { LOGGER.error("getColumnNames close pstem and connection failure", e); } } } return columnNames; } /** * 获取表中所有字段类型 * @param tableName * @return */ public static List
getColumnTypes(String tableName) { List
columnTypes = new ArrayList<>(); //与数据库的连接 Connection conn = getConnection(); PreparedStatement pStemt = null; String tableSql = SQL + tableName; try { pStemt = conn.prepareStatement(tableSql); //结果集元数据 ResultSetMetaData rsmd = pStemt.getMetaData(); //表列数 int size = rsmd.getColumnCount(); for (int i = 0; i < size; i++) { columnTypes.add(rsmd.getColumnTypeName(i + 1)); } } catch (SQLException e) { LOGGER.error("getColumnTypes failure", e); } finally { if (pStemt != null) { try { pStemt.close(); closeConnection(conn); } catch (SQLException e) { LOGGER.error("getColumnTypes close pstem and connection failure", e); } } } return columnTypes; } /** * 获取表中字段的所有注释 * @param tableName * @return */ public static List
getColumnComments(String tableName) { List
columnTypes = new ArrayList<>(); //与数据库的连接 Connection conn = getConnection(); PreparedStatement pStemt = null; String tableSql = SQL + tableName; List
columnComments = new ArrayList<>();//列名注释集合 ResultSet rs = null; try { pStemt = conn.prepareStatement(tableSql); rs = pStemt.executeQuery("show full columns from " + tableName); while (rs.next()) { columnComments.add(rs.getString("Comment")); } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); closeConnection(conn); } catch (SQLException e) { LOGGER.error("getColumnComments close ResultSet and connection failure", e); } } } return columnComments; } public static void main(String[] args) { List
tableNames = getTableNames(); System.out.println("tableNames:" + tableNames); for (String tableName : tableNames) { System.out.println("ColumnNames:" + getColumnNames(tableName)); System.out.println("ColumnTypes:" + getColumnTypes(tableName)); System.out.println("ColumnComments:" + getColumnComments(tableName)); } }}

 

转载于:https://www.cnblogs.com/sunBinary/p/10412622.html

你可能感兴趣的文章
grub resource>unknown filesystem异常处理
查看>>
ways of make process to background job
查看>>
【译】①JWS之Java[tm] Web Start开发者指南目录
查看>>
Oracle date 和 timestamp 区别
查看>>
backtrack笔记本无法用Fn调亮度
查看>>
20返回指针的函数与指向函数的指针
查看>>
域名无法加入域解决方法
查看>>
sql instr()与LOCATE()字符串查找函数
查看>>
linux查看内核版本、系统版本、系统位数(32or64)
查看>>
Hibernate:映射文件元素属性说明
查看>>
ethtool 命令详解
查看>>
函数作为变量,类型---golang
查看>>
运维自动化的哲学
查看>>
shell-while循环
查看>>
Flink State和容错机制
查看>>
粗略的看下两款Linux下的性能分析工具
查看>>
Eclipse中使用SVN
查看>>
Play 1.x框架学习之六:未登陆拦截机制 (Not Login intercept In play framework)
查看>>
常用命令-临时记录!
查看>>
表的垂直拆分和水平拆分
查看>>