• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting Values from resultSet without knowing the column data types

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the ResultSet#getMetaData method. It will give you useful information like number of columns, column name, type...
 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start your own thread.
 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!