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

  • JdbcTemplate

    I am fresher in spring framework and have a very small questions

    public Map<Integer,String> getSubXYZ(final Integer reasonId){
    final Map<Integer, String> subReasonsMap =
    new HashMap<Integer, String>();
    final List<BatchPropertyVO> batchPropertyVOList = new ArrayList<BatchPropertyVO>();
    PreparedStatementCreator psc = new PreparedStatementCreator() {
    public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
    String query = SQL_GET_SUBXYZ;
    PreparedStatement stmt = conn.prepareStatement(query);
    stmt.setInt(1, reasonId);
    return stmt;
    }
    };

    jdbcTemplate.query(psc,
    new ResultSetExtractor() {
    public Object extractData(ResultSet rs) throws SQLException,
    DataAccessException {

    while (rs.next()) {
    subReasonsMap.put( rs.getInt("reason_id"),
    rs.getString("reason"));
    }
    return null;
    }
    });

    return subReasonsMap;
    }

    Question1: why do we need to use final variable as parameter? when we can change the value of final variable by making it Map ?

    Question2: In JdbcTemplate we are using innerclasses and looks little complicated instead using java Jdbc looks simple , I have not read performance of JdbcTemplate over Jdbc so if someOne can provide me good tutorial, I would be very thankful.

    Question3: if we have to return simple String or Integer from method we can't as we have declare final variable .
    it looks burdensome for code. But to maintain consistency I have to either use JdbcTemplate for all method of dao or java JDBC. Pls help me if some solution is there?

    Question 4: if I am not opening connection why it sometime give me exception "close connection"?

  • #2
    Originally posted by fortechforums View Post
    Question1: why do we need to use final variable as parameter? when we can change the value of final variable by making it Map ?
    This is just a general Java thing, if you want to pass the variable to an anonymous inner class it's going to have to be final. I'm not sure what the second part of the question is.

    Originally posted by fortechforums View Post
    Question2: In JdbcTemplate we are using innerclasses and looks little complicated instead using java Jdbc looks simple , I have not read performance of JdbcTemplate over Jdbc so if someOne can provide me good tutorial, I would be very thankful.
    It's just providing a thin wrapper around JDBC so the performance should be comparable. A simple test should prove this.

    Originally posted by fortechforums View Post
    Question3: if we have to return simple String or Integer from method we can't as we have declare final variable .
    it looks burdensome for code. But to maintain consistency I have to either use JdbcTemplate for all method of dao or java JDBC. Pls help me if some solution is there?
    If you just want to return a single item you can do something like queryForObject, but there are lots of options here. Have a look at the JavaDoc.
    http://www.springframework.org/docs/...va.lang.Class)

    Originally posted by fortechforums View Post
    Question 4: if I am not opening connection why it sometime give me exception "close connection"?
    I'm not sure what the problem is here. You'd have to post the exception, the configuration and the code you are running. If you post this in [code] [ /code] tags, it's soooo much more readable.
    Last edited by karldmoore; Aug 29th, 2007, 10:30 AM.

    Comment


    • #3
      Thanks karldmoore !!!!
      Now I can change to queryForObject which really looks good and solve my purpose.

      Comment


      • #4
        Originally posted by fortechforums View Post
        Thanks karldmoore !!!!
        Now I can change to queryForObject which really looks good and solve my purpose.
        Not a problem, it's well worth having a read of the JavaDoc it can save you lots of time . If you are using Java 1.5 you might also want to check this out.
        http://blog.interface21.com/main/200...-in-spring-20/
        Last edited by karldmoore; Aug 29th, 2007, 10:29 AM.

        Comment

        Working...
        X