如何使用JDBC引用/转义标识符(例如列名)? [英] How to quote/escape identifiers such as column names with JDBC?

查看:127
本文介绍了如何使用JDBC引用/转义标识符(例如列名)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不同的数据库服务器使用不同的方式来引用和转义标识符.

Different database servers use different ways to quote and escape identifiers.

例如"foo bar" vs`foo bar` vs [foo bar]或"10"""vs 10 \"",或者某些数据库需要引用诸如FooBar或数组之类的标识符,而其他数据库则不需要引用.

E.g. "foo bar" vs `foo bar` vs [foo bar], or "10""" vs "10\"", or identifiers such as FooBar or array need to be quoted for some databases but not for others.

对于给定的数据库连接,是否有任何API方法可以正确执行引用/转义?或任何其他解决方案?

Is there any API method that performs the quoting/escaping correctly for a given database connection? Or any alternative solution?

推荐答案

我认为您的问题的答案是,如果您正在使用JDBC编写数据库中立的应用程序,那么您需要使用数据库中立的名称,而不是那些需要每个数据库供应商进行特殊转义.

I think the answer to your question is that if you are writing a database neutral application using JDBC, then you need to use database neutral names, and not things that require special escaping per database vendor.

在JDBC中,我对此一无所知. ORM产品将处理此类问题.

There is nothing I know of in the JDBC which supports that. A ORM product will deal with such things.

如果您正在编写ORM,那么我认为每个受支持的数据库都需要一个单独的SQL生成类,只是为了处理所涉及的各种语法,因此您必须编写该类.您当然可以查看各种开源ORM的源代码,并查看它们如何处理.

If you are writing an ORM, then I would think need a seperate SQL generation class for each supported database, just to handle the various syntax involved, so you would have to write that. You can certainly look at the source code of the various open source ORM's out there and see how they handle it.

这篇关于如何使用JDBC引用/转义标识符(例如列名)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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