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

时间:2022-02-19 07:28:05

how to get list of Databases "Schema" names of MySql using java JDBC ?

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

4 个解决方案

#1


18  

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") );
}

#2


4  

  • Either use SHOW DATABASES to see if it is inside,
  • 使用SHOW DATABASES来查看它是否在里面,
  • Check the INFORMATION_SCHEMA,
  • 检查INFORMATION_SCHEMA,
  • or just do USE DATABASE; and catch the error.
  • 或者只是使用数据库;并抓住错误。

#3


2  

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);
}

Hope this help

希望这有帮助

#4


0  

DatabaseMetaData dbmd = con.getMetaData();
ResultSet ctlgs = dbmd.getCatalogs();
while(ctlgs.next())
{
System.out.println("ctlgs  =  "+ctlgs.getString(1));
}

#1


18  

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") );
}

#2


4  

  • Either use SHOW DATABASES to see if it is inside,
  • 使用SHOW DATABASES来查看它是否在里面,
  • Check the INFORMATION_SCHEMA,
  • 检查INFORMATION_SCHEMA,
  • or just do USE DATABASE; and catch the error.
  • 或者只是使用数据库;并抓住错误。

#3


2  

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);
}

Hope this help

希望这有帮助

#4


0  

DatabaseMetaData dbmd = con.getMetaData();
ResultSet ctlgs = dbmd.getCatalogs();
while(ctlgs.next())
{
System.out.println("ctlgs  =  "+ctlgs.getString(1));
}