2012-03-16 101 views
5

我有一個包含2列的表,用於在Oracle數據庫中存儲應用程序設置。這是隻是基本的例子:SQL如何使用一條SQL語句更新錶行

enter image description here

我想創建與準備好的聲明中更新值的Java方法。

示例代碼:

更新的代碼

public void updateDBSettings() throws SQLException { 


      String SQL_Statement = null; 

      if (ds == null) throw new SQLException();  
     Connection conn = ds.getConnection(); 
      if (conn == null) throw new SQLException();  

     try { 
      conn.setAutoCommit(false); 
      boolean committed = false; 
       try { 
         SQL_Statement = "UPDATE GLOBALSETTINGS SET (SettingName = ?, SettingValue = ?)"; 

         PreparedStatement updateQuery = conn.prepareStatement(SQL_Statement); 
         updateQuery.setString(1, "20"); 
         updateQuery.setString(2, "40"); 

         updateQuery.executeQuery(); 

         conn.commit(); 
         committed = true; 
       } finally { 
         if (!committed) conn.rollback(); 
         } 
      } 
       finally {    
       conn.close(); 

       } 

     }  

我知道這個SQL語句是錯誤的。編寫SQL語句的正確方法是什麼?

最良好的祝願 彼得

P.S更新代碼我得到這個錯誤堆棧後:

javax.faces.el.E​​valuationException:java.sql.SQLSyntaxErrorException:ORA-00907:缺少右括號

at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102) 
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) 
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) 
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) 
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161) 
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231) 
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317) 
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195) 
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849) 
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746) 
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045) 
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228) 
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) 
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) 
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) 
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) 
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) 
at com.sun.grizzly.ContextTask.run(ContextTask.java:71) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) 
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) 
at java.lang.Thread.run(Thread.java:722) 

產生的原因:java.sql.SQLSyntaxErrorException:ORA-00907:缺少右括號

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:445) 
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396) 
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:879) 
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:450) 
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:192) 
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:531) 
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:207) 
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1044) 
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1329) 
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3584) 
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3628) 
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1493) 
at com.sun.gjc.spi.jdbc40.PreparedStatementWrapper40.executeQuery(PreparedStatementWrapper40.java:642) 
at com.DX_57.SM_57.Application.updateDBSettings(Application.java:124) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:779) 
at javax.el.BeanELResolver.invoke(BeanELResolver.java:528) 
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:257) 
at com.sun.el.parser.AstValue.invoke(AstValue.java:248) 
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302) 
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) 
... 32 more 

回答

7

正確的語法是這樣的:

UPDATE GLOBALSETTINGS 
    SET settingValue = case 
         when settingName = 'SessionTTL' = then ? 
         when settingName = 'MaxUsersActive' = then ? 
         else settingValue 
         end 
WHERE settingName in ('SessionTTL', 'MaxUsersActive'); 

但是我不會推薦,因爲它使你的代碼很難閱讀和維護。

你可能最好運行兩個更新語句:

String sql = 
    "UPDATE GLOBALSETTINGS " + 
    " SET settingValue = ? " + 
    "WHERE settingName = ?"; 

PreparedStatement pstmt = conn.prepareStatement(sql); 
pstmt.setString(1, "40"); 
pstmt.setString(2, "SessionTTL"); 
pstmt.executeUpdate(); 

pstmt.setString(1, "20"); 
pstmt.setString(2, "MaxUsersActive"); 
pstmt.executeUpdate(); 

如果你想要一些往返保存到數據庫中,你可以作爲一個批處理語句運行此:

pstmt.setString(1, "40"); 
pstmt.setString(2, "SessionTTL"); 
pstmt.addBatch(); 

pstmt.setString(1, "20"); 
pstmt.setString(2, "MaxUsersActive"); 
pstmt.addBatch(); 

pstmt.executeBatch(); 

這將兩個語句「一次去」數據庫服務器。

注:我假設settingName是唯一的。

+0

你確定第一個例子是正確的嗎?我再次得到java.sql.SQLSyntaxErrorException:ORA-00905:缺少關鍵字。我如何在sqlplus中運行它來測試它?如果它在sqlplus中以這種方式運行,我會在命令行得到錯誤:3列:56 錯誤報告: SQL錯誤:缺少IN或OUT參數在索引:: 1 – 2012-03-16 14:48:31

+0

我的示例是Java代碼,SQL旨在成爲作爲準備好的語句運行(就像你的例子)。您不能在SQL * Plus中使用'?'運行這樣的SQL語句。你需要用真正的價值取代它們。 – 2012-03-16 15:03:38

+0

我在SQL開發人員的示例中運行:http://pastebin.com/aeb9FmQQ – 2012-03-16 15:57:41

0
SQL_Statement = "UPDATE GLOBALSETTINGS SET (SettingName = ?, SettingValue = ?)"; 
+0

我按照你的建議更新了代碼,但現在我得到了這個錯誤信息:javax.faces.el.E​​valuationException:java.sql.SQLSyntaxErrorException:ORA-00907:缺少右括號 – 2012-03-16 12:35:04

3
String sqlStatement = 
    "update GLOBALSETTINGS " + 
    "set SettingName = ?, " + 
    "SettingValue = ?" + 
    "where id = ?"; // You have to put where condition here, otherwise all rows will get affected. I assume your serch-key-column as id. Change 'id' according to your table 
PreparedStatement updateQuery = con.prepareStatement(sqlStatement); 
updateQuery.setString(1, "20"); 
updateQuery.setString(2, "40"); 
updateQuery.setString(3, applicationSettingId); 

您可以檢查出tutorials from oracle

+2

這很可能也需要一個'WHERE'條款 – 2012-03-16 12:47:02

+0

我想過要添加,但是我看了Peter提出的問題要求。 – 2012-03-16 12:56:07

+0

是的,這將更新所有行並覆蓋所有內容。所以提問的人表示要小心這個。 – 2012-03-16 14:26:25