The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of columns, column name, column type etc., ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.
The ResultSetMetaData interface is used to get the structural information about the ResultSet. It helps in understanding the structure of the result returned by a query. It works with the ResultSet object and provides useful details about the query result.
The ResultSetMetaData interface belongs to the java.sql package, so we need to import it before using.
Or we can use:
| Field | Description |
|---|---|
| columnNoNulls | It that a column does not allow NULL values. |
| columnNullable | It indicates that a column allows NULL values. |
| columnNullableUnknown | It indicates that the nullability of a column's values is unknown. |
| Method | Description |
|---|---|
| getCatalogName(int column) | It gets the designated column's table's catalog name. |
| getColumnClassName(int column) | It returns the fully-qualified name of the Java class whose instances are manufactured if the method ResultSet.getObject is called to retrieve a value from the column. |
| getColumnCount() | It returns the number of columns in this ResultSet object. |
| getColumnDisplaySize(int column) | Indicates the designated column's normal maximum width in characters. |
| getColumnLabel(int column) | It gets the designated column's suggested title for use in printouts and displays. |
| getColumnName(int column) | It gets the designated column's name. |
| getColumnType(int column) | It retrieves the designated column's SQL type. |
| getColumnTypeName(int column) | It retrieves the designated column's database-specific type name. |
| getPrecision(int column) | It gets the designated column's specified column size. |
| getScale(int column) | It gets the designated column's number of digits to right of the decimal point. |
| getSchemaName(int column) | It gets the designated column's table's schema. |
| getTableName(int column) | It gets the designated column's table name. |
| isAutoIncrement(int column) | It indicates whether the designated column is automatically numbered. |
| isCaseSensitive(int column) | It indicates whether a column's case matters. |
| isCurrency(int column) | It indicates whether the designated column is a cash value. |
| isDefinitelyWritable(int column) | It indicates whether a write on the designated column will definitely succeed. |
| isNullable(int column) | It indicates the nullability of values in the designated column. |
| isReadOnly(int column) | It indicates whether the designated column is definitely not writable. |
| isSearchable(int column) | It indicates whether the designated column can be used in a where clause. |
| isSigned(int column) | It indicates whether values in the designated column are signed numbers. |
| isWritable(int column) | It indicates whether it is possible for a write on the designated column to succeed |
The getMetaData() method of ResultSet interface is used to get the ResultSetMetaData object.
public ResultSetMetaData getMetaData() throws SQLException
To get metadata from a ResultSet:
ResultSet rs = statement.executeQuery("SELECT * FROM employees");
ResultSetMetaData rsmd = rs.getMetaData();Consider the following schema.
CREATE TABLE emp (
emp_id NUMBER(5) PRIMARY KEY,
emp_name VARCHAR2(50) NOT NULL,
job VARCHAR2(30),
mgr NUMBER(5),
hiredate DATE,
salary NUMBER(10,2),
commission NUMBER(10,2),
dept_id NUMBER(3)
);
Inserting Some data into emp table.
INSERT INTO emp VALUES (101, 'John Smith', 'Manager', NULL, TO_DATE('2020-01-15','YYYY-MM-DD'), 75000, NULL, 10);
INSERT INTO emp VALUES (102, 'Alice Brown', 'Developer', 101, TO_DATE('2021-03-10','YYYY-MM-DD'), 55000, NULL, 20);
INSERT INTO emp VALUES (103, 'David Lee', 'Salesman', 101, TO_DATE('2022-07-01','YYYY-MM-DD'), 40000, 5000, 30);
Let’s get the metadata from the above data.
Java
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
// Load Oracle JDBC Driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Establish connection
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "oracle");
// Execute query
PreparedStatement ps = con.prepareStatement("SELECT * FROM emp");
ResultSet rs = ps.executeQuery();
// Get metadata
ResultSetMetaData rsmd = rs.getMetaData();
// Print total number of columns
int columnCount = rsmd.getColumnCount();
System.out.println("Total columns: " + columnCount);
// Print details of each column
for (int i = 1; i <= columnCount; i++) {
System.out.println("Column " + i + ":");
System.out.println(" Name: " + rsmd.getColumnName(i));
System.out.println(" Type: " + rsmd.getColumnTypeName(i));
System.out.println(" Display Size: " + rsmd.getColumnDisplaySize(i));
System.out.println(" Nullable: " + (rsmd.isNullable(i) == ResultSetMetaData.columnNullable ? "YES" : "NO"));
System.out.println(" Auto Increment: " + rsmd.isAutoIncrement(i));
System.out.println("-----------------------------------");
}
// Close connection
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
Total columns: 8 Column 1: Name: EMP_ID Type: NUMBER Display Size: 5 Nullable: NO Auto Increment: false ----------------------------------- Column 2: Name: EMP_NAME Type: VARCHAR2 Display Size: 50 Nullable: NO Auto Increment: false ----------------------------------- Column 3: Name: JOB Type: VARCHAR2 Display Size: 30 Nullable: YES Auto Increment: false ----------------------------------- Column 4: Name: MGR Type: NUMBER Display Size: 5 Nullable: YES Auto Increment: false ----------------------------------- Column 5: Name: HIREDATE Type: DATE Display Size: 7 Nullable: YES Auto Increment: false ----------------------------------- Column 6: Name: SALARY Type: NUMBER Display Size: 10 Nullable: YES Auto Increment: false ----------------------------------- Column 7: Name: COMMISSION Type: NUMBER Display Size: 10 Nullable: YES Auto Increment: false ----------------------------------- Column 8: Name: DEPT_ID Type: NUMBER Display Size: 3 Nullable: YES Auto Increment: false -----------------------------------
We request you to subscribe our newsletter for upcoming updates.