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 |
Post date | 2010-08-24T08:38:39Z |
On 23-8-2010 21:57, Yves Glodt wrote:
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
> Thanks for the hint, I am aware of ResultSetMetaData and already use it. HowIt is relatively easy: first query the ResultSetMetaData and then
> to get the names as first row without iterating twice is still beyond my
> knowledge however.
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