Subject Re: [Firebird-Java] How to get column names as first row? (or why I would need ResultSet.first() and ResultSet.previous())
Author Mark Rotteveel
On 23-8-2010 21:57, Yves Glodt wrote:
> Thanks for the hint, I am aware of ResultSetMetaData and already use it. How
> to get the names as first row without iterating twice is still beyond my
> knowledge however.

It is relatively easy: first query the ResultSetMetaData and then
iterate over the resultset:

(warning: quick&dirty code without resource management)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
*
* @author Mark
*/
public class SelectWithMetadata {

public static void main(String[] args) throws
ClassNotFoundException, SQLException {
Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection con =
DriverManager.getConnection("jdbc:firebirdsql://localhost/D:/data/DB/EMPLOYEE.FDB",
"sysdba", "masterke");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM CUSTOMER");
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
for (int idx = 1; idx <= colCount; idx++) {
System.out.print(rsmd.getColumnLabel(idx) + "\t");
}
System.out.println();

while (rs.next()) {
for (int idx = 1; idx <= colCount; idx++) {
System.out.print(rs.getString(idx) + "\t");
}
System.out.println();
}
}
}

--
Mark Rotteveel