English 中文(简体)
无法在 DDL 语句中使用绑定变量。 替代品?
原标题:Cannot use bind variables in a DDL statement. Alternatives?

首先,前言:我正在用jdbc. Im使用JSE6和Oracle 11XE作为测试DB,写一个java类,在数据库中用jdbc建立临时表格,但该类也需要符合DB2。

我试图创建的临时表格来自更大的表格,我在数据上做了一些过滤和汇总。我根据的筛选参数是由运行时的用户决定的。我试图做的简化例子之一是:

CREATE TABLE temp_table AS (
    SELECT
           table1.department_id,
           SUM(CASE WHEN table1.number_1 < &1 THEN table1.number_1 ELSE 0 END)) AS column1              
    FROM
           table1
    GROUP BY table1.department_id
)

My problem is that I need to specify parameters to filter the data, and I need to be sure they re properly escaped/localized/typed. This would be easy using a prepared statement, but I cannot use bind variables with DDL. The temporary solution I resorted to is to alter the query String myself, writing the parameters in the correct place, but this means I now have to implement all the checks instead of relying on a PreparedStatement object to do it for me, on top of losing all the other benefits.

我调查了其他解决办法,但至今为止,没有一个办法说服我:

  1. 我首先可以创建一个空的温度表,然后用 INSERT 填入温度表(id, 第1栏) (SELECT...) (SELECT...) (SELECT.) ) 但它看起来可能会造成性能损失, 所以我想继续使用 CREATE 温度表

  2. 我本想建立一个临时声明来保持内部 SELECT 查询, 并且让它产生一个正确的格式化/ 本地化/ etc. 查询字符串, 但我还没有找到任何方法从它获得最后查询( 我读到它绝对不可能 < a href=> https:// stackoverflow.com/ problemss/46926/ pactal- sql- statement- faf- bind- evid- 可变性- execredition > 。 我为本案找到的唯一选择是使用 < a href=" http://www.javaworld.com/javaworld/jw-01-2002/jw-0125-overpower.html? page=1, rel= “ nofolvegrer” > Debuggable Statement , 但我不确定我能否把它包括在这个项目里( 另外, 这似乎是解决我问题的一个相当隐含的方法)

  3. 我想到的另一个解决办法是,简单地将创建临时表格的询问(对于其中每一个问题,我将整个CREATE AST(SELECT...)放在数据库中,在一个程序内,然后我就可以用可调用声明调用。 这样我就可以避免处理打字和仍然有良好的表现,代价是更严格地与 db 连接(我必须确保程序已经存在,或者在 java 管理它们从 db 上增加/删除)

因此,我的问题是:有没有比我能想到的更好的替代办法?

最佳回答

这是数据库不可知性, 还是你只瞄准甲骨文? 您不必在存储程序内存储 PL/ SQL 来使用它; 只要建立一个匿名的 PL/ SQL 块来做你需要的东西, 然后执行它。 匿名的 PL/ SQL 块可以动态地建立, 以便在 PL/ SQL 中声明强烈的输入变量以保持您的参数, 然后您的 java 代码将数值粘贴在其中 。 类型安全不会由 Java 处理, 因为您正在构建一个字符串; 当您执行匿名的 PL/ SQL 块时, 它将会由 Oracle 处理 。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签