MS Access数据库多数据源JDBC查询

文章描述了一种在Druid或C3P0不支持Access数据库的情况下,如何通过Ucanaccess驱动实现多数据源JDBC查询的方法。配置了两个不同的Access数据源,并展示了Java代码示例进行数据查询,同时提供了日志输出和源码下载链接。

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

Druid, C3P0都不支持微软的Access,如何实现多数据源JDBC查询??

2023-01-23T23:54:52.476+0800  WARNING  com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@544a5904 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: 
net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::5.0.1 Decoding not supported.  Please choose a CodecProvider which supports reading the current database encoding.
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:231)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)

1、添加配置信息

声明使用多个数据源

bee.dosql.multiDS.enable=true
bee.osql.showSQL=true
#配置占位参数显示数据类型; 
bee.osql.showSql.showType=true
#打印能执行的语句
bee.osql.showSql.showExecutableSql=true

2、配置数据源

			SimpleDataSource dataSource0=new SimpleDataSource();
			dataSource0.setUrl("jdbc:ucanaccess://d:/school.accdb");
			dataSource0.setUsername("");
			dataSource0.setPassword("");
			dataSource0.init();
			
			SimpleDataSource dataSource1=new SimpleDataSource();
			dataSource1.setUrl("jdbc:ucanaccess://d:/school-pw3.accdb");
			dataSource1.setUsername("");
			dataSource1.setPassword("123456");
			dataSource1.init();

			Map<String, DataSource> dataSourceMap = new HashMap<>();
			dataSourceMap.put("ds0", dataSource0);
			dataSourceMap.put("ds1", dataSource1); 
			BeeFactory.getInstance().setDataSourceMap(dataSourceMap);

数据库(ds0)里的数据: 

3、使用java查询数据

路由到数据源可支持类的包名限定,直接指定,分片等

package org.teasoft.exam.bee.access.ds;

import java.util.List;

import org.teasoft.bee.osql.Suid;
import org.teasoft.exam.bee.access.Stu;
import org.teasoft.honey.osql.shortcut.BF;

/**
 * Access 使用ORM
 */
public class AccessDbTest2 {
	
	public static void main(String[] args){
		InitSameAccessDsUtil.initDS();
		
		Suid suid=BF.getSuid();
		suid.setDataSourceName("ds0");
		List<Stu> list=suid.select(new Stu());
		
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
		System.out.println("finished");
	}
}

4、输出日志:

[INFO] [Bee] select SQL: select id,name,age,remark from stu   [values]: 
[INFO] [Bee] select SQL:  ( ExecutableSql )
select id,name,age,remark from stu
[INFO] [Bee] ========= the current DataSource name is :ds0
[DEBUG] bee.osql.cache.prototype=1 , the entity is not Serial, will do not put in cache!
[INFO] [Bee]  | <--  select rows: 2
Stu [id=1, name=zhang shan, age=22, remark=twitter被收购了]
Stu [id=2, name=李四, age=23, remark=哈哈。。。]

源码下载:

https://gitee.com/automvc/bee

https://gitee.com/automvc/bee-springboot

github:

https://github.com/automvc/bee

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值