i am new to JDBC and trying following code. Could not find the error in it

        Connection con = ConnectionProvider.getCon();
        stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1,?,?,?,?,?");

Getters are working fine, i could print their values . i guess i am wrong with the query.Please point out my mistake. Thanks in advance!!

  • 1
    Are you inserting a question mark into the table? ifso add '' around the ? eg '?'. You have to tell Oracle it's a string. Not used to work with Java so could be wrong. – Munchi Dec 24 '13 at 12:48
  • @Munchi no i guess , no need of adding quotes . Anyhow thanks for your attempt :) – San Krish Dec 24 '13 at 12:54
  • 1) What are you trying to do? 2) What is the structure of the table? 3) What error do you get? – Roeland Van Heddegem Dec 24 '13 at 14:56
  • @Munchi For your future reference, the '?' are binds – Philᵀᴹ Dec 24 '13 at 19:00
  • So he messed up with his query? He got 1 value and trying to insert 5? – Munchi Dec 26 '13 at 8:57

Specify the columns and use SELECT:

"insert into create_request (col1,col2,etc..col6) 
select ?,?,?,?,?, count(reqno) +1 from create_request"

Just make sure the values and columns match.


I guess you have forgotten a ')'.

try this:

stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1),?,?,?,?,?");
  • @rheddeg nope :( it doesnt work – San Krish Dec 24 '13 at 12:40

@Krish: If your Getters are correct, then can you try below options:

a.) Try using

stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1,?,?,?,?,?)");

b.) In case the above one does not work, try running your insert query in database directly with the approprate values for the bind variables.

Please let us know if it works.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.