使用PreparedStatementCreator插入记录(必须在Oracle和PostgreSQL上运行)
我正在尝试使用 Spring Jdbc 准备好的语句将记录插入表中。此代码必须在 Postgres 和 Oracle 上运行。我需要在插入后读取插入的记录密钥。
我在 Spring 文档 JDBC 章节中找到了以下代码,它说“这适用于 Oracle”。
请参阅此处的链接(第 12.2 节。 8).
它在 Postgres 上工作得很好,插入记录,我可以使用 keyHolder 读取插入的记录键。但是在 Oracle 上它失败并显示以下错误消息...
[junit] STDOUT [错误] [2011.11.04 01:26:04] ..... - 测试......失败。 [junit] 消息:PreparedStatementCallback; SQL[]; ORA-01400: 无法将 NULL 插入 ("SOME_USER"."SOME_TABLE"."ID") [junit] ;嵌套异常是 java.sql.SQLIntegrityConstraintViolationException: ORA-01400: 无法将 NULL 插入 ("SOME_USER"."SOME_TABLE"."ID")
这是代码...
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator()
{
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException
{
PreparedStatement ps = connection.prepareStatement("insert into some_table(xxx_name,xxx_date), new String[] { "id" });
ps.setString(1, "some name");
ps.setDate(2, "some date");
return ps;
}
}, keyHolder);
注意:我使用ojdbc6.jar(Oracle jdbc jar) ),commons dbcp jar
这是 applicationContext dataSource bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
我在 Postgres 和 Oracle 上都存在序列,用于表some_table_id_seq
。我必须使此代码适用于 Postgres(在开发计算机上运行)和 Oracle(在生产环境中运行)。任何帮助/想法表示赞赏。谢谢。
I am trying to use Spring Jdbc Prepared statement to insert records into a table. This code must run on Postgres and Oracle. I need to read the inserted record key after insertion.
I found the below code on Spring documentation JDBC chapter and it says "this works with Oracle".
See link here (section 12.2.8).
It works on Postgres nicely, inserts the record and I can read inserted record key with keyHolder. However on Oracle it fails with below error message...
[junit] STDOUT [ERROR] [2011.11.04 01:26:04] ..... - Test ...... failed.
[junit] Message: PreparedStatementCallback; SQL []; ORA-01400: cannot insert NULL into ("SOME_USER"."SOME_TABLE"."ID")
[junit] ; nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("SOME_USER"."SOME_TABLE"."ID")
Here is the code...
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator()
{
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException
{
PreparedStatement ps = connection.prepareStatement("insert into some_table(xxx_name,xxx_date), new String[] { "id" });
ps.setString(1, "some name");
ps.setDate(2, "some date");
return ps;
}
}, keyHolder);
Note: I'm using ojdbc6.jar (Oracle jdbc jar), commons dbcp jar
Here is applicationContext dataSource bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
I have sequences present both on Postgres and Oracle, for the table some_table_id_seq
. I have to make this code work for Postgres (runs on devs machines) and Oracle (runs on production). Any help/ideas appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有向我们展示 Postgres 和 Oracle 的表定义 (DDL),但我猜 ID 列在 Postgres 中被定义为
serial
,因此会自动从链接到的序列中检索新的 ID那列Oracle没有这样的概念。如果您不想手动调用
sequence.nextval
(Postgres 也可以这样做),您必须创建一个触发器来为该列分配下一个序列值。You have not shown us the table definition (DDL) for Postgres and Oracle, but I guess the ID column is defined as
serial
in Postgres and thus a new ID is automatically retrieved from the sequence that is linked to that columnOracle does not have such a concept. If you don't want to call
sequence.nextval
manually (which would be possible with Postgres as well) you have to create a trigger that assigns the next sequence value for that column.