Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Rowset problem

  Asked By: Raynard    Date: Jan 10    Category: Java    Views: 499

I want to search a row in the rowset by column name(for example..product rowset,
have to search by product id).How can I do it(one way is to traverse whole
rowset and check product id.) is there any other solution(like find method in
RecordSet in ADO).



3 Answers Found

Answer #1    Answered By: Doyle Gonzalez     Answered On: Jan 10

I am assuming that you are talking about JDBC here?

If so, how about getting the row  you want with an SQL statement, then
getting the RecordSet and retreiving the data? But maybe I've missed
something about your problem?

Answer #2    Answered By: Balbir Kaur     Answered On: Jan 10

I m making rowset  by first making statement and Resultset through sql.
Connection cn = .................
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("Select pr_id,pr_name from Product");
CachedRowSet crs = new CachedRowSet();

thats how I do it.
How can I search  any pr_id(Product ID) in the rowset.

Answer #3    Answered By: Rene Sullivan     Answered On: Jan 10

You use SQL... (see for instance: http://www.sqlcourse.com/ )

What you should do is the following:

Connection cn = .................
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("Select pr_id,pr_name from product  WHERE
pr_id = '4'"); // note the last section here

In the WHERE clause you may put =, <, > etc. (check out the tutorial).

It is not advisable to perform searches in resultsets through java beacuse
almost always will the DBMS do it faster and more efficient. Instead you may
put your objects (here product objects) in a hashmap where the pr_id is the
key (but thats a completely different story).

However if you still decide to stay with selecting the whole table into your
resultset the only thing you can do is to parse through it until you find
the row  with the correct pr_id (either you do it or you find  someone elses
implementation). You can hopefully see that once your product catalog grows
to several hundred items your JVM will be bogged down with a lot of in
memory data and unneccesary iterations through the ResultSet.

Didn't find what you were looking for? Find more on Rowset problem Or get search suggestion and latest updates.