通过JDBC发送DEFAULT占位符? [英] Sending the DEFAULT placeholder via JDBC?

查看:81
本文介绍了通过JDBC发送DEFAULT占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有通过JDBC显式发送DEFAULT占位符的方法,就像在INSERT INTO sometables VALUES (blah, DEFAULT)中一样? (我几乎可以肯定答案是否",但是我正在寻找JDBC专家确认).

Is there any way, via JDBC, to send the DEFAULT placeholder explicitly, like in INSERT INTO sometables VALUES (blah, DEFAULT)? (I'm almost certain the answer is "no", but I'm looking for JDBC-expert confirmation).

假设您有一个PreparedStatement,例如:

INSERT INTO mytable(a, b) VALUES (?, ?)

用于表格:

CREATE TABLE mytable (
    a integer,
    b integer default some_function()
);

,您想在某些批处理中将数据库集DEFAULT用于mytable.b,而在其他批处理中则不使用.

and you wanted to use the database-set DEFAULT for mytable.b in some executions in a batch but not others.

在常规SQL中,您将编写:

In regular SQL you'd write:

INSERT INTO mytable(a, b) VALUES (1, 42)
INSERT INTO mytable(a, b) VALUES (2, DEFAULT);
...

或者当然是

INSERT INTO mytable(a, b) VALUES (1, 42)
INSERT INTO mytable(a) VALUES (2);

...但是您无法通过JDBC执行此操作. setString("DEFAULT")当然不会发送DEFAULT关键字,而只是发送字面意义上的'DEFAULT'.

... but you can't do this via JDBC. setString("DEFAULT") will of course not send the DEFAULT keyword, just the string-literal 'DEFAULT'.

在任何广泛使用的驱动程序中,是否有一种方法可以设置占位符参数,该参数表示DEFAULT?

Is there a way to set a placeholder-parameter that means DEFAULT in any widely used drivers?

我看不到使用标准API和规范完成此操作的方法.

I don't see a way to do it with the standard API and spec.

我在想像这样的东西

pstmt.setObject(2, Postgresql.DEFAULT, Types.OTHER);

其中,Postgresql.DEFAULT是一个特殊的占位符实例,因为对于

where Postgresql.DEFAULT is a special placeholder instance, since there doesn't seem to be a setDefault() method for PreparedStatement.

任何现有的驱动程序都支持吗?

Does any existing driver support this?

推荐答案

没有标准的方法.据我所知,SQL标准不支持通过参数声明使用DEFAULT的机制. SQL标准似乎假定每个INSERT都是为特定目的而设计的.因此,声明DEFAULT只能在insert语句本身中进行,而不能作为参数的值进行.在这些决策中,JDBC规范通常遵循SQL标准.

There is no standard way of doing this. As far as I know the SQL standard does not support a mechanism for declaring to use the DEFAULT through parameters. The SQL standard seems to assume that each INSERT is crafted for its specific purpose. So declaring DEFAULT can only be done in the insert statement itself and not as a value for a parameter. In these kinds of decisions, the JDBC specification usually follows the SQL standard.

当前唯一的方法是在JDBC驱动程序中创建特定于供应商的方法,例如您在问题本身中指定的方法,但是您还可以想到以下内容:

The only method you currently have is to create a vendor-specific method in the JDBC driver, for example as you specified in your question itself, but you could also think of something like:

需要澄清的是:下面是一个驱动程序实现如何解决它的示例,它实际上并不是这样工作的.

使用

setNull(idx, PostgresTypes.DEFAULT_VALUE)

或使用 setNull(int, int, String)

setNull(idx, Types.<correct-field-type>, Postgres.DEFAULT_VALUE_MARKER)

但是,这假设PostgreSQL实际上有一种通过参数指定DEFAULT的方法,或者驱动程序实现将解析并重新创建每组接收到的参数的语句,以便它可以声明文字DEFAULT.

However this assumes that PostgreSQL actually has a method of specifying DEFAULT through parameters, or that the driver implementation will parse and recreate the statement for each set of received parameters so that it can declare a literal DEFAULT.

我不知道当前是否有任何支持这种解决方法的驱动程序.

I don't know if there is any driver that currently supports such a workaround.

这篇关于通过JDBC发送DEFAULT占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆