Results 1 to 4 of 4

Thread: Executing SQL using a connection

  1. #1
    Join Date
    Sep 2004
    Posts
    6

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Posts
    1,104

    Default

    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
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

  4. #4
    Join Date
    Sep 2004
    Posts
    6

    Default 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

Similar Threads

  1. recovering from SQL connection loses...
    By ElPapa in forum Data
    Replies: 10
    Last Post: Oct 22nd, 2007, 05:45 AM
  2. hibernate pagination
    By oliverchua in forum Data
    Replies: 8
    Last Post: Sep 23rd, 2005, 06:06 PM
  3. Replies: 7
    Last Post: Aug 18th, 2005, 02:41 PM
  4. stale Oracle processes
    By compostellas in forum Data
    Replies: 7
    Last Post: Jun 27th, 2005, 12:14 PM
  5. Transaction Management
    By caverns in forum Data
    Replies: 3
    Last Post: Mar 8th, 2005, 06:38 AM

Posting Permissions

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