executeUpdate()仅将返回值设为1

时间:2016-01-29 12:08:11

标签: java database

我之前问过同样的问题,没有得到答案 我正在从前端更新数据库中的一行,但我总是得到返回值为1 无论查询是否正在更新,我故意尝试插入错误的值或尝试违反约束,我在后端获得异常,但返回值始终为1

java代码:

 public int editportal(String env,String region,String url,String user,String rnode,String password,int envid,String attuid) throws Exception
   {
    int Affected=0; 
    log.debug("[Editconsole Method Call]");

     try {
      Connection con = db.getConnection();
      String editquery = "update ENV_MOTS_DETAILS set " +
            "ENV_NAME= ? , CONSOLE_URL= ? , REMOTE_NODE= ? , CONSOLE_TO_LOGON=? , " +
            "REGION=? , DELETE_FLAG='Y' , ADMIN_PWD=? , " +
            "COMPONENT='' , COMMENTS='' , LAST_UPDATED_BY=?, " +
            "LAST_UPDATED_ON=to_date(to_char(SYSDATE,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS') WHERE ENV_ID = ?";

      log.info("[Editconsole Method Call Query]"+editquery);

      PreparedStatement ps1 = con.prepareStatement(editquery);

      ps1.setString(1,env);
      ps1.setString(2,url);
      ps1.setString(3,rnode);
      ps1.setString(4,user);
      ps1.setString(5,region);
      ps1.setString(6,password);
      ps1.setString(7,attuid);
      ps1.setInt(8,envid);

     Affected = ps1.executeUpdate();

     log.info("Statement affected " + Affected + " rows");
     ps1.close();
     con.close();

    } 
     catch (Exception e) 
     {
        log.fatal("[SQL Exception in Edit Server Details]"+e.getMessage());
        throw e;

      }
    return Affected;      
 }

当我故意尝试获取错误或返回值为0时的堆栈跟踪

java.sql.SQLException:ORA-12899:值太大而不能列#34; INFRAJEWL"。" ENV_MOTS_DETAILS"。" ADMIN_PWD" (实际:147,最大值:50)

    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
    at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
    at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:573)
    at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1891)
    at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:1093)
    at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2047)
    at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1940)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2709)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:589)
    at com.amdocs.infraportal.query.GetServerInfoDao.editportal(GetServerInfoDao.java:202)
    at com.amdocs.infraportal.servlets.Edit.doPost(Edit.java:45)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)

2 个答案:

答案 0 :(得分:3)

查看executeUpdate()的文档。它的返回值不应用于确定更新是否成功。

答案 1 :(得分:-1)

executeUpdate()的{​​{1}}方法始终返回

  • SQL数据操作语言(DML)语句的行数或
  • 0表示没有返回任何内容的SQL语句

来自https://stackoverflow.com/a/25659518/715269

的参考资料