Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically create a Java ResultSet from custom data with no database

Tags:

java

sql

I have some existing code that accepts a java.sql.ResultSet that contains info retrieved from an Oracle database. I would now like to reuse this code, but I'd like to pass it a ResultSet object that I create myself from some in-memory data that is not affiliated with any database. Is there an existing Java framework class that I can use for this? ResultSet has tons of methods, so implementing my own class for this seemed like overkill, even though I could ignore most of the methods for my specific case.

I was thinking of something along the lines of the old Microsoft ADO recordset object, where I could create the fields and then populate the row data for each field. This seemed like an easily googlable question, but I've been unable to find any good pointers.

like image 851
Chris Farmer Avatar asked Jan 12 '10 21:01

Chris Farmer


People also ask

How do you create an empty ResultSet in Java?

default: throw new SQLException("No results"); }; private static final ResultSet EMPTY_SET = java. lang. reflect. Proxy.

How do I create a ResultSet object?

Generating a ResultSetPreparedStatement pstmt = dbConnection. prepareStatement("select * from employees"); ResultSet rs = pstmt. executeQuery(); The ResultSet object maintains a cursor that points to the current row of the result set.

How does Java handle empty ResultSet?

The right way to check if ResultSet is empty A better way to solve this problem is to get the data from the first row before calling the next() method again and you can do that by using a do-while loop, which checks the condition at the end of the loop as opposed to starting of the loop in case of a while loop.


1 Answers

  • Create your own AbstractResultSet class, one that (like AbstractQueue) implements all methods by throwing UnsupportedOperationException (Eclipse autogenerates these methods in a split second).
  • Now extend AbstractResultSet. The subclass can override only the methods you're interested in implementing.
like image 57
Joel Shemtov Avatar answered Sep 28 '22 01:09

Joel Shemtov