how to list names of all databases in java

To list the names of all databases in Java, you can use JDBC (Java Database Connectivity) API, which provides a standard interface for connecting to databases and performing SQL queries.

Here's an example code snippet that demonstrates how to list the names of all databases:

‮ot refer‬
import java.sql.*;

public class ListAllDatabases {
    public static void main(String[] args) throws SQLException {
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root&password=mypassword");
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet resultSet = metaData.getCatalogs();
        while ( {
            String databaseName = resultSet.getString(1);

This code connects to a MySQL database (replace the connection string with your own database URL, username, and password), retrieves the metadata for the database, and then uses the getCatalogs() method of the DatabaseMetaData interface to get a list of all the databases. The getString(1) method of the ResultSet interface is used to retrieve the name of each database, and the name is printed to the console.

Note that the getCatalogs() method returns a ResultSet that contains the names of all databases that the current user has access to. If the user has limited privileges, they may not be able to see all databases.