Accessing Database Schemas in MySQL Using Java JDBC
Question: Retrieve a list of database schemas within a MySQL database using Java JDBC.
Response:
To obtain a list of database schemas, the getCatalogs() method of the DatabaseMetaData interface should be utilized. In the case of MySQL databases, the getCatalogs() method returns schema information instead of the term "schema."
<code class="java">Class.forName("com.mysql.jdbc.Driver"); // Modify user and password as needed Connection con = DriverManager.getConnection (connectionURL, "user", "password"); ResultSet rs = con.getMetaData().getCatalogs(); while (rs.next()) { System.out.println("TABLE_CAT = " + rs.getString("TABLE_CAT") ); }</code>
This code demonstrates the process of connecting to a MySQL database and retrieving a list of schemas. Each schema will be printed to the console.
The above is the detailed content of How to Retrieve a List of Database Schemas in MySQL Using Java JDBC?. For more information, please follow other related articles on the PHP Chinese website!