Move to first record

Discussions

Web tier: servlets, JSP, Web frameworks: Move to first record

  1. Move to first record (3 messages)

    I'm trying to load data from a result set into an array, and I need to loop through the data set looking for a particular string in one of the data fields. when I find it, that goes into the new array. I then need to go back to the result set and loop through again, looking for the next value. The ORDER BY function of sql is not able to sort in the way I need, so this is the only way I can think of to do this. I am using JRun as an appserver, with sql server as the database server. When I try to use the java.sql RecordSet.first() function, I get an SQL Exception that says method RecordSet.first not found. It's in the java.sql specs, so why can't I use it? Any suggestions?

    Threaded Messages (3)

  2. Move to first record[ Go to top ]

    This is from the API docs for the JDK1.3:

    "A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, it is possible to iterate through it only once and only from the first row to the last row. New methods in the JDBC 2.0 API make it possible to produce ResultSet objects that are scrollable and/or updatable."

    I guess the first thing to check is whether or not your JDBC driver is at least up to 2.0 spec. Then, check to make sure that you're creating a scrollable result set. You should create your statement object, either a regular Statement or a PreparedStatement, with a result set type of either ResultSet.TYPE_SCROLL_SENSITIVIE or ResultSet.TYPE_SCROLL_INSENITIVE.

    e.g.
    Connection con = ...;
    Statement statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

    Hope this helps,

    Andy
  3. Move to first record[ Go to top ]

    Thanks!
  4. Move to first record[ Go to top ]

    If that doesn't work you could always use two arrays. Load the first one with all of the data from the ResultSet, then then iterate to your heart's content filling the second array with the stuff you want.