Tim Holloway wrote:You can access resultset values by column number, not just by column name. NOTE THAT column numbers start with 1, and not 0!
Also, you can obtain the resultset's metadata which will allow you to determine how many columns are in each row and their names. In the event that the query has a computed value, the database will have synthesized a column name. Or you can make your SQL query return alias column names using the "AS" option like so:
SELECT acctno AS account_number, amt_paid / avg(amg_paid) AS average_amount_paid_ratio ...
Tim Holloway wrote:While Junilu has a point (EmployeeListItem would be a better name), there are basically 2 ways to do that. One is really brutal and that's to construct a Class completely in Java code using the Class meta-methods and their friends and relatives. The other is to construct Java source code (well, you could construct raw bytecodes, but that's harder to debug). and run the code through the Java compiler, then use a classloader to cause the current runtime to adopt this new class.
OR you could simply forget about making custom classes and return a Map, which is what most people do. It's a lot less work.
Junilu Lacar wrote:Why would you name an object "EmployeeList" if it only represents one employee?
There are several ways to go about this but here are the main problems you have:
1. Figuring out which columns are included in the query and the order they will be in
2. Mapping the query columns to the appropriate setter() method on the object
For #1, you'll need metadata or you'll need to parse the text between "SELECT" and "FROM" of the query
For #2, for maximum flexibility, you'll have to use the Reflection API. Another option, although more tedious, would be to specify a "mapper" object for each query. That means you would potentially have to define as many mapper objects as you have queries. A mapper object will essentially have for its main logic whatever code you have in your while (rs.next()) loop.
Tim Holloway wrote:I'm not sure, but I think that Junilu was expecting that you had ready-made class definitions and you just wanted to construct instances of those classes and have your generic code populate their values. Like ORMs do and JAXB does. For example, the Apache Digester.
That's not trivial, but there are things to make it easier. You can use the Apache beanutils to leverage Java's introspection services. All the cool apps do that - Tomcat's configuration compiler, popular JPA implementations, and so forth.
However, if you're hoping to build up a custom class field by field on the fly, that's much messier. Aside from everything else, Java's internal security mechanisms deliberately discourage rogue apps from "sabotaging" internal class structure,
The Map option is much easier. Basically, you construct a Map<String, Object>, then iterate the metadata of the resultset. For each returned column, put() a name/value pair into the Map, where the name is the column name and the value is the object returned by doing a "get" value for that column from the ResultSet.
Dave Tolls wrote:I'd be surprised if there wasn't already some package that can do this for you.
Dave Tolls wrote:Not off the top of my head, but there must be something on github or similar.
Do a search on Java ResultSet to JSON, or something like that.