Announcement Announcement Module
Collapse
No announcement yet.
Executing SQL using a connection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Executing SQL using a connection

    Hi

    I have a java.sql.Connection that I obtained from JDO (kodo) and want to use it to do plain jdbc stuff. Can I get spring (-dao) to make life easier? I don't want to mess around with Statements, closing them at the right time...etc.

    I see that JDBCTemplate needs a DataSource. Is it possible to somehow use it if all I have is the Connection?

    Thanks in advance
    Srini

  • #2
    You can wrap your connection into a org.springframework.jdbc.datasource.SingleConnecti onDataSource and use it to instanciate the JdbcTemplate:
    Code:
      SingleConnectionDataSource ds = new SingleConnectionDataSource(myConnection, true);
      JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
    You should pay attention to the fact that SingleConnectionDataSource is not thread safe.

    HTH

    Comment


    • #3
      You could use a SingleConnectionDataSource that can accept a connection when you create it. You just have to make sure the connection is closed when you are done.
      Code:
      		DataSource dataSource = new SingleConnectionDataSource(con, true);
      Another option is to use the Spring-JDO integration as outlined in this article
      http://www.artima.com/announce/ment.jsp?num=67449

      Comment


      • #4
        Thanks

        Thanks! Just what I needed. I have more questions though. If I use JDBCTemplate.execute("some sql") will it take care of reusing Statements or will it create one for each? What else do I need to worry about?

        I'll be running lots of queries in loops and the transactions could get large.

        Thanks in advance
        Srini

        Comment

        Working...
        X