Describe a JDBC row without a database in Java

RowSetMetaDataImpl can describe a result shape in memory. JDBC column numbering starts at one, so the first and second columns use indices 1 and 2.

A simple example

Read column number 1

  1. Start with One column named n
  2. Do this Read column number 1
  3. Result n
JDBC column numbering begins at one.

Apply it to a real problem

  1. Start with An order form with two columns
  2. Do this Read the metadata in column order
  3. Result order_id then quantity
A report can label its two columns using metadata before reading any rows.
Advertisements

Run the example

Save as Main.java and run java Main.java with JDK 21.

Main.java
import javax.sql.rowset.RowSetMetaDataImpl;

public class Main {
    public static void main(String[] args) throws Exception {
        var columns = new RowSetMetaDataImpl();
        columns.setColumnCount(2);
        columns.setColumnName(1, "order_id");
        columns.setColumnName(2, "quantity");
        for (int i = 1; i <= columns.getColumnCount(); i++) {
            System.out.println(i + " " + columns.getColumnName(i));
        }
    }
}
Output
1 order_id
2 quantity

Official Java documentation