package org.kodejava.jdbc;
import java.sql.*;
public class GettingTableListExample {
private static final String URL = "jdbc:mysql://localhost/kodejava";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
public static void main(String[] args) throws Exception {
try (Connection connection =
DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
// Gets the metadata of the database
DatabaseMetaData metaData = connection.getMetaData();
String[] types = {"TABLE"};
ResultSet rs = metaData.getTables("kodejava", "kodejava", "%", types);
while (rs.next()) {
String tableCatalog = rs.getString(1);
String tableSchema = rs.getString(2);
String tableName = rs.getString(3);
System.out.printf("%s - %s - %s%n",
tableCatalog, tableSchema, tableName);
}
}
}
}
Here is the result:
...
kodejava - null - authors
kodejava - null - book_authors
kodejava - null - books
...
Maven dependencies
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023