如何在同一应用程序中使用多个JDBC驱动程序? [英] How to use multiple JDBC drivers in the same application?

查看:197
本文介绍了如何在同一应用程序中使用多个JDBC驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,一旦执行

Class.forName("net.sourceforge.jtds.jdbc.Driver");

我初始化应用程序以全局使用JTDS SQL Server驱动程序,并且

I initialize the application to use JTDS SQL Server driver globally and

java.sql.DriverManager.getConnection(url, user, password);

之后将全部返回SQL Server连接.

returns SQL Server connections all after that.

但是,如果我想在同一功能中使用多个不同的数据库引擎,先获得JTDS SQL Server连接,然后再获得一个PostgreSQL连接,然后再重新建立一个新的JTDS SQL Server连接,该怎么办?

But what if I want to work with multiple different database engines in the same function, getting a JTDS SQL Server connection, then, for example a PostgreSQL connection and then a new JTDS SQL Server connection again?

推荐答案

您误会了.当您使用Class.forName()加载驱动程序类时,该驱动程序会在驱动程序管理器中注册自己.您可以使用任意数量的驱动程序来完成此操作.

You misunderstand. When you load a driver class with Class.forName(), that driver registers itself with the driver manager. You can do this with as many drivers as you want.

getConnection()的第一个参数是一个URL,它将唯一地标识用于该连接的驱动程序.

The first parameter of getConnection() is a URL that will uniquely identify the driver to use for that connection.

但是,我建议您使用连接池(例如 Apache DBCP ).这样一来,您就可以根据需要获得连接,并提供一些其他功能,例如,如果忘记将连接返回到池中,则会发出警告.

However, rather than getting connections directly from the driver manager, I recommend that you use a connection pool (such as Apache DBCP). This will let you get connections on an as-needed basis, and will provide some additional functionality such as warning you if you forget to return the connection to the pool.

这篇关于如何在同一应用程序中使用多个JDBC驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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