*
The moose likes JDBC and the fly likes Getting Values from resultSet without knowing the column data types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Getting Values from resultSet without knowing the column data types" Watch "Getting Values from resultSet without knowing the column data types" New topic
Author

Getting Values from resultSet without knowing the column data types

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi All,
I am in a situation where I need to get data from resultSet and I dont know how many columns it contains, I dont know the data types of the columns and I dont know the names of the columns. Lets say its a generic resultSet, then how will I be able to get data out of it. For example I have a function print(ResultSet rs) which prints the data in the resultSet passed to it. And I have the following 2 queries.

rs1=statement.executeQuery("Select * from Employees");

rs2=statement.executeQuery("Select name, emailId from Employees");

print(rs1);

print(rs2);


I need the print function to be generic for any result set.

Any ideas on that? Please correct me if I am wrong.

Thanks in advance.


Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Use the ResultSet#getMetaData method. It will give you useful information like number of columns, column name, type...


[My Blog]
All roads lead to JavaRanch
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Thanks Christophe,
It solved the problem for me ad saved me from a big problem. Thanks man for your help.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,

Could any body please tell me such a requirement where we need to extract the data from the databse without knowing the column names .

could anybody share their experience please on this question . Thanks .


Save India From Corruption - Anna Hazare.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Please start your own thread.
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hi Ravi,
I don't know if you have opened another thread or not(though searched for it but didn't find it). Anyway, I have a situation where I have to search for records in a table containing a huge number of rows and the number of resulted rows is also big. So it takes a lot of time in searching through that huge table of data. So in that case what I do is that I initially search through the table and then I store the resulted rows in a temporary table. Let say the number of resulted rows are 5000 and I want to show only 15 on my page(paging/pagination). So I show the first 15 from temporary table and then on next call I get the next 15 and so on.

Now, I wanted a generic solution for this. So that I may handle any query and any resultset and based on the column types and column names in resulteSet.getMetadata(), I may make a temporary table. So thanks to Christophe that he helped me out here.

HTH.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Values from resultSet without knowing the column data types