how to get list of Databases “Schema” names of MySql using java JDBC

后端 未结 4 839
误落风尘
误落风尘 2020-11-29 09:25

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

4条回答
  •  攒了一身酷
    2020-11-29 10:16

    The getSchemas() method of the DatabaseMetaData is the obvious but with MySQL you have to use 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") );
    }
    

提交回复
热议问题