Home  >  Article  >  Database  >  How to Retrieve a List of Database Schemas in MySQL Using Java JDBC?

How to Retrieve a List of Database Schemas in MySQL Using Java JDBC?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-27 23:10:29166browse

How to Retrieve a List of Database Schemas in MySQL Using Java JDBC?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn