首页 > 解决方案 > prepareStatement.executeUpdate(sql) 给出错误

问题描述

executeUpdate(sql)当我尝试使用准备好的语句更新 SQL 数据库时,我不知道为什么我的代码在错误。当我尝试在 Oracle SQL 终端中运行 SQL 查询时,它工作得很好。谁能帮帮我吗。我在下面添加了代码。

我试图调试,它显示错误

int iRs = cimStmt.executeUpdate(sql.toString());
public String updatePatternDiffRemarks(String sLot,
                                   String sRemarks,
                                   String sUserId,
                                   int iLangMode)
    {
        StringBuffer sql        = null;

        Connection con          = null;
        PreparedStatement stmt  = null;


        StringBuffer rtnMsg     = new StringBuffer("");

        try {
            con = CimDbConnect.getDbConnection();
            if (con == null) {
                throw new Exception("Failed DB connect.");
            }
            con.setAutoCommit(false);

            sql = new StringBuffer();
            sql.append("   UPDATE ");
            sql.append("   CIM.PRODUCT_BPM_DIFF_COMMENT_TBL ");
            sql.append("   SET REMARKS = ? ");
            sql.append(" , LAST_MODIFIED_DATE = sysdate ");
            sql.append(" , USER_ID = ? ");
            sql.append("   WHERE ");
            sql.append("   LOT_NO = ? ");

            stmt = con.prepareStatement(sql.toString());
            CimPreparedStatement cimStmt = new CimPreparedStatement(stmt);

            int index = 1;
            cimStmt.setString(index++, sRemarks);
            cimStmt.setString(index++, sUserId);
            cimStmt.setString(index++, sLot);

            int iRs = cimStmt.executeUpdate(sql.toString());
            con.commit();

        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch(Exception e) {

            e.printStackTrace();

            rtnMsg = CimMessage.setMsgStyleCForHtml(rtnMsg, e.getMessage(), false);
            CimMessage.putErrMsgEx(e,  this.getClass().getName() + ": updatePatternDiffRemarks Exception!!");

            if(con != null) {
                try {
                    con.rollback();
                } catch(Exception ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            try {
                if(stmt != null) {
                    stmt.close();
                }
                if(con != null) {
                    con.close();
                }

            } catch(Exception e) {
                CimMessage.putErrMsgEx(e,  this.getClass().getName() + ": updatePatternDiffRemarks Exception!!");
            }
        }
        return rtnMsg.toString();
    }

标签: javasql

解决方案


你可以尝试像这个例子一样使用分隔:

public void closeConn(PreparedStatement ps) throws SQLException {
        if (ps != null) {
            ps.close();
        }
    }

public void updateService(Servs srv){
        String sql = "UPDATE services SET name=?, description =? , content = ? , img =  ? WHERE id = ? ";
        PreparedStatement ps = null;
        try{
            ps = super.prepareStatement(sql);
            ps.setString(1, srv.getName());
            ps.setString(2, srv.getDescription());
            ps.setString(3, srv.getContent());
            ps.setString(4, srv.getImg());
            ps.setInt(5, srv.getId());
            ps.execute();
        }catch(SQLException|NullPointerException e){    
            logger.error("Error UPDATE  >> ",e);
        }
        finally {
            closeCn(ps);
        }
    }// CRUD UPDATE

推荐阅读