How do I get the column name/value pairs from a ResultSet?

L

laredotornado

Hello,

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?

Thanks, - Dave
 
B

Bjorn Abelli

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top