我以前問過同樣的問題,沒有得到答案 我從前端更新數據庫中的一行,但我總是得到返回值爲1 無論查詢是否正在更新,我有意嘗試插入錯誤的值或試圖違反約束,我在後臺得到異常,但返回的值始終爲1executeUpdate()只給返回值爲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:值太大,列 「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)
如果你的** WHERE ENV_ID =?**這裏ENV_ID在你的數據庫中是唯一的,所以它會返回1嘗試如果這個值有兩個或更多 –
如果executeUpdate()不更新數據庫中的任何內容,則應返回0。 – user2004685
你能分享一個試圖插入不正確值的例子嗎?什麼是堆棧跟蹤?因爲如果你的'executeUpdate()'失敗了,我懷疑它會返回一些... –