Results 1 to 4 of 4

Thread: JdbcTemplate

  1. #1

    Thumbs down 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. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote 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.

    Quote 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.

    Quote 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)

    Quote 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 at 10:30 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3

    Default

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

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote 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 at 10:29 AM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •