我们可以只使用 odbc 和 java 连接数据库吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4378493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:05:58  来源:igfitidea点击:

Can we use odbc only with java to connect to databases?

javajdbcodbc

提问by Mishthi

Do we always have to use jdbc with Java programs for making connectivity with database or can we use only odbc for connecting to databases with Java programs?

我们是否总是必须使用 jdbc 和 Java 程序来连接数据库,或者我们可以只使用 odbc 来连接 Java 程序的数据库?

采纳答案by rustyx

Sun JRE contains a built-in JDBC/ODBC driver (sun.jdbc.odbc.JdbcOdbcDriver). Here's an example how to use it: http://www.javacoffeebreak.com/articles/jdbc/

Sun JRE 包含一个内置的 JDBC/ODBC 驱动程序 (sun.jdbc.odbc.JdbcOdbcDriver)。这是如何使用它的示例:http: //www.javacoffeebreak.com/articles/jdbc/

The driver was removed in Oracle JRE 8, so use Java version 7 or earlier.

该驱动程序已在 Oracle JRE 8 中删除,因此请使用 Java 版本 7 或更早版本。

回答by systempuntoout

You can't use ODBCdirectly because your JAVA program needs to use the JDBCdriver to interact with the Database.

不能直接使用ODBC,因为你的JAVA程序需要使用JDBC驱动与数据库交互。

回答by Berin Loritsch

As others have mentioned you can use the JDBC/ODBC bridge driver. (Repeating @Rustam's link here: http://www.javacoffeebreak.com/articles/jdbc/).

正如其他人提到的,您可以使用 JDBC/ODBC 桥驱动程序。(在此处重复@Rustam 的链接:http: //www.javacoffeebreak.com/articles/jdbc/)。

There are a couple things to keep in mind when using the JDBC-ODBC bridge. First: it's use was not recommended by Sun for various reasons. The top three implications of using the bridge instead of a proper JDBC driver are:

在使用 JDBC-ODBC 桥接器时,有几件事情需要牢记。第一:由于各种原因,Sun 不推荐使用它。使用桥而不是正确的 JDBC 驱动程序的前三个含义是:

  • Not every feature of JDBC is supported. ODBC is a more restrictive API, so some features (like savepoints in transactions) are not supported. However, the most common features like prepared statements are.
  • The native code to Java runtime translation is much slower than if you were doing everything in Java.
  • The JDBC/ODBC driver is more fragile than the appropriate JDBC driver. Essentially, if implementers of the ODBC driver don't do things a certain way, the JDBC driver will fail and throw some extra exceptions you might not be able to catch. In particular, you will be more susceptible to memory leaks. If you aren't building a long running service you might be OK.
  • 并非所有 JDBC 功能都受支持。ODBC 是一种限制性更强的 API,因此不支持某些功能(例如事务中的保存点)。但是,最常见的功能如准备好的语句是。
  • 将本机代码转换为 Java 运行时转换比在 Java 中执行所有操作要慢得多。
  • JDBC/ODBC 驱动程序比适当的 JDBC 驱动程序更脆弱。本质上,如果 ODBC 驱动程序的实现者不以某种方式做事,JDBC 驱动程序将失败并抛出一些您可能无法捕捉到的额外异常。特别是,您将更容易受到内存泄漏的影响。如果您不构建长期运行的服务,则可能没问题。

That said, the JDBC/ODBC driver will work for a database that does not have direct JDBC support (most major databases do). Sometimes you don't need all those fancy features and just want to throw something together quickly. The JDBC/ODBC driver is designed for that.

也就是说,JDBC/ODBC 驱动程序将适用于没有直接 JDBC 支持的数据库(大多数主要数据库都支持)。有时您不需要所有这些花哨的功能,只想快速地将一些东西组合在一起。JDBC/ODBC 驱动程序就是为此而设计的。

回答by Vanchinathan Chandrasekaran

Short answer : NO.

简短回答:不。

ODBC ( Open Database Connectivity ) hides the details of what database you are talking to. It has nothing to do with Java. If java programs need to talk to the database, then they have to interact with ODBC drivers. To interact with ODBC drivers, you need JDBC-ODBC drivers which hides the details of how the communication happens. You can pretty much make a few method calls and all set to go. The power of abstraction.

ODBC(开放式数据库连接)隐藏了您正在使用的数据库的详细信息。它与Java无关。如果 Java 程序需要与数据库对话,则它们必须与 ODBC 驱动程序交互。要与 ODBC 驱动程序交互,您需要 JDBC-ODBC 驱动程序,它隐藏了通信如何发生的细节。您几乎可以进行一些方法调用,然后一切就绪。抽象的力量。

回答by Dave

My understanding is that you would not want to - it would become tedious and error prone when things dont go perfectly.

我的理解是你不会想要 - 当事情不完美时,它会变得乏味和容易出错。

I.E. you can't catch an exception when/if you invoke a non java DLL from inside java.

IE 当/如果您从 java 内部调用非 java DLL 时,您无法捕获异常。

回答by Boris Pavlovi?

You can use JDBC-ODBC drivers

您可以使用JDBC-ODBC 驱动程序