Results 1 to 5 of 5

Thread: Spring Bulk Insert

Hybrid View

  1. #1
    Join Date
    Sep 2007
    Posts
    21

    Default Spring Bulk Insert

    Does anyone have any code examples of how to do a bulk insert using Simple JDBC Template?

  2. #2
    Join Date
    Oct 2006
    Posts
    156

    Default

    What exactly do you mean by "bulk insert"? Do you want to send many insert statements in a single batch, or execute a single statement that inserts may rows? An example of the latter is:

    INSERT CUSTOMER
    SELECT * FROM PEOPLE

    This can be achieved simply by using any of the techniques for updating the database using Simple JDBC Template.

  3. #3
    Join Date
    Sep 2007
    Posts
    21

    Default

    I wish to execute a single statement that inserts may rows, can you give me any examples?

  4. #4
    Join Date
    Oct 2006
    Posts
    156

    Default

    Here's a method that inserts a single row into a table named TRANSACTION:

    Code:
    	private static final String INSERT_TRAN_SQL = 
    			"insert into TRANSACTION (type, tran_date, description, maude_paid, donal_paid) values (?, ?, ?, ?, ?)";
    
    	public void insertTransaction(final Transaction tran) {
    
    	
    		getJdbcTemplate().update(new PreparedStatementCreator() {
    			public PreparedStatement createPreparedStatement(
    					Connection connection) throws SQLException {
    
    				PreparedStatement ps = connection
    						.prepareStatement(INSERT_TRAN_SQL);
    
    				// Parameters order is: type, tran_date, description,
    				// maude_paid, donal_paid
    				ps.setString(1, tran.getTransactionType().toString());
    
    				Date sqlDate = new Date(tran.getDate().getTime());
    				ps.setDate(2, sqlDate);
    				ps.setString(3, tran.getDescription());
    				ps.setFloat(4, tran.getMaudePaid());
    				ps.setFloat(5, tran.getDonalPaid());
    
    				return ps;
    			}
    		});
    
    	}
    To insert multiple rows I guess you just need to replace the INSERT...VALUES with an INSERT....SELECT. Also, you should replace the ? parameters with named parameters.

    Cheers,
    DM
    Last edited by domurtag; Nov 28th, 2007 at 12:30 PM.

  5. #5
    Join Date
    Sep 2007
    Posts
    21

    Default

    thank you for your help on this topic.

Posting Permissions

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