使用DatabaseMetaData接口查询GBase 8s数据库结构
Java程序中允许使用DatabaseMetaData接口查询数据库结构,以下是使用该接口查询GBase 8s数据库的示例代码。
更换驱动信息,同样适用于GBase 8t/IBM Informix。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | package testGBasedbt; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; public class TestDBMetadata { public static void main(String[] args) { testDBMetadata(); } public static void testDBMetadata() { String url = "jdbc:gbasedbt-sqli://192.168.80.100:9000/testdb:GBASEDBTSERVER=gbasedbt;DB_LOCALE=en_US.utf8;CLIENT_LOCALE=en_US.utf8;IFX_USE_STRENC=true" ; String user = "gbasedbt" ; String pass = "123456" ; try { Class.forName( "com.gbasedbt.jdbc.IfxDriver" ).newInstance(); Connection conn = DriverManager.getConnection(url, user, pass); DatabaseMetaData metadata = conn.getMetaData(); System.out.println( "数据库已知的用户: " + metadata.getUserName()); System.out.println( "数据库的系统函数的逗号分隔列表: " + metadata.getSystemFunctions()); System.out.println( "数据库的时间和日期函数的逗号分隔列表: " + metadata.getTimeDateFunctions()); System.out.println( "数据库的字符串函数的逗号分隔列表: " + metadata.getStringFunctions()); System.out.println( "数据库供应商用于 'schema'的首选术语: " + metadata.getSchemaTerm()); System.out.println( "数据库URL: " + metadata.getURL()); System.out.println( "是否允许只读: " + metadata.isReadOnly()); System.out.println( "数据库的产品名称: " + metadata.getDatabaseProductName()); System.out.println( "数据库的版本: " + metadata.getDatabaseProductVersion()); System.out.println( "驱动程序的名称: " + metadata.getDriverName()); System.out.println( "驱动程序的版本: " + metadata.getDriverVersion()); System.out.println(); System.out.println( "数据库中使用的表类型: " ); ResultSet rs = metadata.getTableTypes(); while (rs.next()) { System.out.println(rs.getString( 1 )); } rs.close(); System.out.println(); /* * 获取指定的数据库的所有表的类型, * getTables()的第一个参数就:数据库名称 ,第二个参数:schema,等效于表的所有者,第三个参数:表模式、表名称,可以使用表达式,第四个参数:数组类型,表的类型 */ System.out.println( "获取指定的数据库的所有表的类型" ); //ResultSet rs1 = metadata.getTables("testdb", null, null, null); String tmptype[] = { "TABLE" }; ResultSet rs1 = metadata.getTables( "testdb" , "gbasedbt" , "%1%" , tmptype); while (rs1.next()) { System.out.println( "数据库名: " + rs1.getString( 1 )); System.out.println( "表名 : " + rs1.getString( 3 )); System.out.println( "类型 : " + rs1.getString( 4 )); System.out.println(); } rs1.close(); System.out.println(); /* * 获取指定的数据库的表的主键, * getPrimaryKeys()的第一个参数:数据库名称,第二个参数:schema,等效于表的所有者,第三个参数:表名称 */ System.out.println( "获取指定的数据库的表的主键: " ); ResultSet rs2 = metadata.getPrimaryKeys( "testdb" , null , "t1" ); while (rs2.next()) { System.out.println( "主键字段名称: " + rs2.getString( 4 )); } rs2.close(); System.out.println(); /* * 获取指定的数据库的表的索引 * getIndexInfo()的第一个参数:数据库名称,第二个参数:schema,等效于表的所有者,第三个参数:表名称 * 第四个参数:是否仅返回唯一索引,第五个参数:是否返回准确结果 */ System.out.println( "获取指定的数据库的表的索引: " ); ResultSet rs3 = metadata.getIndexInfo( "testdb" , null , "t1" , false , true ); while (rs3.next()) { System.out.println( "数据库名: " + rs3.getString( 1 )); System.out.println( "表模式: " + rs3.getString( 2 )); System.out.println( "表名称: " + rs3.getString( 3 )); System.out.println( "索引值是否可以不唯一: " + rs3.getString( 4 )); System.out.println( "索引类别: " + rs3.getString( 5 )); System.out.println( "索引名称: " + rs3.getString( 6 )); System.out.println( "索引类型: " + rs3.getString( 7 )); System.out.println( "索引中的列序列号: " + rs3.getString( 8 )); System.out.println( "列名称: " + rs3.getString( 9 )); System.out.println( "列排序序列: " + rs3.getString( 10 )); System.out.println( "TYPE为 tableIndexStatistic时它是表中的行数否则它是索引中唯一值的数量: " + rs3.getString( 11 )); System.out.println( "TYPE为 tableIndexStatisic时它是用于表的页数否则它是用于当前索引的页数: " + rs3.getString( 12 )); System.out.println( "过滤器条件: " + rs3.getString( 13 )); System.out.println(); } rs3.close(); } catch (Exception e) { e.printStackTrace(); } } } |
运行结果类似以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 数据库已知的用户: gbasedbt 数据库的系统函数的逗号分隔列表: avg,max,min,sum,count,range,stdev,variance,trim,hex,filetoblob,filetoclob,lotofile,lotocopy 数据库的时间和日期函数的逗号分隔列表: date,day,month,weekday,year,extend,mdy 数据库的字符串函数的逗号分隔列表: trunc,length 数据库供应商用于 'schema'的首选术语: user 数据库URL: jdbc:gbasedbt-sqli://192.168.80.100:9000/testdb:GBASEDBTSERVER=gbasedbt;DB_LOCALE=en_US.utf8;CLIENT_LOCALE=en_US.utf8;IFX_USE_STRENC=true 是否允许只读: false 数据库的产品名称: GBase Server 数据库的版本: 12.10.FC4G1AEE 驱动程序的名称: GBase JDBC Driver for GBase Server 驱动程序的版本: 4.10.JC4G1N999 数据库中使用的表类型: SYSTEM TABLE TABLE VIEW 获取指定的数据库的所有表的类型 数据库名: testdb 表名 : t1 类型 : TABLE 获取指定的数据库的表的主键: 主键字段名称: col1 获取指定的数据库的表的索引: 数据库名: testdb 表模式: gbasedbt 表名称: t1 索引值是否可以不唯一: 0 索引类别: gbasedbt 索引名称: 100_3 索引类型: 3 索引中的列序列号: 1 列名称: col1 列排序序列: null TYPE为 tableIndexStatistic时它是表中的行数否则它是索引中唯一值的数量: 0 TYPE为 tableIndexStatisic时它是用于表的页数否则它是用于当前索引的页数: 0 过滤器条件: null 数据库名: testdb 表模式: gbasedbt 表名称: t1 索引值是否可以不唯一: 1 索引类别: gbasedbt 索引名称: ix_t1 索引类型: 3 索引中的列序列号: 1 列名称: col2 列排序序列: null TYPE为 tableIndexStatistic时它是表中的行数否则它是索引中唯一值的数量: 0 TYPE为 tableIndexStatisic时它是用于表的页数否则它是用于当前索引的页数: 0 过滤器条件: null |
- 上一篇: GBase 8t数据库SQL优化一例
- 下一篇: GBase 8T数据库存储过程使用出参示例