我的无效字符在哪里 (ORA-00911)

2022-08-31 14:17:12

我正在尝试将 s 插入到数据库中(请参阅相关问题)。我不太清楚出了什么问题。我有一个列表,其中包含要插入到表中的大约85个clob。即使只插入第一个 clob,我也会得到 .我无法弄清楚如何在语句执行之前将其取出,因此我无法100%确定它是正确的,但是如果我做对了,那么它应该看起来像这样:CLOBORA-00911: invalid characterPreparedStatement

insert all
  into domo_queries values ('select 
substr(to_char(max_data),1,4) as year,
substr(to_char(max_data),5,6) as month,
max_data
from dss_fin_user.acq_dashboard_src_load_success
where source = ''CHQ PeopleSoft FS''')
select * from dual;

最终,这个插入所有语句会有很多's,这就是为什么我只是不做一个常规语句。我没有看到一个无效的字符,对吧?(哦,当我在我的sql开发人员工具中运行它时,上面的代码运行良好。如果我去掉中的分号,就会抛出一个错误。intoinsertPreparedStatementORA-00933: SQL command not properly ended

无论如何,这是我执行查询的代码(以及上面示例的变量值)。

public ResultSet executeQuery(String connection, String query, QueryParameter... params) throws DataException, SQLException {
  // query at this point = "insert all
                          //into domo_queries values (?)
                          //select * from dual;"
  Connection conn = ConnectionPool.getInstance().get(connection);
  PreparedStatement pstmt = conn.prepareStatement(query);
  for (int i = 1; i <= params.length; i++) {
    QueryParameter param = params[i - 1];
    switch (param.getType()) { //The type in the example is QueryParameter.CLOB
      case QueryParameter.CLOB:
        Clob clob = CLOB.createTemporary(conn, false, oracle.sql.CLOB.DURATION_SESSION);
        clob.setString(i, "'" + param.getValue() + "'");
        //the value of param.getValue() at this point is:
        /*
         * select 
         * substr(to_char(max_data),1,4) as year,
         * substr(to_char(max_data),5,6) as month,
         * max_data
         * from dss_fin_user.acq_dashboard_src_load_success
         * where source = ''CHQ PeopleSoft FS''
         */
        pstmt.setClob(i, clob);
        break;
      case QueryParameter.STRING:
        pstmt.setString(i, "'" + param.getValue() + "'");
        break;
    }
  }
  ResultSet rs = pstmt.executeQuery(); //Obviously, this is where the error is thrown
  conn.commit();
  ConnectionPool.getInstance().release(conn);
  return rs;
}

有什么是我刚刚错过了很多时间吗?


答案 1

如果您完全按照向我们展示的那样使用字符串文本,则问题在于末尾的字符。您不能在 JDBC 调用的查询字符串中包含该内容。;

由于您只插入一行,因此即使插入多行,常规也应该很好。使用批处理语句可能会更有效。不需要 .此外,您不需要临时 clob 和所有这些。你可以将你的方法简化为这样的(假设我得到了正确的参数):INSERTINSERT ALL

String query1 = "select substr(to_char(max_data),1,4) as year, " + 
  "substr(to_char(max_data),5,6) as month, max_data " +
  "from dss_fin_user.acq_dashboard_src_load_success " + 
  "where source = 'CHQ PeopleSoft FS'";

String query2 = ".....";

String sql = "insert into domo_queries (clob_column) values (?)";
PreparedStatement pstmt = con.prepareStatement(sql);
StringReader reader = new StringReader(query1);
pstmt.setCharacterStream(1, reader, query1.length());
pstmt.addBatch();

reader = new StringReader(query2);
pstmt.setCharacterStream(1, reader, query2.length());
pstmt.addBatch();

pstmt.executeBatch();   
con.commit();

答案 2

在我的头顶上,你可以尝试使用'q'运算符作为字符串文本

类似的东西

insert all
  into domo_queries values (q'[select 
substr(to_char(max_data),1,4) as year,
substr(to_char(max_data),5,6) as month,
max_data
from dss_fin_user.acq_dashboard_src_load_success
where source = 'CHQ PeopleSoft FS']')
select * from dual;

请注意,谓词的单引号未转义,字符串位于 q'[...]' 之间。


推荐