介绍说明
Java,JDBC中通过MetaData来获取具体的表的相关信息,查询数据库中有哪些表,表有哪些字段,字段的属性等,MetaData中通过一系列getXXX函数,将这些信息存放到ResultSet里面,然后返回。
代码共享
<!-- Oracle 11G release --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency>
package com.w; import java.; import java.; import java.; public final class JDBCHelper { /** * @param driverName * @param url * @param user * @param password * @return */ public static Connection createConnection(String driverName, String url, String user, String password) { Connection connection = null; try { Cla(driverName); connection = DriverManager.getConnection(url, user, password); } catch (Exception e) { e.printStackTrace(); } return connection; } /** * @param connection */ public static void close(Connection connection) { if (connection != null) { try { connec(); } catch (SQLException e) { e.printStackTrace(); } connection = null; } } }
import com.w.JDBCHelper; import java.; import java.; import java.; import java.; import java.u; import java.u; public class Demo02Main { public static void main(String[] args) throws SQLException { String driverName = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@110.42.135.194:1521:orcl"; String user = "saveinterface_test"; String password = "pass#word1"; Connection connection = JDBCHel(driverName, url, user, password); // 获取表名称 List<Object> objectList = getAllTableName(connection); for (Object object : objectList) { String tableName = S(object); Sy("表==>" + tableName); } // 某个表的列描述 tableNameForColumn(connection, "DP_ADDRESS"); // 某个表的主键 tableNameForPrimary(connection, "DP_ADDRESS"); // 某个表的外键 tableNameForForeginKey(connection, "DP_ADDRESS"); } /** * @param connection * @return * @throws SQLException */ public static List getAllTableName(Connection connection) throws SQLException { DatabaseMetaData dbMetaData = connec(); // 只要表 String[] types = {"TABLE"}; String catalog = connec(); ResultSet tabs = dbMe(catalog, null, null, types); List<Object> tables = new ArrayList<Object>(); while ()) { ("TABLE_NAME")); } return tables; } /** * @param connection * @param tableName * @throws SQLException */ private static void tableNameForColumn(Connection connection, String tableName) throws SQLException { DatabaseMetaData dbMetaData = connec(); String catalog = connec(); ResultSet colRet = dbMe(catalog, "%", tableName, "%"); while ()) { String columnName = colRet.getString("COLUMN_NAME"); String columnType = colRet.getString("TYPE_NAME"); int dataSize = colRet.getInt("COLUMN_SIZE"); int digits = colRet.getInt("DECIMAL_DIGITS"); int nullable = colRet.getInt("NULLABLE"); // Sy("名称<列名称>:" + columnName + "\t"); Sy("数据类型<类型名称>:" + columnType + "\t"); Sy("长度/集合<列的大小>:" + dataSize + "\t"); Sy("小数部分的位数:" + digits + "\t"); Sy("允许NULL<是否允许使用 NULL>:" + nullable + "\t"); Sy(); } } /** * @param connection * @param tableName * @throws SQLException */ private static void tableNameForPrimary(Connection connection, String tableName) throws SQLException { DatabaseMetaData dbMetaData = connec(); String catalog = connec(); ResultSet primaryKeyResultSet = dbMe(catalog, null, tableName); while ()) { String primaryKeyColumnName = ("COLUMN_NAME"); Sy("列名称:" + primaryKeyColumnName + "\t"); String PK_NAME = ("PK_NAME"); Sy("主键的名称:" + PK_NAME + "\t"); String KEY_SEQ = ("KEY_SEQ"); Sy("主键中的序列号:" + KEY_SEQ + "\t"); Sy(); } } /** * @param connection * @param tableName * @throws SQLException */ private static void tableNameForForeginKey(Connection connection, String tableName) throws SQLException { DatabaseMetaData dbMetaData = connec(); String catalog = connec(); ResultSet foreignKeyResultSet = dbMe(catalog, null, tableName); while ()) { String fkColumnName = ("FKCOLUMN_NAM"); String pkTablenName = ("PKTABLE_NAME"); String pkColumnName = ("PKCOLUMN_NAME"); Sy("列名称:" + fkColumnName + "\t"); Sy("表名称:" + pkTablenName + "\t"); Sy("列名称:" + pkColumnName + "\t"); Sy(); } } }