//先获取jdbc连接
Connection connection = DriverManager.getConnection(JDBCUrl);
//从conn中获取数据库的元数据
DatabaseMetaData databaseMetaData = connection.getMetaData();
//库信息
//连接的库名
System.out.println(connection.getCatalog());
//数据库类型 MYSQL ORACLE
System.out.println(databaseMetaData.getDatabaseProductName());
//数据库版本号 8.0.15
System.out.println(databaseMetaData.getDatabaseProductVersion());
//数据库大版本 8
System.out.println(databaseMetaData.getDatabaseMajorVersion());
//jdbc连接的url
System.out.println(databaseMetaData.getURL());
System.out.println("数据库中使用的表类型");
String[] types = { "TABLE" };
//获取所有表
ResultSet rs = databaseMetaData.getTables(connection.getCatalog(), null, null, null);
while (rs.next()) {
String tableName = rs.getString("TABLE_NAME"); //表名
String tableType = rs.getString("TABLE_TYPE"); //表类型
String remarks = rs.getString("REMARKS"); //表备注
System.out.println("表名:"+tableName + " 表类型: " + tableType + " 表注释:" + remarks);
//表元数据
ResultSetMetaData resultSetMetaData = rs.getMetaData();
//遍历表所有元数据信息
for (int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
System.out.println(rs.getString(i)+"-==-"+ resultSetMetaData.getColumnName(i));
//表字段信息
ResultSet resultSet = databaseMetaData.getColumns(connection.getCatalog(),databaseMetaData.getUserName(),tableName,null);
while (resultSet.next()){
//遍历表字段所有元数据
for (int i = 0; i < resultSet.getMetaData().getColumnCount(); i++) {
System.out.println(resultSet.getMetaData().getColumnName(i+1)+":"+resultSet.getString(i+1));
System.out.println(resultSet.getString("TABLE_NAME")//表名
+"-"+resultSet.getString("column_name")//字段名
+"-"+resultSet.getString("TYPE_NAME")//字段类型
+"-"+resultSet.getString("DATA_TYPE")//字段类型
+"-"+resultSet.getString("COLUMN_SIZE")//长度
+"-"+resultSet.getString("DECIMAL_DIGITS")//长度
+"-"+resultSet.getString("COLUMN_DEF")//默认值
+"-"+resultSet.getString("REMARKS")//注释
+"-"+resultSet.getString("ORDINAL_POSITION")//字段位置
+"-"+resultSet.getString("IS_AUTOINCREMENT"));//是否自增
//获取表主键
ResultSet rs2 = connection.getMetaData().getPrimaryKeys(connection.getCatalog(), null, tableName);
ResultSetMetaData resultSetMetaData2 = rs2.getMetaData();
while (rs2.next()){
System.out.println("主键:"+rs2.getString("COLUMN_NAME"));