java jdbc桥接模式_16.3.1 使用JDBC-ODBC Bridge桥接方式连接数据库

本文介绍了使用JDBC-ODBC桥接器连接数据库的原理,该方法通过建立ODBC数据源屏蔽不同数据库的异构性,但移植性较差。还阐述了连接数据库的三个步骤,包括创建ODBC数据源、建立JDBC-ODBC桥接器和连接数据库,并给出了相应代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

16.3.1  使用JDBC-ODBC Bridge桥接方式连接数据库

使用JDBC-ODBC桥接器连接数据库的原理如图16-2所示。应用程序只需建立JDBC和ODBC之间的连接,即所谓的JDBC-ODBC桥接器,而和数据库的连接由ODBC完成。这种方法通过建立ODBC数据源而不是直接连接数据库的方式屏蔽了不同数据库的异构性,在需要更换数据库系统时,操作非常简单。然而,这种方法也使得应用程序依赖于ODBC,移植性较差,即应用程序所驻留的计算机必须提供ODBC。

5caa2660393fd182cff1e64ed5e205bc.png

图16-2  使用JDBC-ODBC桥接器连接数据库

具体地讲,使用JDBC-ODBC桥接器连接数据库主要包括以下三个步骤。

(1)创建ODBC数据源

使用操作系统中的ODBC系统,例如Windows XP的"控制面板'性能和维护'管理工具'ODBC数据源",按照向导添加ODBC数据源即可。

(2)建立JDBC-ODBC桥接器

JDBC使用java.lang包中的Class类建立JDBC-ODBC桥接器,即通过调用Class类的静态方法forName加载sun.jdbc.odbc包中的JdbcOdbcDriver类建立JDBC-ODBC桥接器,具体代码为:try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

}catch(ClassNotFoundException e){

System.out.println(e);

}

(3)连接数据库

在连接数据库的代码中不会出现数据库的名字,而是出现数据源的名字。可以使用java.sql包中的Connection类声明一个对象,再使用类DriverManager调用它的静态方法getConnection来创建连接对象,代码如下:try{

Connectioncon=DriverManager.getConnection(

"jdbc:odbc:NameofDatasource","loginName", "password");

}catch(SQLException e){

System.out.println(e);

}

【责任编辑:云霞 TEL:(010)68476606】

点赞 0

C:\Users\15990\Desktop\diangoRecomemend\project\static\home>npm install npm warn old lockfile npm warn old lockfile The package-lock.json file was created with an old version of npm, npm warn old lockfile so supplemental metadata must be fetched from the registry. npm warn old lockfile npm warn old lockfile This is a one-time fix-up, please be patient... npm warn old lockfile npm warn old lockfile Could not fetch metadata for vue-loader-v16@16.3.1 vue-loader-v16: No matching version found for vue-loader-v16@16.3.1. npm warn old lockfile at module.exports (E:\nodejs\node_modules\npm\node_modules\npm-pick-manifest\lib\index.js:215:23) npm warn old lockfile at RegistryFetcher.manifest (E:\nodejs\node_modules\npm\node_modules\pacote\lib\registry.js:130:54) npm warn old lockfile at async E:\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:715:24 { npm warn old lockfile code: 'ETARGET', npm warn old lockfile type: 'version', npm warn old lockfile wanted: '16.3.1', npm warn old lockfile versions: [ npm warn old lockfile '16.0.0-beta.5', npm warn old lockfile '16.0.0-beta.5.1', npm warn old lockfile '16.0.0-beta.5.2', npm warn old lockfile '16.0.0-beta.5.3', npm warn old lockfile '16.0.0-beta.5.4' npm warn old lockfile ], npm warn old lockfile distTags: { latest: '16.0.0-beta.5.4' }, npm warn old lockfile defaultTag: 'latest' npm warn old lockfile } npm error code ERESOLVE npm error ERESOLVE unable to resolve dependency tree npm error npm error While resolving: client_home@0.1.0 npm error Found: vue@3.5.16 npm error node_modules/vue npm error vue@"^3.2.40" from the root project npm error npm error Could not resolve dependency: npm error peer vue@"^2.5.17" from element-ui@2.15.14 npm error node_modules/element-ui npm error element-ui@"^2.15.0" from the root project npm error npm error Fix the upstream dependency conflict, or retry npm error this command with --force or --legacy-peer-deps npm error to accept an incorrect (and potentially broken) dependency resolution. npm error npm error npm error For a full report see: npm error C:\Users\15990\AppData\Local\npm-cache\_logs\2025-06-14T13_58_07_877Z-eresolve-report.txt npm error A complete log of this run can be found in: C:\Users\15990\AppData\Local\npm-cache\_logs\2025-06-14T13_58_07_877Z-debug-0.log
最新发布
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值