I'm using JDK 1.3. If I am iterating through a ResultSet, for a
particular result row, how do I look up all the name/value pairs
without explicitly knowing the column names or the number of columns
returned in the result row?
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/ResultSetMetaData.html
Here's an example:
--------------------------------------
import java.sql.*;
class SelectSample
{
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver(new YourDriver());
String cstring = "the Connection string";
Connection conn =
DriverManager.getConnection (cstring);
Statement stmt = conn.createStatement ();
String query = "the query";
ResultSet rset = stmt.executeQuery (query);
ResultSetMetaData rsMetaData = rset.getMetaData();
int columnCount = rsMetaData.getColumnCount();
for (int i = 0; i < columnCount; i++)
{
String columnName = rsMetaData.getColumnName(i+1);
System.out.print(columnName + "\t");
}
System.out.println("");
while (rset.next ())
{
for (int i = 0; i < columnCount; i++)
{
System.out.print (rset.getString (i+1) + "\t");
}
System.out.println ("");
}
// Don't forget to clean up...
rset.close();
stmt.close();
conn.close();
}
}
// Bjorn A
Inviato da X-Privat.Org - Registrazione gratuita
http://www.x-privat.org/join.php