Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping a JDBC ResultSet to an object

I have a user class that has 16 attributes, things such as firstname, lastname, dob, username, password etc... These are all stored in a MySQL database and when I want to retrieve users I use a ResultSet. I want to map each of the columns back to the user attributes but the way I am doing it seems terribly inefficient. For example I am doing:

//ResultSet rs; while(rs.next()) {    String uid = rs.getString("UserId");    String fname = rs.getString("FirstName");    ...    ...    ...    User u = new User(uid,fname,...);    //ArrayList<User> users     users.add(u); }  

i.e I retrieve all the columns and then create user objects by inserting all the column values into the User constructor.

Does anyone know of a faster, neater, way of doing this?

like image 977
Quanqai Avatar asked Feb 22 '14 15:02

Quanqai


People also ask

How do I map ResultSet to an object?

Code to map ResultSet to Object - ResultSetMapper.The function mapRersultSetToObject maps resultset to an ArrayList of type SamplePojo. It first checks if the outputClass has the 'Entity' annotation. The core logic of this function is that, it gets the attributes in the outputClass having annotation 'Column'.

How do you create a ResultSet object?

But we can make this object to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by: Statement stmt = con. createStatement(ResultSet.

Which method is used to fetch the result from database and assign it to ResultSet object?

The ResultSet interface declares getter methods (for example, getBoolean and getLong ) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column.


2 Answers

If you don't want to use any JPA provider such as OpenJPA or Hibernate, you can just give Apache DbUtils a try.

http://commons.apache.org/proper/commons-dbutils/examples.html

Then your code will look like this:

QueryRunner run = new QueryRunner(dataSource);  // Use the BeanListHandler implementation to convert all // ResultSet rows into a List of Person JavaBeans. ResultSetHandler<List<Person>> h = new BeanListHandler<Person>(Person.class);  // Execute the SQL statement and return the results in a List of // Person objects generated by the BeanListHandler. List<Person> persons = run.query("SELECT * FROM Person", h); 
like image 105
Leo Avatar answered Sep 24 '22 17:09

Leo


No need of storing resultSet values into String and again setting into POJO class. Instead set at the time you are retrieving.

Or best way switch to ORM tools like hibernate instead of JDBC which maps your POJO object direct to database.

But as of now use this:

List<User> users=new ArrayList<User>();  while(rs.next()) {    User user = new User();          user.setUserId(rs.getString("UserId"));    user.setFName(rs.getString("FirstName"));   ...   ...   ...     users.add(user); }  
like image 28
Shoaib Chikate Avatar answered Sep 22 '22 17:09

Shoaib Chikate