Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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!!

share|improve this question
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

3 Answers 3

up vote 1 down vote accepted

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.

share|improve this answer

I guess you have forgotten a ')'.

try this:

stmt=con.prepareStatement("insert into create_request values((select count(reqno) from create_request)+1),?,?,?,?,?");
share|improve this answer
    
@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.

Thanks

Vishad

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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