Print Resultset

M

MAB

How to print the contents of the resultset if the no. of columns and there
types are not known? If I know the no. of columns and types I can do

while (rs.next()) {
System.out.println( rs.getString(1) + ' ' + rs.getInt(2) ) ;
}

thx
 
B

Brusque

MAB said:
How to print the contents of the resultset if the no. of columns and there
types are not known? If I know the no. of columns and types I can do

while (rs.next()) {
System.out.println( rs.getString(1) + ' ' + rs.getInt(2) ) ;
}

thx

Look at the rs.getMetaData() method, and the resulting ResultSetMetaData
object. You can get the column count, plus the column types if you need to
(although you might be able to get away with just using rs.getObject(i)
depending on your JDBC driver)
 
A

Alex Kizub

Always use rs.getString(i); It gives you toString() presentation.
Alex Kziub.
 
D

Donald Roby

How to print the contents of the resultset if the no. of columns and there
types are not known? If I know the no. of columns and types I can do

while (rs.next()) {
System.out.println( rs.getString(1) + ' ' + rs.getInt(2) ) ;
}

thx

You might want to use ResultSetMetaData to find the info you're missing:

ResultSetMetaData rsmd = rs.getMetaData();

The ResultSetMetaData class has methods to tell you how many columns there
are and what their SQL datatypes are. You may still have to play with
datatype conversion somewhat, but this can help make some things much more
generic.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top