2

I am trying to insert multiple rows into Oracle table using prepared statement executebatch... I get the java.sql.BatchUpdateException: invalid argument(s) in call exception and my connection doesn't have a problem.. it's working in all other functions.....

I searched on the net and found invalid arguments in call only for java.sql.SQLException but not for BatchUpdateException.

Any help would be really appreciated.


public void insert(List<Employee> str, Connection con) throws SQLException {
    String sql = "insert into tbl_list(e_name,e_id) values (?,?)";
    PreparedStatement ps = con.prepareStatement(sql);
    con.setAutoCommit(false);
    for (int i = 1; i <= str.size(); i++) {
        ps.setString(1, str.get(i - 1).getName());
        ps.setString(2, str.get(i - 1).getId());
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();
    con.close();
}

The stacktrace is:

May 16, 2017 2:34:40 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/insert] threw exception [Request processing failed; nested exception is java.sql.BatchUpdateException: invalid arguments in call] with root cause java.sql.BatchUpdateException: invalid arguments in call at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10345) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:230) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) at dao.insert.preparedInsert(insert.java:322)

insert.java :322 is ps.executeBatch();

7
  • 1
    Well for one thing, you've said that you're going to insert five values in the row, but you're only providing two of them... what do you expect the other three to be? Commented May 16, 2017 at 8:51
  • str.get(i-1) but what if i==0? Commented May 16, 2017 at 8:58
  • changed it to two parameters as I created table with two parameters only ..edited the post accordingly....its still the same exception.....This is actually not the real code...but the original code looks like this Commented May 16, 2017 at 8:59
  • Can you show your Employee class? Commented May 16, 2017 at 9:21
  • both name and Id are strings if thats what u r asking Commented May 16, 2017 at 9:32

3 Answers 3

1

Well I would recommend specifiying column names in the query String too, but I think you problem lies in setting the arguuments.

And more specifically the problem is with ps.addBatch() call, in fact you better use Statement and its .addBatch(String sql) method accepts an SQL String so i will take in consideration the updated SQL query in each iteration, so just make sure to pass it in ps.addBatch(sql).

And why would your for loop start from 1 and then use .get(i-1), if it can simply start from 0?

Just update your code accordingly:

public void insert(List < Employee > str, Connection con) throws SQLException {
  String sql = "insert into tbl_list(e_name,e_id) values (?,?)";
  Statement ps = con.createStatement();

  for (int i = 0; i <= str.size(); i++) {
    ps.setString(1, str.get(i).getName());
    ps.setString(2, str.get(i).getId());
    ps.addBatch(sql); //Note the sql String here
  }
  ps.executeBatch();
  ps.close();
  con.close();
}

And if the problem persists with setting query arguments, why don't you just create the query in the loop itself and change it dynamically?

Here's what I suggest here:

for (int i = 0; i <= str.size(); i++) {
    String sql = "insert into tbl_list(e_name,e_id) values ('"+str.get(i).getName()+"','"+str.get(i).getId()+"')";
    ps.addBatch(sql);
}

You can take a look at this Batch Insert In Java – JDBC tutorial for further reading and for full options.

Sign up to request clarification or add additional context in comments.

7 Comments

ps.addBatch(sql); is giving java.sql.SQLException: unsupported feature
Have you used Statement ps = con.createStatement(); instead of PreparedSatement as I recommended? You can use the code I provided in my update.
i believe that you know this can cause SQL Injection, so be careful down-voter will not understand what you mean :)
I have used oracle insert all statement which is far more better perfomance wise as i can understand from the way my code is executing....got the functionality I wanted
@YCF_L Yes, That's why I provided the tutorial link, I just gave that suggestion for testing purposes. In the given article It clearly states that.
|
1

Your query contain (?,?,?,?,?,) 5 ? and a typo , in the end, but you only set 2 parameters to your query :

ps.setString(1, str.get(i-1).getName());
ps.setString(2,str.get(i-1).getId());

Instead you have to correct your query, and like @Berger mention in comment, you have to specify the columns names in your query :

String sql = "insert into tbl_list(col_name1, col_name2) values (?,?)";

Or you have to set the correct number of your parameters.


You have also to use con.setAutoCommit(false); and con.commit(); like this :

con.setAutoCommit(false); // disabling autoCommit is recommend for batching

PreparedStatement ps = con.prepareStatement(sql);

for (int i = 1; i <= str.size(); i++) {
    ps.setString(1, str.get(i - 1).getName());
    ps.setString(2, str.get(i - 1).getId());
    ps.addBatch();
}
ps.executeBatch();

con.commit();//commit statements to apply changes 

7 Comments

what did you mean it still the same thing, can you try to show us your full error @KrishnaChaitanya
@KrishnaChaitanya you ca use try{}catch(Exception e){e.printstacktrace()} to see the full error
good point @Berger , i totally forgot it, can i add it to my answer?
@YCF_L this is not actually the original code...but i edited the code according to the original one.....the cloumn names are specified in the original and the value of i starts from 1 it has no problem... the point in question is why am i getting invalid arguments in call which is an sql exception for connection errors.....my connection is totally fine and tthe addBatch() command is working fine....i have a problem with executeBatch();....Also, the size of my original list is around 8000
@KrishnaChaitanya what is the type of e_id in your table?
|
0

Check this loop. Here in case of i=0 it is fetching (0-1) that is -1. As we know List provides 0 based indexing so your str.get(..) should start from 0 not from -1

try this

     public void insert(List<Employee> str, Connection con) throws SQLException {
    String sql = "insert into tbl_list values (?,?)";
    PreparedStatement ps = con.prepareStatement(sql);
       for (Employee e:str) {
        ps.setString(1, e.getName());
        ps.setLong(2, e.getId());
        ps.addBatch();
       }
    ps.executeBatch();
    ps.close();
    con.close();
}

The stacktrace is:

`May 16, 2017 2:34:40 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [spring] in context with path [/insert] threw exception [Request processing failed; nested exception is java.sql.BatchUpdateException: invalid arguments in call] with root cause java.sql.BatchUpdateException: invalid arguments in call at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10345) at oracle.jdbc.driver.OracleStatementWrapper.executeBatch(OracleStatementWrapper.java:230) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) at org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297) at dao.insert.preparedInsert(insert.java:322)

insert.java :322 is ps.executeBatch();`

2 Comments

If your id is type of int or long take appropriate datatype like ps.setLong(2, e.getId());
e_id is actually a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.