简体   繁体   English

如何使用java JDBC获取MySql的数据库“架构”名称列表

[英]how to get list of Databases "Schema" names of MySql using java JDBC

如何使用java JDBC获取MySql的数据库“架构”名称列表?

The getSchemas() method of the DatabaseMetaData is the obvious but with MySQL you have to use getCatalogs() DatabaseMetaData 的getSchemas()方法是显而易见的,但是对于 MySQL,您必须使用getCatalogs()

http://download.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getSchemas() http://download.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getCatalogs() http://download.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getSchemas() http://download.oracle.com/javase/7/docs/api/java/sql /DatabaseMetaData.html#getCatalogs()

Example:例子:

Class.forName("com.mysql.jdbc.Driver");

// change user and password as you need it
Connection con = DriverManager.getConnection (connectionURL, "user", "password");

ResultSet rs = con.getMetaData().getCatalogs();

while (rs.next()) {
    System.out.println("TABLE_CAT = " + rs.getString("TABLE_CAT") );
}
  • Either use SHOW DATABASES to see if it is inside,要么使用SHOW DATABASES查看它是否在里面,
  • Check the INFORMATION_SCHEMA,检查 INFORMATION_SCHEMA,
  • or just do USE DATABASE;或者只是USE DATABASE; and catch the error.并捕获错误。
DatabaseMetaData meta = conn.getMetaData();
ResultSet schemas = meta.getSchemas();
while (schemas.next()) {
  String tableSchema = schemas.getString(1);    // "TABLE_SCHEM"
  String tableCatalog = schemas.getString(2); //"TABLE_CATALOG"
  System.out.println("tableSchema "+tableSchema);
}
DatabaseMetaData dbmd = con.getMetaData();
ResultSet ctlgs = dbmd.getCatalogs();
while(ctlgs.next())
{
System.out.println("ctlgs  =  "+ctlgs.getString(1));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM