Results 1 to 4 of 4

Thread: Beginner Q: deleting a list from jdbc using template

  1. #1
    Join Date
    Aug 2004
    Posts
    19

    Default Beginner Q: deleting a list from jdbc using template

    Hello,
    i want to erase a list of records from a database using Spring JdbcTemplate, However i couldnt find the corect method for that. i know with hibernate it is possible, is there a way to erase a list of objects efficiently using JdbcTemplate? (i would appreciate a tiny example)
    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    San Mateo, CA
    Posts
    1,265

    Default

    It sounds like you want to issue a SQL "DELETE...WHERE", which you can do via the various update methods of JdbcTemplate.

    There is no way in Spring JDBC to delete all objects in a collection from the database. However, that would usually be too be inefficient, as it would involve n JDBC calls, rather than 1.

  3. #3
    Join Date
    Aug 2004
    Posts
    19

    Default

    thanks a lot. i will use update methods. i think it is fine to me it in n iterations.

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

    Default

    If you don't mind a few roundtrips to the database, then you could use the batchUpdate feature.

    Here is an example where we pass in a JdbcTemplate and a List of Customer objects that should be deleted.

    Code:
    	private void deleteBatch(JdbcTemplate jt, final List customers) {
    		
    		int[] actualRowsAffected = jt.batchUpdate(
    			"delete from customer where id = ?",
    			new BatchPreparedStatementSetter() {
    				public void setValues(PreparedStatement ps, int i) throws SQLException {
    					ps.setInt(1, ((Customer)customers.get(i)).getId());
    				}
    				public int getBatchSize() {
    					return customers.size();
    				}
    			});
    		
    	}
    You need to implement the BatchPrepareStatementSetter with two methods setValues and getBatchSize. The setValues methods gets a PreparedStatment and the index indicating the position in the batch. In our case this corresponds to the position in the List of Customer objects.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

Similar Threads

  1. Replies: 2
    Last Post: Sep 1st, 2009, 09:24 AM
  2. Glazed lists
    By adepue in forum Swing
    Replies: 14
    Last Post: Aug 2nd, 2006, 07:13 AM
  3. Odd behaviour when injecting TransactionTemplate
    By damon311 in forum Container
    Replies: 3
    Last Post: Jul 23rd, 2005, 11:21 AM
  4. Date format using JDBC Template
    By macmeureg in forum Data
    Replies: 5
    Last Post: Jun 29th, 2005, 10:18 AM
  5. Warning jdbc template
    By khem in forum Data
    Replies: 2
    Last Post: Oct 5th, 2004, 07:42 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
  •