0

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

try{     
        Connection con = ConnectionProvider.getCon();
        stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1,?,?,?,?,?");
        stmt.setString(1,obj_Leaverequest.getUser_name());
        stmt.setString(2,obj_Leaverequest.getLeave_Type());
        stmt.setInt(3,obj_Leaverequest.getLeave_Units());
        stmt.setString(4,obj_Leaverequest.getLeave_Reason());
        stmt.setString(5,"pending");
        count=stmt.executeUpdate();
        if(count>0){
            status=true;
        }
    }

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? – rvheddeg 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
1

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.

1

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
0

@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.

Thanks

Vishad

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.