Home > Database > Mysql Tutorial > body text

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

Barbara Streisand
Release: 2024-11-01 07:36:02
Original
757 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!