Prerequisites
- MySQL database installed.
- MySQL JDBC driver added to your project.
- Basic understanding of Java and JDBC.
Steps
- Set Up Dependencies
- Establish a Database Connection
- Retrieve and List Database Names
1. Set Up Dependencies
Ensure you have the MySQL JDBC driver in your classpath. If you are using Maven, add the following dependency to your pom.xml
:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
2. Establish a Database Connection
First, we need to establish a connection to the MySQL server using the DriverManager
class.
3. Retrieve and List Database Names
We'll use the SHOW DATABASES
SQL statement to retrieve the list of all databases and display their names.
Example Code
Below is the complete example code demonstrating how to connect to a MySQL server and list all database names.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ListDatabasesExample {
// JDBC URL, username, and password of MySQL server
private static final String JDBC_URL = "jdbc:mysql://localhost:3306/?useSSL=false";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) {
try (Connection connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD)) {
System.out.println("Connected to the database");
// List all database names
listDatabaseNames(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}
private static void listDatabaseNames(Connection connection) throws SQLException {
String showDatabasesSQL = "SHOW DATABASES";
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(showDatabasesSQL)) {
System.out.println("Databases:");
while (rs.next()) {
String databaseName = rs.getString(1);
System.out.println(databaseName);
}
}
}
}
Explanation
-
MySQL Connection:
DriverManager.getConnection(JDBC_URL, USER, PASSWORD)
is used to establish a connection to the MySQL server.- The URL
jdbc:mysql://localhost:3306/?useSSL=false
connects to the MySQL server without specifying a particular database.
-
Listing Database Names:
- The
listDatabaseNames()
method executes the SQL querySHOW DATABASES
to retrieve all database names. - A
Statement
object is created to execute the query. - A
ResultSet
object is used to iterate through the result set and print each database name.
- The
Output
Running the code will produce output similar to the following:
Connected to the database
Databases:
information_schema
mysql
performance_schema
sys
test
Conclusion
Listing all database names in a MySQL server using Java is straightforward with JDBC. By executing the SHOW DATABASES
SQL query, we can easily retrieve and display the names of all databases. This approach can be adapted for other database systems by modifying the JDBC URL, username, password, and SQL query as needed.
Comments
Post a Comment
Leave Comment