【发布时间】:2012-05-30 11:58:51
【问题描述】:
我正在尝试将CLOBs 插入数据库(请参阅related question)。我无法完全弄清楚出了什么问题。我有一个要插入到表中的大约 85 个 clob 的列表。即使只插入第一个 clob,我也会得到ORA-00911: invalid character。我无法弄清楚如何在执行之前将语句从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 语句会有很多into,这就是为什么我不做一个常规的insert 语句。我没有在其中看到无效字符,是吗? (哦,上面的代码在我的 sql 开发人员工具中运行时运行良好。)如果我删除 PreparedStatement 中的分号,它会引发 ORA-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;
}
我错过了什么?
【问题讨论】:
-
你能把 PreparedStatement 看成一个字符串来看看它包含什么吗?
-
我在帖子中提到过。我不知道该怎么做。我在互联网上看到的只是由于某种原因它非常复杂......
-
一些 JDBC 驱动程序允许 toString() 向您显示来自 PreparedStatement 的查询,而有些则不允许。不确定甲骨文。
-
@DanArmstrong,不幸的是,这里不是这种情况。我在
toString()打印输出上得到oracle.jdbc.driver.OraclePreparedStatementWrapper@8870a2。 -
Oracle 是否支持查询日志记录。如果是这样,那么您可以在查询到达服务器时看到它。
标签: java oracle jdbc prepared-statement