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
- Start with One column named n
- Do this Read column number 1
- Result n
Apply it to a real problem
- Start with An order form with two columns
- Do this Read the metadata in column order
- Result order_id then quantity
Advertisements
Run the example
Save as Main.java and run java Main.java with JDK 21.
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));
}
}
}
1 order_id 2 quantity