准备好的声明不插入

时间:2013-12-24 12:02:27

标签: oracle jdbc insert prepared-statement

我是JDBC新手并尝试使用代码。无法在其中找到错误:

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;
        }
    }

更新: 如果我像这样替换我的代码它可以工作:

String sqQuery="insert into create_request values (?,?,?,?,?,?)";
        stmt=con.prepareStatement(sqQuery);
        stmt.setString(1,"(select count(reqno) from create_request)+1");
        stmt.setString(2,obj_Leaverequest.getUser_name());
        stmt.setString(3,obj_Leaverequest.getLeave_Type());
        stmt.setInt(4,obj_Leaverequest.getLeave_Units());
        stmt.setString(5,obj_Leaverequest.getLeave_Reason());
        stmt.setString(6,"pending");

Getters工作正常,我可以打印他们的价值观。我想我的查询错了。请指出我的错误。在此先感谢!!

1 个答案:

答案 0 :(得分:1)

String sqQuery="insert into create_request values (?,?,?,?,?,?)";
        stmt=con.prepareStatement(sqQuery);
        stmt.setString(1,"(select count(reqno) from create_request)+1");
        stmt.setString(2,obj_Leaverequest.getUser_name());
        stmt.setString(3,obj_Leaverequest.getLeave_Type());
        stmt.setInt(4,obj_Leaverequest.getLeave_Units());
        stmt.setString(5,obj_Leaverequest.getLeave_Reason());
        stmt.setString(6,"pending");

检查"?"准备好的报表索引你有6个,但你只需要5个

希望这有帮助