Nearby lessons

15 of 37

JDBC - Metadata

📌 What You Will Learn
  • What metadata means and the three metadata types in JDBC
  • DatabaseMetaData — information about the database
  • ResultSetMetaData — information about query results
  • The ResultSet types: forward-only, scrollable, updatable
  • What RowSets are and how they improve on ResultSet

Metadata is a fundamental part of database programming with JDBC. This lesson explains What is Metadata?, DatabaseMetaData — About the Database and ResultSetMetaData — About the Result Columns with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

What is Metadata?

Metadata means 'data about data'. It is extra information about your actual data — not the data itself. Examples:

In simple words: Metadata is 'data about data' — it describes your information, not the information itself, like the name, type and count of the columns in a table.
  • About the database: its product name, version.
  • About a ResultSet: how many columns, each column's name and type.

JDBC provides three types of metadata:

TypeGives information aboutHow to get it
DatabaseMetaDataThe database itselfcon.getMetaData()
ResultSetMetaDataA ResultSet's columnsrs.getMetaData()
ParameterMetaDataThe parameters of a PreparedStatementpst.getParameterMetaData()
In simple words: JDBC gives you three kinds of metadata. DatabaseMetaData tells you about the database itself, ResultSetMetaData about the result columns, and ParameterMetaData about the parameters of a prepared statement.

DatabaseMetaData — About the Database

getMetaData() on a Connection returns a DatabaseMetaData object with methods to describe the database:

DatabaseMetaData can also tell you whether the database supports a feature, like scrollable ResultSets: dbmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE).

Example02
JCode Cell
1import java.sql.*;
2 
3public class DBMetaDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 DatabaseMetaData dbmd = con.getMetaData();
9 System.out.println("Product name : " + dbmd.getDatabaseProductName());
10 System.out.println("Product version: " + dbmd.getDatabaseProductVersion());
11 System.out.println("Driver name : " + dbmd.getDriverName());
12 System.out.println("Driver version : " + dbmd.getDriverVersion());
13 
14 // list all table names in the database
15 ResultSet rs = dbmd.getTables(null, null, "%", new String[]{"TABLE"});
16 System.out.println("\nTables:");
17 while (rs.next()) {
18 System.out.println(" - " + rs.getString(3)); // 3rd column = table name
19 }
20 con.close();
21 }
22}
Output
Product name : MySQL Product version: 8.0.36 Driver name : MySQL Connector/J Driver version : 8.0.36 Tables: - employee - account

ResultSetMetaData — About the Result Columns

rs.getMetaData() gives the structure of the returned rows — useful when you don't know the columns in advance:

Example03
JCode Cell
1import java.sql.*;
2 
3public class RsMetaDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 Statement st = con.createStatement();
8 ResultSet rs = st.executeQuery("SELECT * FROM employee");
9 
10 ResultSetMetaData rsmd = rs.getMetaData();
11 int cols = rsmd.getColumnCount();
12 System.out.println("Number of columns: " + cols);
13 for (int i = 1; i <= cols; i++) {
14 System.out.println("Column " + i + ": " + rsmd.getColumnName(i)
15 + " (" + rsmd.getColumnTypeName(i) + ")");
16 }
17 con.close();
18 }
19}
Output
Number of columns: 3 Column 1: id (INT) Column 2: name (VARCHAR) Column 3: salary (DOUBLE)
📝 Key Takeaways
  • Metadata = data about data; JDBC has three kinds (Database, ResultSet, Parameter).
  • DatabaseMetaData (con.getMetaData()) describes the database and its tables.
  • ResultSetMetaData (rs.getMetaData()) describes the columns of a result.
  • ResultSet types: FORWARD_ONLY, SCROLL_INSENSITIVE, SCROLL_SENSITIVE.
  • Concurrency: READ_ONLY or UPDATABLE.
  • RowSets are flexible, scrollable, and can work disconnected from the database.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4