You can get the list of databases from the MetaData of the Database.
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password=edureka");
ResultSet resultSet = connection.getMetaData().getCatalogs();
//iterate each catalog in the ResultSet
while (resultSet.next()) {
// Get the database name, which is at position 1
String databaseName = resultSet.getString(1);
System.out.print(databaseName);
}
Hope this helps!
Check out Java certification course to become certified expert.
Thanks!