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

我正在尝试将CLOB插入数据库(参见related question).我无法弄清楚出了什么问题.我有一个大约85个clobs的列表,我想插入表中.即使只插入第一个clob,我也会得到ORA-00911:无效字符.我无法弄清楚如何在执行之前将语句从PreparedStatement中删除,所以我不能100%确定它是正确的,但是如果我做对了,那么它看起来应该是这样的:

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;

最终,这个insert all语句会有很多内容,这就是我不做常规插入语句的原因.我没有在那里看到无效的角色,对吗? (哦,当我在我的sql开发人员工具中运行它时,上面的代码运行正常.)如果我在PreparedStatement中删除分号,它会抛出ORA-00933:SQL命令没有正确结束错误.

在任何情况下,这是我执行查询的代码(以及上面示例的变量值).

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;
}

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

最佳答案
如果您完全按照向我们展示的那样使用字符串文字,则问题是:最后的角色.您可能不会在JDBC调用的查询字符串中包含它.

由于您只插入一行,因此即使插入多行,常规INSERT也应该没问题.使用批处理语句可能更有效率.不需要INSERT ALL.此外,您不需要临时clob和所有这些.您可以将方法简化为类似的方法(假设我的参数正确):

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();

转载注明原文:java – 我的无效字符在哪里(ORA-00911) - 代码日志