jdbc驱动程序类型_JDBC驱动程序类型

本文详细介绍了JDBC驱动程序的工作原理,包括四种类型的JDBC驱动(类型1:桥驱动,类型2:本机API,类型3:网络协议,类型4:本机协议)及其优缺点。在生产环境中,类型3和类型4驱动因其灵活性和性能而常用。选择JDBC驱动主要取决于应用使用的数据库类型。
摘要由CSDN通过智能技术生成

jdbc驱动程序类型

A JDBC Driver is a software component, which allows a Java application to interact with a database. With a JDBC driver, an application is made capable of sending queries to a database and receiving results.

JDBC驱动程序是一个软件组件,它允许Java应用程序与数据库进行交互。 使用JDBC驱动程序,使应用程序能够向数据库发送查询并接收结果。

In this lesson, we will try to answer some simple but important questions related to these jdbc drivers like how they do what they do, how many types of drivers are there, etc. JDBC stands for Java DataBase Connectivity.

在本课中,我们将尝试回答一些与这些jdbc驱动程序有关的简单但重要的问题,例如它们的工作方式,驱动程序的类型等等。JDBC代表Java DataBase Connectivity。

JDBC驱动程序如何工作? (How does JDBC driver works?)

The Java JDBC API (Application Programming Interface) is a set of interfaces built upon the JavaSoft specification. These interfaces consist of a set of contract (or abstract) methods which its implementations must agree to, or must implement to conform to it. What this means is, the JDBC drivers are actually a set of classes that implement these interfaces.

Java JDBC API(应用程序编程接口)是一组基于JavaSoft规范构建的接口 。 这些接口由一组合同(或抽象)方法组成,其实现必须同意或必须实现才能与之兼容。 这意味着JDBC驱动程序实际上是实现这些接口的一组类。

As JDBC drivers follow the JavaSoft specification, they can virtually access any Database, execute commands with Spreadsheets and even some flat file formats! The only condition is that a JDBC driver must exist for that SQL based database.

当JDBC驱动程序遵循JavaSoft规范时,它们实际上可以访问任何数据库,使用电子表格甚至某些平面文件格式执行命令! 唯一的条件是该基于SQL的数据库必须存在JDBC驱动程序。

JDBC驱动程序类型 (JDBC Driver Types)

In this section, we will explore the types of JDBC drivers. But wait! Didn’t we say that JDBC follows a specification, still there are multiple types? Yes, a specification just informs what should be done and not how it should be done.

在本节中,我们将探讨JDBC驱动程序的类型。 可是等等! 我们不是说JDBC遵循规范,还是有多种类型吗? 是的,规范只是告知应该做什么,而不是应该怎么做。

Overall, JDBC driver types are used to categorize the technology used to connect to the database. Let’s see each of them here.

总体而言,JDBC驱动程序类型用于对用于连接数据库的技术进行分类。 让我们在这里看到他们每个人。

  1. 类型1 –桥驱动器 (Type 1 – Bridge Drivers)

Type 1 drivers are termed as Bridge Drivers. These bridge drivers translate all JDBC query calls into ODBC calls. This is an advantage as many drivers exist for the Open Database Connectivity (ODBC) technology which means, Type 1 drivers exist for all those databases as well.

类型1驱动程序称为网桥驱动程序。 这些桥驱动程序将所有JDBC查询调用转换为ODBC调用。 这是一个优点,因为开放数据库连接(ODBC)技术存在许多驱动程序,这意味着所有这些数据库也都具有Type 1驱动程序。

Due to following disadvantages, Type 1 Driver is rarely used in production applications:

由于以下缺点,类型1驱动程序很少在生产应用中使用:

  • Queries are slow as they need to go through ODBC conversion followed by native DB conversions.

    查询速度很慢,因为它们需要先进行ODBC转换,然后再进行本机DB转换。
  • Bridge driver must be installed on host application which may not always be possible.

    桥驱动程序必须安装在主机应用程序上,但并非总是可能的。
  1. 类型2 –本机API (Type 2 – Native API)

Type 2 Drivers are termed as Native Drivers. These native drivers translate all JDBC calls into native API database calls. Type 2 drivers are faster than Type 1 Bridge drivers as conversion happens directly to DB operations. This API isn’t completely written in Java as it access Native Database code as well.

类型2驱动程序称为本机驱动程序。 这些本机驱动程序将所有JDBC调用转换为本机API数据库调用。 类型2驱动程序比类型1桥驱动程序更快,因为转换直接发生在数据库操作中。 该API并非完全用Java编写的,因为它也可以访问本机数据库代码。

Due to following disadvantages, Type 2 Drivers are rarely used in production applications:

由于以下缺点,Type 2驱动程序很少在生产应用中使用:

  • Client-side library for a Database has to be installed which may not always be possible.

    必须安装数据库的客户端库,但这并非总是可能的。
  • Not many database vendors provide native Client-side library.

    没有多少数据库供应商提供本机客户端库。
  1. 类型3 –网络协议 (Type 3 – Network Protocol)

Type 3 Drivers are termed as Network Protocol or Middleware Drivers. These Network Protocol drivers translate all JDBC calls into database-specific calls. If a Java application needs to access multiple types of databases at the same time, type 3 is the preferred driver.

类型3驱动程序称为网络协议或中间件驱动程序。 这些网络协议驱动程序将所有JDBC调用转换为特定于数据库的调用。 如果Java应用程序需要同时访问多种类型的数据库,则类型3是首选驱动程序。

Type 3 Drivers are most used drivers in production applications due to following advantages:

由于以下优点,类型3驱动程序是生产应用程序中最常用的驱动程序:

  • Type 3 JDBC drivers are the most flexible JDBC driver as they don’t need any native binary code on the client machine.

    Type 3 JDBC驱动程序是最灵活的JDBC驱动程序,因为它们在客户端计算机上不需要任何本机二进制代码。
  • The middleware application can do many other things apart from JDBC logic like Caching, Load balancing on Database servers etc. which is an overall performance boost for any JEE application.

    中间件应用程序除了可以执行JDBC逻辑(如缓存,数据库服务器上的负载平衡等)外,还可以做很多其他事情,这可以提高所有JEE应用程序的性能。

Type 3 Driver does have some disadvantages as well:

Type 3驱动程序确实也有一些缺点:

  • The middleware tier of the application needs some Database specific coding as well. This is usually an accepted solution due to the flexibility it offers.

    应用程序的中间件层也需要一些特定于数据库的编码。 由于它提供的灵活性,通常是一种公认​​的解决方案。
  • The middleware application may add a latency. But that can be improved by better coding standards etc.

    中间件应用程序可能会增加延迟。 但这可以通过更好的编码标准等来改善。

Also, the driver manager class is a fully implemented class which connects an application to a data source, which is specified by a database URL.

同样,驱动程序管理器类是完全实现的类,它将应用程序连接到由数据库URL指定的数据源。

  1. 类型4 –本机协议 (Type 4 – Native Protocol)

Type 4 Drivers are termed as Native Protocol or Middleware Drivers. These Native Protocol drivers translate all JDBC calls into direct vendor-specific database calls. The type 4 driver is written purely in Java and is platform independent.

类型4驱动程序称为本机协议或中间件驱动程序。 这些本机协议驱动程序将所有JDBC调用转换为直接的特定于供应商的数据库调用。 Type 4驱动程序完全用Java编写,并且与平台无关。

Type 4 Drivers has a great advantage of it doesn’t needing any middleware application. Thus the performance is considerably improved. But Drivers are database specific and so, all vendors might not provide Type 4 driver for its usage.

Type 4驱动程序具有很大的优势,因为它不需要任何中间件应用程序。 因此,性能大大提高。 但是驱动程序是特定于数据库的,因此,所有供应商都可能不提供Type 4驱动程序来使用。

如何确定用于JDBC的驱动程序? (How to determine which driver to use for JDBC?)

We need to decide this based on the Database that we use in our application.

我们需要根据我们在应用程序中使用的数据库来决定这一点。

For example:

例如:

  • In case of MySQL, we need to use jdbc driver for MySQL provided here.

    如果是MySQL,我们需要使用此处提供MySQL的jdbc驱动程序。
  • If case of Oracle, we can use jdbc driver provided by Oracle.here.

    如果是Oracle,我们可以使用Oracle提供的jdbc驱动程序。 在这里

You can find JDBC drivers on the chosen DB’s official website. It is dependent on the JDK version as well as DB version.

您可以在所选数据库的官方网站上找到JDBC驱动程序。 它取决于JDK版本以及DB版本。

结论 (Conclusion)

In this lesson, we categorised 4 kinds of JDBC drives and looked at their advantages and disadvantages along with reasons to use them in production-grade web applications.

在本课程中,我们对4种JDBC驱动器进行了分类,并研究了它们的优缺点以及在生产级Web应用程序中使用它们的原因。

Feel free to share your views in comments below.

随时在下面的评论中分享您的观点。

翻译自: https://www.journaldev.com/17060/jdbc-driver-types

jdbc驱动程序类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值