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