900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长

如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长

时间:2019-03-20 00:50:26

相关推荐

如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长

* 如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长度等等)

* 1,表字段名称

* 2,表字段类型

* 3,表字段注释信息

这里介绍3种方式,如下:

第一种方式:执行sql语句获取 select * from user_pop_info where 1 = 2

第二种方式:执行sql语句获取 show create table user_pop_info

第二种方式:直接从jdbc数据库连接Connection实例中获取

三种方式获取的数据有一些区别

第一种方式不能获取到的信息比较丰富,但是唯一不能获取的是表字段备注信息,其他信息基本都有了

第二种方式可以获取完整的建表语句,但是不太好用,需要自己解析表字段,如果自己可以解析的话,建议使用

第三种方式能够获取到表字段备注信息,但是获取不到表字段对应的java类型

do not talk,show me code.

package com.yangcq.learning.hantang.learning;import com.baomidou.mybatisplus.generator.config.DataSourceConfig;import lombok.extern.slf4j.Slf4j;import java.sql.*;/*** 如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长度等等)* 1,表字段名称* 2,表字段类型* 3,表字段注释信息*/@Slf4jpublic class How2ObtainFieldInfoFromJdbc {private static Connection connection;public static void main(String[] args) {How2ObtainFieldInfoFromJdbc how2ObtainFieldInfoFromJdbc = new How2ObtainFieldInfoFromJdbc();// 第一种方式:执行sql语句获取 select * from user_pop_info where 1 = 2how2ObtainFieldInfoFromJdbc.method1();// 第二种方式:执行sql语句获取 show create table user_pop_infohow2ObtainFieldInfoFromJdbc.method2();// 第二种方式:直接从jdbc数据库连接Connection实例中获取how2ObtainFieldInfoFromJdbc.method3();}private void method1() {try{PreparedStatement preparedStatement = connection.prepareStatement("select * from user_pop_info where 1 = 2");ResultSetMetaData resultSetMetaData = preparedStatement.executeQuery().getMetaData();for (int i = 0; i < resultSetMetaData.getColumnCount(); i++) {log.info("数据库实例名:{}", resultSetMetaData.getCatalogName(i + 1));log.info("表名:{}", resultSetMetaData.getTableName(i + 1));log.info("java类型:{}", resultSetMetaData.getColumnClassName(i + 1));log.info("数据库类型:{}", resultSetMetaData.getColumnTypeName(i + 1));log.info("字段名称:{}", resultSetMetaData.getColumnName(i + 1));log.info("字段长度:{}", resultSetMetaData.getColumnDisplaySize(i + 1));log.info("getColumnType:{}", resultSetMetaData.getColumnType(i + 1));log.info("getPrecision:{}", resultSetMetaData.getPrecision(i + 1));log.info("getScale:{}", resultSetMetaData.getScale(i + 1));log.info("getSchemaName:{}", resultSetMetaData.getSchemaName(i + 1));log.info("getScale:{}", resultSetMetaData.getScale(i + 1));}} catch (Exception e) {log.error("method1 error ", e);}}private void method2() {try{PreparedStatement preparedStatement2 = connection.prepareStatement("show create table user_pop_info");ResultSet resultSet2 = preparedStatement2.executeQuery();while(resultSet2.next()) {String tableName = resultSet2.getString("Table");String createTable = resultSet2.getString("Create Table");log.info("tableName:{}", tableName);log.info("createTable:");System.out.println(createTable);}} catch (Exception e) {log.error("method2 error ", e);}}private void method3() {try{DatabaseMetaData databaseMetaData = connection.getMetaData();// 获取所有表ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"});// 获取指定表ResultSet specificResultSet = databaseMetaData.getColumns(null, "%", "user_pop_info", "%");String columnName2;String columnType2;String comment2;while(specificResultSet.next()) {columnName2 = specificResultSet.getString("COLUMN_NAME");columnType2 = specificResultSet.getString("TYPE_NAME");comment2 = specificResultSet.getString("REMARKS");log.info("COLUMN_NAME:{}", columnName2);log.info("TYPE_NAME:{}", columnType2);log.info("REMARKS:{}", comment2);}} catch (Exception e) {log.error("method3 error ", e);}}static {try{// 数据源配置DataSourceConfig dataSourceConfig = new DataSourceConfig();dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/databasename?serverTimezone=GMT%2b8&Unicode=true&characterEncoding=utf8");dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");dataSourceConfig.setUsername("root");dataSourceConfig.setPassword("1234567");connection = dataSourceConfig.getConn();} catch (Exception e) {log.error("autoCodeGeneratorProcess error ", e);}}}

如何从jdbc中获取数据库建表语句信息(表字段名称/表字段类型/表字段注释信息/表字段长度等等)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。