Announcement Announcement Module
No announcement yet.
JDBCTemplate Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • JDBCTemplate

    Hello all!

    I am quite a novice regarding jdbctemplate.

    I need to create a generic method which allows the caller to send an sql-query and get a resultSet in return. When I, inside this method, calls jdbctemplate.query() this requires for instance a RowMapper. But the way I understand it, when using RowMapper you need to know the datatype of the column of the row it should map? But the problem is that this SQL-query could be any kind of sql-select towards any table in the database.

    My question is as follows:
    How can I use the jdbcTemplate.query-method when I don`t know the result of the query? All I want is to be able to return whatever ResultSet the query fetches.

    Hope I was able to describe my challenge here.

    Thank you in advance!

  • #2
    Unless I'm missing something here, this sounds rather silly. A ResultSet is a resource that should be managed carefully. You don't want to return a ResultSet from a method call simply because you need to close it properly, along with the connection / statement(s) you were using.

    The whole point of the DAO layer is to encapsulate the persistence and data extraction logic from the other layers, and Spring allows you to do that easily if you choose to use JDBCTemplate correctly.

    If you want a generic method that will return a generic representation of the queried data you can use one of the queryForMap() methods. This will return a list of maps. I strongly advise you to write a properly typed API instead though.