Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
690 views
in Technique[技术] by (71.8m points)

java - Sending the DEFAULT placeholder via 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).

Say you had a PreparedStatement like:

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

for table:

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

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

In regular SQL you'd write:

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

or of course:

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

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

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

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

I'm imagining something like:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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:

To clarify: below is an example of how a driver implementation could solve it, it doesn't actually work like this.

Using setNull(int, int)

setNull(idx, PostgresTypes.DEFAULT_VALUE)

or using setNull(int, int, String)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...