当前位置:网站首页>Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
2022-07-06 19:41:00 【InfoQ】
One 、 background
Two 、 Pain points
3、 ... and 、 chance
Four 、 Realization
org.apache.shardingsphere.driver.ShardingSphereDriver
- With
jdbc:shardingsphere
: The prefix
- The configuration file :
xxx.yaml
, The configuration file format is the same as YAML Consistent configuration
- Configuration file loading rules :
- No prefix means that the configuration file is loaded from the absolute path
- classpath: Prefix means loading configuration file from classpath
5、 ... and 、 Use steps
Class.forName("org.apache.shardingsphere.driver.ShardingSphereDriver");
String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = DriverManager.getConnection(jdbcUrl);
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, 10);
ps.setInt(2, 1000);
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
// ... }
}
}
String driverClassName = "org.apache.shardingsphere.driver.ShardingSphereDriver";
String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";// With HikariCP For example HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setJdbcUrl(jdbcUrl);
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, 10);
ps.setInt(2, 1000);
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
// ... }
}
}
- JDBC drive
6、 ... and 、 Conclusion
边栏推荐
- 手把手教你学会js的原型与原型链,猴子都能看懂的教程
- Hudi vs Delta vs Iceberg
- Test Li hi
- Benefit a lot, Android interview questions
- C language daily practice - day 22: Zero foundation learning dynamic planning
- Looting iii[post sequence traversal and backtracking + dynamic planning]
- VMware virtual machine cannot open the kernel device "\.\global\vmx86"
- 深入分析,Android面试真题解析火爆全网
- Blue Bridge Cup microbial proliferation C language
- JDBC details
猜你喜欢
Blue Bridge Cup microbial proliferation C language
算法面试经典100题,Android程序员最新职业规划
Information System Project Manager - Chapter VIII project quality management
[play with Linux] [docker] MySQL installation and configuration
Dark horse -- redis
DaGAN论文解读
Analysis of rainwater connection
打家劫舍III[后序遍历与回溯+动态规划]
Vmware虚拟机无法打开内核设备“\\.\Global\vmx86“的解决方法
学习探索-使用伪元素清除浮动元素造成的高度坍塌
随机推荐
short i =1; i=i+1与short i=1; i+=1的区别
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
350. 两个数组的交集 II
【翻译】供应链安全项目in-toto移至CNCF孵化器
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Use of map (the data of the list is assigned to the form, and the JSON comma separated display assignment)
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
Lick the dog until the last one has nothing (simple DP)
Druid database connection pool details
Druid 数据库连接池 详解
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
冒烟测试怎么做
121. The best time to buy and sell stocks
usb host 驱动 - UVC 掉包
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
CPU负载很低,loadavg很高处理方法
学习探索-函数防抖
C language daily practice - day 22: Zero foundation learning dynamic planning
Leetcode 30. 串联所有单词的子串
算法面试经典100题,Android程序员最新职业规划