In my java app, it seems to use parameters in my query to the database, I need to utilize the PreparedStatement. However at the same time, I would like to use the resultset from the statement in forward/backward mode (scrollable) PreparedStatement does not seem to offer setting the scrollable mode Statement does not seem to offer parameters.
Seems like a basic question..but nothing jumping out at me (other than using Statement and constructing the SQL without parameters). Is there really no way to supply parameters to a Statement..or have a preparedstatement scrollable? Am I missing something?
conn = Utility.getConnection();
tmpSQL = "SELECT * FROM " + baseTable + " WHERE " + filterCriteria
+ " ORDER BY " + sortCriteria;
//method 1
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rset = stmt.executeQuery(tmpSQL); //not using any parameters!
//method 2
PreparedStatement pStatement = conn.prepareStatement(tmpSQL); //not scrollable!
if (params != null)
for (int i = 0; i < params.size(); i++) {
pStatement.setString(i + 1,
((Parameter) params.get(i)).getStringValue());
}
rset = pStatement.executeQuery();
JDBC provides two types of result sets that allow you to scroll in either direction or to move the cursor to a particular row. Derby supports one of these types: scrollable insensitive result sets ( ResultSet. TYPE_SCROLL_INSENSITIVE ).
It is used for accessing your database. Statement interface cannot accept parameters and useful when you are using static SQL statements at runtime. If you want to run SQL query only once then this interface is preferred over PreparedStatement.
You can create a Statement that returns result sets in one of the following types: - TYPE_FORWARD_ONLY: the result set is not scrollable (default). - TYPE_SCROLL_INSENSITIVE: the result set is scrollable but not sensitive to database changes.
Now, as we have mentioned, we can scroll through records with the help of the ResultSet object. But, this ability does not come by default. The default behavior of the ResultSet object is that it is not updatable and the cursor it owns actually moves in one direction, forward only.
Use
PreparedStatement pStatement = conn.prepareStatement(tmpSQL,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Java Doc Info
Then to get the count of records in your ResultSet, use rset.last()
followed by rset.getRow()
. Then use rset.beforeFirst()
to put the cursor back to where it was initially.
Scrollability is mostly depending on the underlying database. Even though JDBC has a method to scroll back, it is not implemented e.g. in Oracle JDBC driver.
I would suggest to avoid of scrolling the result set. In fact even if it works for some databases, it is quite inefficient to implement. Also inefficient to use on the GUI, since each scrolling would then trigger a database operation, which is slow.
The usual approach is to load all rows to a container (e.g. List<...> ) and process that, if you have a moderate number of rows (say up to 1000 rows). If you have a lot more rows, then:
To get the count of records, execute a separate statement with select count(*). Then execute another select to actually read the records and fetch them (only forward).
It is much faster than reading all records just to count them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With