Home  >  Article  >  Database  >  How to Retrieve a List of MySQL Database Schema Names Using Java JDBC?

How to Retrieve a List of MySQL Database Schema Names Using Java JDBC?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 07:36:02758browse

How to Retrieve a List of MySQL Database Schema Names Using Java JDBC?

Obtaining a List of MySQL Database Schema Names via Java JDBC

The JDBC API provides methods to retrieve information about the connected database schema. To retrieve a list of schema names in MySQL using Java JDBC, follow these steps:

  1. Establish a connection to the database using DriverManager.getConnection().
  2. Obtain the DatabaseMetaData object using Connection.getMetaData().
  3. Since MySQL uses the term "catalog" instead of "schema," use the getCatalogs() method to retrieve a ResultSet containing the catalog names.

The following code example demonstrates this process:

<code class="java">Class.forName("com.mysql.jdbc.Driver");

// Replace "connectionURL", "user", and "password" with appropriate values
Connection con = DriverManager.getConnection(connectionURL, user, password);

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

while (rs.next()) {
    System.out.println("CATALOG_NAME = " + rs.getString("TABLE_CAT"));
}</code>

This code assumes you have loaded the MySQL JDBC driver and established a connection to the database. The output of the code will be a list of database schema names, or "CATALOG_NAME" in MySQL terminology.

The above is the detailed content of How to Retrieve a List of MySQL Database Schema Names 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