使用prepareStatement的Java JDBC中的占位符

问题描述

我在prepareStatment中使用以下查询。之前我在PL / sql过程中使用了相同的查询

 INSERT INTO dynamicentitygtt
    (entity_type,entity_id,entity_code,synonyms,action)
    WITH data_view AS
     ( -- ITEM table
      SELECT 'ITEMENTITY' entity_type,-- This separates inserted values
              item_id data_id,item_name data_name,item_desc data_desc,creation_date
        FROM itemde
      UNION ALL
      -- ORG table
      SELECT 'ORGENTITY' entity_type,-- This separates inserted values
              org_id,org_name,org_desc,creation_date
        FROM orgde
        UNION ALL
      -- Feature table
              SELECT 'FEATUREENTITY' entity_type,-- This separates inserted values
              FEATURE_id data_id,FEATURE_NAME data_name,FEATURE_DESC data_desc,CREATION_DATE
        FROM FEATURESDE
      )
    SELECT upper(t.entity_type),t.data_id,t.data_name,t.data_desc,CASE UPPER(p_update_mode)
             WHEN 'INCREMENTAL' THEN
               CASE
                 WHEN t.creation_date > b.last_update_date THEN
                   'modify'
                 WHEN t.creation_date < b.last_update_date THEN
                   'add'
               END
             WHEN 'FULL' THEN
              'add' 
           END action
      FROM data_view t
           LEFT JOIN ODA_REFRESH_DETAILS b
                  ON b.entity_type = t.entity_type
                 AND lower(p_update_mode )='INCREMENTAL'
     WHERE (upper(p_entity_type) = t.entity_type OR p_entity_type IS NULL)
       AND (UPPER(p_update_mode) = 'FULL'
            OR (UPPER(p_update_mode) = 'INCREMENTAL' AND b.entity_type IS NOT NULL)
           );  

我从上游收到2个变量p_entity_type和p_update_mode。因此我正在使用?设置这些值。 我的代码如下。无论这些变量放在哪里,我都将?放在后面,然后设置相应的值。

public int insertDataToGTT(String p_entity_type,String p_update_mode) throws Exception
{
    
    
    String sql= "INSERT INTO dynamicentitygtt
               (entity_type,action)
          WITH data_view AS( -- ITEM table
         SELECT 'ITEMENTITY' entity_type,-- This separates inserted values
          item_id data_id,creation_date
                      FROM itemde
      UNION ALL
              -- ORG table
      SELECT 'ORGENTITY' entity_type,-- This separates inserted values
          org_id,creation_date
    FROM orgde
    UNION ALL
  -- Feature table
          SELECT 'FEATUREENTITY' entity_type,-- This separates inserted values
          FEATURE_id data_id,CREATION_DATE
    FROM FEATURESDE
  )
SELECT upper(t.entity_type),CASE UPPER(?) //p_update_mode
         WHEN 'INCREMENTAL' THEN
           CASE
             WHEN t.creation_date > b.last_update_date THEN
               'modify'
             WHEN t.creation_date < b.last_update_date THEN
               'add'
           END
         WHEN 'FULL' THEN
          'add' 
       END action
  FROM data_view t
       LEFT JOIN ODA_REFRESH_DETAILS b
              ON b.entity_type = t.entity_type
             AND lower(?)='INCREMENTAL' //p_update_mode
 WHERE (upper(?) = t.entity_type OR ? IS NULL)  //p_entity_type(both places)
   AND (UPPER(?) = 'FULL'  //p_update_mode
        OR (UPPER(?) = 'INCREMENTAL' AND b.entity_type IS NOT NULL) //p_update_mode
       ) "

    PreparedStatement statement= connection.prepareStatement(sql);
    statement.setString(1,p_update_mode);
    statement.setString(2,p_update_mode);
    statement.setString(3,p_update_mode);
    statement.setString(4,p_entity_type);
    statement.setString(5,p_entity_type);
    statement.setString(6,p_update_mode);

    
    int x=statement.executeUpdate();
    System.out.println("Number of rows inserted: "+x);
    return x;
}

这给了我以下例外。我检查了我设置正确的参数。实际上,我在多个地方使用相同的值。反正有什么更好的办法吗?

Exception in thread "main" java.sql.sqlSyntaxErrorException: ORA-00923: FROM keyword not found where expected

    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:494)
    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:446)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1052)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:537)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:255)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:610)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:253)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:86)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:928)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1136)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3640)
    at oracle.jdbc.driver.T4CPreparedStatement.executeInternal(T4CPreparedStatement.java:1384)
    at oracle.jdbc.driver.OraclePreparedStatement.executeLargeUpdate(OraclePreparedStatement.java:3730)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3710)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1061)
    at oracle.apps.vof.v1.oda.dao.NewCallableDAONEW.insertDataToGTT(NewCallableDAONEW.java:106)
    at oracle.apps.vof.v1.oda.server.ODADynamicEntity.getDynamicEntities(ODADynamicEntity.java:143)
    at oracle.apps.vof.v1.oda.server.ODADynamicEntity.main(ODADynamicEntity.java:43)
Caused by: Error : 923,Position : 1028,sql = INSERT INTO dynamicentitygtt
    (entity_type,CASE UPPER(:p_update_mode) //p_update_mode
             WHEN 'INCREMENTAL' THEN
               CASE
                 WHEN t.creation_date > b.last_update_date THEN
                   'modify'
                 WHEN t.creation_date < b.last_update_date THEN
                   'add'
               END
             WHEN 'FULL' THEN
              'add' 
           END action
      FROM data_view t
           LEFT JOIN ODA_REFRESH_DETAILS b
                  ON b.entity_type = t.entity_type
                 AND lower(:p_update_mode)='INCREMENTAL' //p_update_mode
     WHERE (upper(:p_entity_type) = t.entity_type OR :p_entity_type IS NULL)  //p_entity_type(both places)
       AND (UPPER(:p_update_mode) = 'FULL'  //p_update_mode
            OR (UPPER(:p_update_mode) = 'INCREMENTAL' AND b.entity_type IS NOT NULL) //p_update_mode
           ),Originalsql = INSERT INTO dynamicentitygtt
    (entity_type,Error Msg = ORA-00923: FROM keyword not found where expected

    at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:498)
    ... 17 more

同一查询在pl / sql中正常工作

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)