用于访问 PostgreSQL 数据库的 JDBC 驱动程序的替代方案是什么
我正在使用 PostgreSQL 的官方 JDBC 驱动程序,但我遇到了以下问题:
- 不支持 PostgreSQL-ish 数据结构,例如 UUID。
- 常见的 JDBC 怪异现象,例如:
- 没有函数可以转义 PostgreSQL 使用的值。
- 对批量执行异构语句的支持有限。
- 在一个表中插入多行时,无需将多个插入语句重写为单个插入语句。
那么问题来了——是否有任何 PostgreSQL 数据库驱动程序可以充分利用 PostgreSQL 的全部功能而无需太多样板?我也使用Scala语言进行开发,所以如果驱动程序是专门为Scala设计的,那就太棒了。
I'm using official JDBC driver for PostgreSQL, but I'm stuck with the following issues:
- No support for PostgreSQL-ish data structures such as UUIDs.
- Common JDBC weirdness, such as:
- No function to escape values for consuming by PostgreSQL.
- Limited support for executing heterogeneous statements in batch.
- No rewriting of multiple insert statements into single insert statement when inserting many rows in one table.
So, the question — is there any PostgreSQL database driver which can leverage full power of PostgreSQL without much boilerplate? I'm also use Scala language for development, so if driver is designed specifically for Scala it would be so much awesome awesome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
其中一些似乎是(除非我不理解)用户在使用 JDBC 时出现的错误。 JDBC 是一个相当丑陋的 API,所以永远不要问你是否能优雅地做到这一点,而只问你是否能做到这一点。
正如 @ColinD 和 @a_horse 指出的那样,应该使用准备好的语句和批处理操作来处理转义和插入多行。在幕后,我希望有一个好的 JDBC 实现来完成您想要的事情(我不熟悉 PostgreSQL 的实现)。
关于 UUID,这里是一个解决方案:
Some of this seems to be (unless I'm not understanding) user error in using JDBC. JDBC is a pretty ugly API, so never ask if you can do it elegantly, just ask if you can do it at all.
Escaping and inserting multiple rows should be handled, as @ColinD and @a_horse pointed out, with Prepared statements and batch operations. Under the hood, I would expect a good JDBC implementation to do the things you want (I am not familiar with PostgreSQL's implementation).
Regarding UUIDs, here is a solution:
该驱动程序支持批处理语句以加速批量插入。
并且使用批处理语句比使用专有的 INSERT 语法更可移植(据我所知,多行插入和批量插入之间没有太大区别)
查看PreparedStatement.addBatch()
UUID 的原因是不支持的原因可能是 UUID 不是 Postgres 核心的一部分,只是一个 contrib 模块。
编辑
关于执行异构语句
Postgres 驱动程序确实支持批处理中不同类型的语句。
以下工作正常:
尽管您确实失去了PreparedStatement 为您提供的自动转义功能。
The driver supports batched statements to speed up bulk inserts.
And using batched statements is a lot more portable than using proprietary INSERT syntax (and as far as I can tell, there is no big different between a multi-row insert and batched inserts)
Check out PreparedStatement.addBatch()
The reason why UUID is not supported is probably that UUID is not part of the Postgres core, just a contrib module.
Edit
Regarding the execute heterogeneous statements
The Postgres driver does support different types of statements in the a batch.
The following works fine:
Although you do lose the automatic escaping that PreparedStatement gives you.
我知道这并不能回答您的全部问题,但希望它仍然有用。
我正在使用 Java 6 和 Postgres 8.4。我使用的驱动程序在我的 Maven POM 文件中如下:
我将
PreparedStatement.getObject()
和PreparedStatement.setObject()
与 Java 的java.lang. util.UUID
类来检索和存储 UUID。例如:
和:
工作正常。
对于较新的驱动程序,还支持以下内容
I realise this doesn't answer your entire question, but hopefully it will be useful all the same.
I'm using Java 6 and Postgres 8.4. The driver I'm using is in my Maven POM file as:
I'm using
PreparedStatement.getObject()
andPreparedStatement.setObject()
with Java'sjava.util.UUID
class to retrieve and store UUIDs.For example:
and:
Works fine.
With newer drivers, the following is alsow supported
相反,用于 Postgres 9.x 确实支持 UUID 通过 setObject 和 getObject 命令。没有比这更直接或更简单的了(在任何数据库、Postgres 或任何其他数据库中),因为 JDBC 不将 UUID 识别为数据类型。
据我所知,没有必要按照 Yishai 的另一个答案中的建议创建一个辅助类。
无需进行任何转换或遍历字符串。
请参阅 我的博客文章以获取更多讨论和代码示例。
代码示例摘录:
On the contrary, the current JDBC driver (9.2-1002 JDBC 4) for Postgres 9.x does indeed support UUID via the setObject and getObject commands. You cannot get any more direct or simpler than that (in any database, Postgres or any other) because JDBC does not recognize UUID as a data type.
As far as I can tell, there is no need to create a helper class as suggest in another answer by Yishai.
No need to do any casting or go through strings.
See my blog post for more discussion and code example.
Code example excerpt:
看一下 O/R Broker,它是一个基于 Scala JDBC 的关系数据库访问库。
Take a look at O/R Broker, which is a Scala JDBC-based library for relational database access.
以下是一些自行实现(实验性)最小客户端库的实验: https://github.com /BeatEngine/postgresql-client-minimal
(我尝试在 Android API 级别 < 24 上使用它)
Here is some experiment with a self implemented (experimental) minimal client library: https://github.com/BeatEngine/postgresql-client-minimal
(I try to work with this on Android API level < 24)