开发者

Fill Class from jdbc ResultSet

Now im filling my classes like this:

Part part = new Part();

ResultSet rs = statement.executeQuery();
part.setBrand(rs.getString("P_BRAND"));
part.setComment(rs.getString("P_COMMENT"));
part.setContainer(rs.getString("P_CONTAINER"));
part.setMfgr(rs.getString("P_MFGR"));
part.setName(rs.getS开发者_开发技巧tring("P_NAME"));

Is there another and fast way to fill part object? Something like that method(Part.class, part, rs) and returns an filled Part class.


Short Answer: Yes

Longer Answer:

There are several routes to an answer:

  1. Use an ORM package like myBatis or Hibernate. You can write a method to populate an object from a ResultSet generically using reflection, but why reinvent the wheel. Both myBatis and Hibernate do this already.
  2. Write a Part constructor that takes a ResultSet and which pulls the column values out.
  3. Write a Part constructor that takes all the values that are in your ResultSet and call it with the values in the ResultSet (see the Dennis answer for this).
  4. Write a Part Builder object that takes a ResultSet and constructs a Part based on the values therein.
  5. something I have not thought about.


If you write a method to construct your part object, you will do the same work there to copy data into it.

However, if you are doing this construction of the part object repeatedly then having a method makes it more maintainable and organized code.

Having a method is not necessarily going to make it fast, it would just make the code more concise and avoid repetition of code (ie. the DRY principle). You may also want to utilize the Builder pattern here.

Another option is to use Apache BeanUtils - it provides you with a lot of helpful utility methods.

However, if you are talking specific to always copying from JDBC result set to an internal DTO object and willing to leverage the spring framework then you can use the RowMapper() from it. Also there is the ORM route with Hibernate, iBatis, etc.


Short answer: No.

In principle, you could write a function to populate a bean from a ResultSet generically by using the reflection package. But frankly I think this would be clumsy and an all-around bad idea.

If you have some real need for generic data, you could create a hash map using the field name as the key for each entry. But for normal use, this would also be a bad idea, because it creates problems in recognizing that you have mis-spelled a field name.

In short, you're really best to copy fields out of the result set one by one. Normally this isn't a big deal. It takes one line of code per field. I suppose if you have a hundred fields in your table it would be a pain.


If I understand your question correctly you could edit the constructor of Part Class to accept the values...

Part part;

ResultSet rs = statement.executeQuery();
part = new Part(rs.getString("P_BRAND"), rs.getString("P_COMMENT"), rs.getString("P_CONTAINER"), 
   rs.getString("P_MFGR"), rs.getString("P_NAME"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜