WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Java JDBC · Intermediate · question 33 of 100

How do you use JDBC to handle database metadata?

📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

JDBC (Java Database Connectivity) is an API that allows Java applications to interact with relational databases. One of the features of JDBC is the ability to retrieve metadata about the database and its objects. Metadata refers to information about the database structure, such as table names, column names, data types, and constraints.

In order to handle database metadata using JDBC, you can make use of the java.sql.DatabaseMetaData interface, which provides methods for retrieving information about the database. Here’s an example of how to use the DatabaseMetaData interface to retrieve information about a database:

    // Load the JDBC driver
    Class.forName("com.mysql.jdbc.Driver");
    
    // Establish a connection to the database
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
    
    // Get the DatabaseMetaData object
    DatabaseMetaData metaData = connection.getMetaData();
    
    // Get information about the database
    String databaseName = metaData.getDatabaseProductName();
    String databaseVersion = metaData.getDatabaseProductVersion();
    String userName = metaData.getUserName();
    
    // Print out the database information
    System.out.println("Database name: " + databaseName);
    System.out.println("Database version: " + databaseVersion);
    System.out.println("User name: " + userName);
    
    // Get information about the tables in the database
    ResultSet resultSet = metaData.getTables(null, null, null, new String[]{"TABLE"});
    while (resultSet.next()) {
        String tableName = resultSet.getString("TABLE_NAME");
        System.out.println("Table name: " + tableName);
    }

In this example, we load the MySQL JDBC driver, establish a connection to the "mydatabase" database, and retrieve the DatabaseMetaData object from the connection. We then use the DatabaseMetaData methods to retrieve information about the database, such as the database name, version, and user name.

We also use the getTables() method to retrieve information about the tables in the database. This method takes four arguments: catalog, schema pattern, table name pattern, and types. In this case, we pass null for the catalog, schema pattern, and table name pattern, and specify that we only want to retrieve TABLE types.

Once we have the ResultSet object containing the table information, we loop through it and print out the name of each table.

This is just a simple example of how to use JDBC to handle database metadata. There are many other methods provided by the DatabaseMetaData interface that allow you to retrieve information about columns, indexes, primary keys, foreign keys, and more.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java JDBC interview — then scores it.
📞 Practice Java JDBC — free 15 min
📕 Buy this interview preparation book: 100 Java JDBC questions & answers — PDF + EPUB for $5

All 100 Java JDBC questions · All topics