Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Result Set + printing

Status
Not open for further replies.

mikedaruke

Technical User
Mar 14, 2005
199
0
0
US
I want to use a select * from table;

Now i would not know how many columns or fields returned.
I wrote this to try and print, and it throws and MSQL exeception. Help! Anybody have an easier way.

// Print Results here
int count = 0;

while(rs.next()) {
while(rs.getBoolean(count)){ //testing if true?
count++;
System.out.println(rs.getString(count));
// Printout
}
}

I just don't want to hardcode rs.getString(1) etc.

Thanks!
 
Hi,

If you just need to record count from select * from table, but it is alway better to use sql functions like select count(*) from table

Code:
// Print Results here
int count = 0;
while(rs.next()) {
    count ++;
}

System.out.println("Record Count is" + count);

Cheers
Venu

 
I know I can count the records.

The question is I don't know how many fields are in the table. So when I get my result set I don't know how many columns there are.

So I can't just say:

rs.getString(1)
rs.getString(2)
rs.getString(3)
rs.getString(4)

What if there isn't a rs.getString(4) value? What if there are 5 more columns and I only thought there were 4?

I am talking about columns here not rows.

Thanks!
 
Hm.
One question is, what might you do with data, you don't know it's there?

Well - generate a meta-application.

but getBoolean (9) will fail, if the data-type isn't boolean at all.
While getString (9) will work (afaik), since the native-type, how things are returned, is String.

Everything you need to start, you will find in the javadocs:
java.sql.ResultSetMetaData

seeking a job as java-programmer in Berlin:
 
To get the number of columns :

int iColumnCount = resultSet.getMetaData().getColumnCount();

and use it as :

while (rs.next()) {
String line = "";
for (int i = 1; i < iColumnCount; i++) {
line += rs.getString(i) +"|";
}
System.out.println("data : " +line);

}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top