问题
JDBC provides an API, which may be used to connect to different RDBMS or similar datastores. But the datastores differ in implementation (e.g. SQL dialects).
Is it possible to use JDBC in such a way, that my queries and statements work on most common RDBMS (e.g.: Oracle, PostgreSQL, SQL Server, MySQL)?
That question is interesting for me at two aspects: * Common SQL (INSERT, UPDATE, SELECT etc.) * Accessing Meta data (getting information about tables and columns)
I am currently experimenting with an self written persistence framework and want to plug a JDBC datastore under it. So if I write a JDBC datastore adapter, it would be nice, if it would work on most common RDBMS.
Thanks in advance
回答1:
No this is not possible because they serve two completely different purposes.
JDBC is an abstraction of the DBMS communication protocol, whereas SQL is a query language.
The queries written in SQL are sent to the server using that communcation protocol and the results of the queries are then returned through that protocol (in a DBMS independent way).
There seems to be a blurry line between the communication protocol and the queries as the JDBC API also specifies calls to retrieve meta data from the server (or from the result). But actually the driver is free to use any means of transportation that deems suitable. That does not necessarily need to be a SQL query.
回答2:
JDBC is an abstraction layer for RDBMS.
Unfortunately, as you already recognized in your question, the abstraction is leaky.
If you stick to the small subset of SQL and Metadata that is common to all RDBMS, you'll be fine. However in reality, you're very likely to quickly outgrow these limits.
回答3:
JDBC works as an abstraction of an RDBMS approximately to the same degree SQL does: basic queries are compatible enough, but you constantly bump into requirements that can only be implemented using vendor-specific features:
- autoincrementing columns
- paging
- internationalization
- etc.
来源:https://stackoverflow.com/questions/4949763/is-it-possible-to-use-jdbc-as-an-abstraction-layer-for-rdbms