import java.sql.*; public class DatabaseMetadataExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/your_database_name"; // Replace with your database URL String user = "your_username"; // Replace with your database username String password = "your_password"; // Replace with your database password try (Connection conn = DriverManager.getConnection(url, user, password)) { // Get Database Metadata DatabaseMetaData metaData = conn.getMetaData(); // Print database information System.out.println("Database Product Name: " + metaData.getDatabaseProductName()); System.out.println("Database Product Version: " + metaData.getDatabaseProductVersion()); System.out.println("Driver Name: " + metaData.getDriverName()); System.out.println("Driver Version: " + metaData.getDriverVersion()); // Get and print tables ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"}); System.out.println("\nTables in the database:"); while (tables.next()) { System.out.println("Table Name: " + tables.getString("TABLE_NAME")); } tables.close(); } catch (SQLException e) { e.printStackTrace(); } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter