java - SQL 更新无法正常工作

标签 java sql oracle

我正在开发 JSF 页面,该页面显示设置值并将设置更新到 Oracle 表中:

public void updateDBSettings() throws SQLException {

        /*
         -- TABLE GLOBALSETTINGS
            CREATE TABLE GLOBALSETTINGS(
            SETTINGNAME VARCHAR2(40 ) NOT NULL,
            SETTINGVALUE VARCHAR2(40 )
            )
            /
         */


        String SQL_Statement = null;

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

        PreparedStatement ps = null;

        try {
            conn.setAutoCommit(false);
            boolean committed = false;
            try {
                /* Insert the new settings values with one SQL statement */
                SQL_Statement = "UPDATE GLOBALSETTINGS "
                        + "SET \"SettingValue\" = "
                        + "CASE "
                        + "WHEN \"SettingName\" = 'SessionTTL' THEN ? "
                        + "WHEN \"SettingName\" = 'MaxUsersActive' THEN ? "
                        + "WHEN \"SettingName\" = 'TimestampFormat' THEN ? "
                        + "END "
                        + "WHERE \"SettingName\"  IN ('SessionTTL', 'MaxUsersActive', 'TimestampFormat')";

                /* Execute the SQL statement */
                ps = conn.prepareStatement(SQL_Statement);
                ps.setString(1, settingsMap.get("SessionTTL"));
                ps.setString(2, settingsMap.get("MaxUsersActive"));
                ps.setString(3, settingsMap.get("TimestampFormat"));               

                ps.executeQuery();
                conn.commit();
                committed = true;
            } finally {
                if (!committed) {
                    conn.rollback();
                }
            }
        } finally {
            /* Release the resource after all SQL queries are executed */
            ps.close();
            conn.close();
        }
        /* Refresh Hash Map
         * Get again settings from Oracle
         */
        initSettings();
        // Display the message
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Successful", "Settings are updated"));
    }

我在 Glassfish 错误日志中收到此错误:

javax.faces.FacesException: #{ApplicationController.updateDBSettings}: java.sql.SQLSyntaxErrorException: ORA-00904: "SettingName": invalid identifier

    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    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:1550)
    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:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    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)
Caused by: javax.faces.el.EvaluationException: java.sql.SQLSyntaxErrorException: ORA-00904: "SettingName": invalid identifier

    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 31 more
Caused by: java.sql.SQLSyntaxErrorException: ORA-00904: "SettingName": invalid identifier

    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:139)
    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 com.sun.el.parser.AstValue.invoke(AstValue.java:254)
    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
|#]

你能帮我解决这个问题吗?我确信问题出在 SQL 语句中。

最佳答案

您的 SQL 错误。在 Oracle 中创建表的方式,SQL 应该是(删除双引号转义)-

SQL_Statement = "UPDATE GLOBALSETTINGS "
    + "SET SettingValue = "
    + "CASE "
    + "WHEN SettingName = 'SessionTTL' THEN ? "
    + "WHEN SettingName = 'MaxUsersActive' THEN ? "
    + "WHEN SettingName = 'TimestampFormat' THEN ? "
    + "END "
    + "WHERE SettingName  IN ('SessionTTL', 'MaxUsersActive', 'TimestampFormat')";

当您使用以下 SQL 创建表时 -

CREATE TABLE GLOBALSETTINGS(
        SETTINGNAME VARCHAR2(40 ) NOT NULL,
        SETTINGVALUE VARCHAR2(40 ) )

这与创建表不同 -

CREATE TABLE "GlobalSettings" (
        "SettingName" VARCHAR2(40 ) NOT NULL,
        "SettingValue" VARCHAR2(40 ) )

使用第一种方法,Oracle 将所有内容转换为大写。如果您使用第二种方法,表/列名称将保留区分大小写。如果您使用第二种方法,则必须在 sql 中对表/列名称进行空格。使用第一种方法,您不应转义表/列名称。

关于java - SQL 更新无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11924956/

相关文章:

Java Servlet 使用 HTML 表单创建 MySQL DB 并对其进行排序

sql - 舍入到 SQL 中最接近的奇数整数

sql - Oracle Database 11g 企业版 11.2.0.4.0 版中的 STRAGG - 64 位生产

php - oci_fetch 与 oci_fetch_array

Java if-else条件改变输出颜色

java - Hibernate - 如何持久化 HashMap

mysql - 我怎样才能得到这个查询中每个技术人员最后完成的服务?

java - Java中如何检查表中是否存在某个值?

java - Selenium Java-需要从第三个 html 子元素中获取 innertext 值

sql - 当我在 nodejs 中使用 sequelize 进行 groupby 时,我被计数为 1 而不是 0