//https://www.baeldung.com/jdbc-database-metadata //https://docs.oracle.com/en/java/javase/11/docs/api/java.sql/java/sql/DatabaseMetaData.html /** * 获取某数据库中的表列表 涂聚文 Geovin Du geovindu * @param * @return TablesMetadata集合 * */ public ArrayList<TablesMetadata> selectTablesMetadata() { ArrayList<TablesMetadata> list=new ArrayList<TablesMetadata>(); TablesMetadata info=null; Connection connection=null; CallableStatement cstm =null; PreparedStatement pstmt = null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // connection.getSchema(); DatabaseMetaData databaseMetaData=connection.getMetaData(); String productName = databaseMetaData.getDatabaseProductName(); String productVersion = databaseMetaData.getDatabaseProductVersion(); String driverName = databaseMetaData.getDriverName(); String driverVersion = databaseMetaData.getDriverVersion(); boolean supportsFullOuterJoins = databaseMetaData.supportsFullOuterJoins(); boolean supportsStoredProcedures = databaseMetaData.supportsStoredProcedures(); boolean supportsTransactions = databaseMetaData.supportsTransactions(); boolean supportsBatchUpdates = databaseMetaData.supportsBatchUpdates(); String userName = databaseMetaData.getUserName(); /* catalog 包含目录名称的 String。对此参数提供 Null 值表示无需使用目录名称。 schema 包含架构名称模式的 String 值。对此参数提供 Null 值表示无需使用架构名称。 tableNamePattern 包含表名称模式的 String。 types 含有要包含的表类型的字符串数组。Null 表示应包含所有表类型。 * */ ResultSet resultSet = databaseMetaData.getTables(null, null, null, new String[]{"TABLE"}); while(resultSet.next()) { info=new TablesMetadata(); info.setTableName(resultSet.getString("TABLE_NAME")); info.setRemarks(resultSet.getString("REMARKS")); info.setTableSchem(resultSet.getString("TABLE_SCHEM")); info.setTableType(resultSet.getString("TABLE_TYPE")); list.add(info); } resultSet.close(); resultSet=null; connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return list; } /** * 返回某表的列属性列表 涂聚文 Geovin Du geovindu * @param TableName * @return 返回集合 * */ public ArrayList<ColumnsMetadata> selectColumnsMetadata(String TableName) { ArrayList<ColumnsMetadata> list=new ArrayList<ColumnsMetadata>(); ColumnsMetadata info=null; Connection connection=null; CallableStatement cstm =null; PreparedStatement pstmt = null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // connection.getSchema(); DatabaseMetaData databaseMetaData=connection.getMetaData(); ResultSet resultSet = databaseMetaData.getColumns(null,null, TableName, null); while(resultSet.next()) { info=new ColumnsMetadata(); info.setColumnName(resultSet.getString("COLUMN_NAME")); // String columnName = resultSet.getString("COLUMN_NAME"); info.setClumnSize(resultSet.getString("COLUMN_SIZE")); info.setDataType(resultSet.getString("DATA_TYPE")); info.setIsNullable(resultSet.getString("IS_NULLABLE")); info.setIsAutoincRement(resultSet.getString("IS_AUTOINCREMENT")); list.add(info); } // resultSet.close(); resultSet=null; // connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return list; } /** * 主键 * @param TableName * @return * * */ public ArrayList<ColumnsPrimaryKeys> selectColumnsPrimaryKeys(String TableName) { ArrayList<ColumnsPrimaryKeys> list=new ArrayList<ColumnsPrimaryKeys>(); ColumnsPrimaryKeys info=null; Connection connection=null; CallableStatement cstm =null; PreparedStatement pstmt = null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // connection.getSchema(); DatabaseMetaData databaseMetaData=connection.getMetaData(); ResultSet resultSet = databaseMetaData.getPrimaryKeys(null, null, TableName); while(resultSet.next()) { info=new ColumnsPrimaryKeys(); info.setColumnName(resultSet.getString("COLUMN_NAME")); info.setPkName(resultSet.getString("PK_NAME")); list.add(info); } resultSet.close(); resultSet=null; connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return list; } /** * 外键 * @param TableName * @return * * */ public ArrayList<ColumnsForeignKeys> selectColumnsForeignKeys(String TableName) { ArrayList<ColumnsForeignKeys> list=new ArrayList<ColumnsForeignKeys>(); ColumnsForeignKeys info=null; Connection connection=null; CallableStatement cstm =null; PreparedStatement pstmt = null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // connection.getSchema(); DatabaseMetaData databaseMetaData=connection.getMetaData(); ResultSet resultSet = databaseMetaData.getImportedKeys(null, null, TableName); while(resultSet.next()) { info=new ColumnsForeignKeys(); info.setPkTableName(resultSet.getString("PKTABLE_NAME")); info.setPkColumnName(resultSet.getString("PKCOLUMN_NAME")); info.setFkTableName(resultSet.getString("FKTABLE_NAME")); info.setFkColumnName(resultSet.getString("FKCOLUMN_NAME")); list.add(info); } resultSet.close(); resultSet=null; connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return list; } /** * * * */ public ArrayList<SchemasMetadata> selectSchemasMetadata() { ArrayList<SchemasMetadata> list=new ArrayList<SchemasMetadata>(); SchemasMetadata info=null; Connection connection=null; CallableStatement cstm =null; PreparedStatement pstmt = null; try { Class.forName(DRIVER_CLASS); } catch (ClassNotFoundException exception) { exception.printStackTrace(); } try { connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); // connection.getSchema(); DatabaseMetaData databaseMetaData=connection.getMetaData(); ResultSet resultSet = databaseMetaData.getSchemas(); while (resultSet.next()){ info=new SchemasMetadata(); System.out.println("Schem:"+resultSet.getString("TABLE_SCHEM")); info.setTableSchem(resultSet.getString("TABLE_SCHEM")); info.setTableCatalog(resultSet.getString("TABLE_CATALOG")); list.add(info); } // resultSet.close(); //resultSet=null; connection.close(); } catch (SQLException sqlException) { sqlException.printStackTrace(); } return list; }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc DuField.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * 数据库元数据的表属性列表 * @author geovindu * @version 1.0 * * */ public class TablesMetadata { //TABLE_SCHEM String=>表架构(可能为空) private String TableSchem; //TABLE_NAME String=>表名 private String TableName; //REMARKS String => 表格注释 private String Remarks; //TABLE_TYPE String =>表类, 典型的类型有"TABLE","VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY","LOCAL TEMPORARY", "ALIAS", "SYNONYM" private String TableType; //TYPE_CAT String => 目录类型(可能为空) private String TypeCat; //TYPE_SCHEM String => 架构类型(可能为空) private String TypeSchem; //TYPE_NAME String => 类型名(可能为空) private String TypeName; //SELF_REFERENCING_COL_NAME String => 类型表的指定“标识符”列的名称(可能为空) private String SelfReferncingColName; //REF_GENERATION String => 指定如何创建引用_col_name的inself_值。值为"SYSTEM", "USER", "DERIVED"(可能为空) private String RefGenearation; /** * * * */ public void setTableName(String tableName) { TableName = tableName; } /** * * * */ public void setRemarks(String remarks) { Remarks = remarks; } /** * * * */ public String getTableName() { return TableName; } /** * * * */ public String getRemarks() { return Remarks; } /** * * * */ public void setTableSchem(String tableSchem) { TableSchem = tableSchem; } /** * * * */ public String getTableSchem() { return TableSchem; } /** * * * */ public void setTypeSchem(String typeSchem) { TypeSchem = typeSchem; } /** * * * */ public void setTypeName(String typeName) { TypeName = typeName; } /** * * * */ public void setTypeCat(String typeCat) { TypeCat = typeCat; } /** * * * */ public void setTableType(String tableType) { TableType = tableType; } /** * * * */ public void setRefGenearation(String refGenearation) { RefGenearation = refGenearation; } /** * * * */ public void setSelfReferncingColName(String selfReferncingColName) { SelfReferncingColName = selfReferncingColName; } /** * * * */ public String getTypeSchem() { return TypeSchem; } /** * * * */ public String getTypeName() { return TypeName; } /** * * * */ public String getTypeCat() { return TypeCat; } /** * * * */ public String getTableType() { return TableType; } /** * * * */ public String getRefGenearation() { return RefGenearation; } /** * * * */ public String getSelfReferncingColName() { return SelfReferncingColName; } }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * * */ public class SchemasMetadata { //TABLE_SCHEM private String TableSchem; //TABLE_CATALOG private String TableCatalog; /** * * */ public void setTableCatalog(String tableCatalog) { TableCatalog = tableCatalog; } /** * * */ public void setTableSchem(String tableSchem) { TableSchem = tableSchem; } /** * * */ public String getTableSchem() { return TableSchem; } /** * * */ public String getTableCatalog() { return TableCatalog; } }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * * @author geovindu * @version 1.0 * */ public class ColumnsMetadata { //COLUMN_NAME private String ColumnName; //COLUMN_SIZE private String ClumnSize; //DATA_TYPE private String DataType; //IS_NULLABLE private String IsNullable; //IS_AUTOINCREMENT private String IsAutoincRement; /** * * * */ public void setColumnName(String columnName) { ColumnName = columnName; } /** * * * */ public void setClumnSize(String clumnSize) { ClumnSize = clumnSize; } /** * * * */ public void setDataType(String dataType) { DataType = dataType; } /** * * * */ public void setIsNullable(String isNullable) { IsNullable = isNullable; } /** * * * */ public void setIsAutoincRement(String isAutoincRement) { IsAutoincRement = isAutoincRement; } /** * * * */ public String getColumnName() { return ColumnName; } /** * * * */ public String getClumnSize() { return ClumnSize; } /** * * * */ public String getDataType() { return DataType; } /** * * * */ public String getIsNullable() { return IsNullable; } /** * * * */ public String getIsAutoincRement() { return IsAutoincRement; } }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * * * */ public class ColumnsPrimaryKeys { //COLUMN_NAME private String ColumnName; //PK_NAME private String PkName; /** * * * */ public void setColumnName(String columnName) { ColumnName = columnName; } /** * * * */ public void setPkName(String pkName) { PkName = pkName; } /** * * * */ public String getColumnName() { return ColumnName; } /** * * * */ public String getPkName() { return PkName; } }
/* * 版权所有 2021 涂聚文有限公司 * 许可信息查看: * 描述:实体类,连接MySQL * * 历史版本: JDK 14.02 * 数据库:My SQL 8.0 * IDE: IntelliJ IDEA 2021.2.3 * OS: Windows 10 x64 * 2021-12-12 创建者 geovindu * 2021-12-15 添加 Lambda * 2021-12-15 修改:date * 接口类 * 2021-12-15 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc BookKind.java * 配置文件: * driver=com.mysql.jdbc.Driver *url=jdbc\:mysql\://localhost\:3306/数据库名称 *user=root *password=root * * */ package Geovin.Model; /** * * * */ public class ColumnsForeignKeys { //PKTABLE_NAME private String PkTableName; //FKTABLE_NAME private String FkTableName; //PKCOLUMN_NAME private String PkColumnName; //FKCOLUMN_NAME private String FkColumnName; /** * * * */ public void setPkTableName(String pkTableName) { PkTableName = pkTableName; } /** * * * */ public void setPkColumnName(String pkColumnName) { PkColumnName = pkColumnName; } /** * * * */ public void setFkTableName(String fkTableName) { FkTableName = fkTableName; } /** * * * */ public void setFkColumnName(String fkColumnName) { FkColumnName = fkColumnName; } /** * * * */ public String getPkTableName() { return PkTableName; } /** * * * */ public String getPkColumnName() { return PkColumnName; } /** * * * */ public String getFkTableName() { return FkTableName; } /** * * * */ public String getFkColumnName() { return FkColumnName; } }