Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Batch Updates with Prepared Statements - ***NEED HELP***

843859Nov 12 2008 — edited Nov 20 2014
I am working on this lab assignment for my Java class at school. The lab is instructing me to insert 7 customers into a table using a prepared statement and batch updates. I did 2 customers to begin with but only 1 of the prepared statements executes in the batch. Help would be much appreciated.

Here is a sample code of my insert function for the database:

The insert SQL update is: INSERT INTO CUSTOMER(custId, custName, custLocation) VALUES(?,?,?)

private void insert()
{
try
{
pstmt = conn.prepareStatement(insertStmt);

pstmt.setInt(1, 1000);
pstmt.setString(2, "Bob Sheppard");
pstmt.setString(3, "St. Louis, MO");
pstmt.addBatch();

pstmt.setInt(1, 1001);
pstmt.setString(2, "Jared Butcher");
pstmt.setString(3, "St. Louis, MO");
pstmt.addBatch();

int[] insertCounts = pstmt.executeBatch();

System.out.println("Insert Complete");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 11 2008
Added on Nov 12 2008
19 comments
209 views