Announcement Announcement Module
Collapse
No announcement yet.
Spring Bulk Insert Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Bulk Insert

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

  • #2
    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.

    Comment


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

      Comment


      • #4
        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, 12:30 PM.

        Comment


        • #5
          thank you for your help on this topic.

          Comment

          Working...
          X