java中JDBC当中请给出一个DataSource的HelloWorld例子

在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接。事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的。当然了,这也是没有办法的事儿。就像我们买贵书必须花大价钱一样。原因是书本身就那么贵,也确实没有办法。但如果有1万个学生看书,我们能不能建立 一个小型图书馆?我们只需买100本书。而不是像没有图书馆的情况下,共买1万本书,人手一本。大家毕了业以后,每个人都销毁自己的书。这就造成了极大的 浪费。但要注意的是,每个人看完书以后,把书要还回图书馆。数据源就像这里的图书馆。里面开始也是费劲巴拉的建立了一堆连接。之后谁用谁就可以,获取一个 连接。但是用完以后,并不是销毁连接,而是把连接简单的返还给数据源,以供别的用户再用(在我的参考目录下,有个连接池实现程序,看看它的close方法,ds.getConnection返回的Connection的实例和前面jdbc helloworld的Connection实例不一样。因为Connection只是一个接口,所以每次的实例的实现都是不一样的。)。你可以想象,如果有很多客户,都需要用连接的话,数据源技术,无 疑是一个非常好的选择。在互联网的环境下,成千上万的用户,从不同的机器上,访问一台机器,从它的数据库当中获取信息。在这种情况下,数据源发挥了极大的 优势。如果用DriverManager连接完一次,销毁一次连接的话,一定是一场噩梦。
1.DataSource的HelloWorld程序
例:6.1.1

import java.sql.*;
import javax.sql.*;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
public class TestMark_to_win {
    public static void main(String args[]) throws SQLException {
        MysqlConnectionPoolDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setURL("jdbc:mysql://localhost:3306/test");
        ds.setUser("root");
        ds.setPassword("1234");
        Connection con = ds.getConnection();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select * from login");
        while (rs.next()) {
            System.out.println(rs.getString("id"));
            System.out.println(rs.getString("name"));
        }
        System.out.println("ok");
    }
}

更多请见:http://www.mark-to-win.com/tutorial/java_10_DataSourceHelloWorld.html

 

以下是一个简单的使用ShardingSphere实现分库分表的Java例子: 首先,需要在pom.xml文件添加ShardingSphere的依赖: ``` <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>4.1.1</version> </dependency> ``` 接着,需要在Spring配置文件添加ShardingSphere的数据源配置: ``` spring.shardingsphere.datasource.names=ds0, ds1 spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=root spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=root spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds${0..1}.user_${0..1} spring.shardingsphere.sharding.tables.user.table-strategy.standard.sharding-column=user_id spring.shardingsphere.sharding.tables.user.table-strategy.standard.precise-algorithm-class-name=com.example.algorithm.ModuloShardingTableAlgorithm spring.shardingsphere.sharding.tables.user.table-strategy.standard.range-algorithm-class-name=com.example.algorithm.RangeShardingTableAlgorithm ``` 注意,上面的例子使用了两个数据源ds0和ds1,分别对应了两个数据库db0和db1。同时,使用了ShardingSphere的分片表策略,将user表按照user_id字段进行分片,使用了ModuloShardingTableAlgorithm和RangeShardingTableAlgorithm算法来实现分片。 最后,需要实现两个算法类: ModuloShardingTableAlgorithm.java: ``` public final class ModuloShardingTableAlgorithm implements PreciseShardingAlgorithm<Integer> { @Override public String doSharding(Collection<String> tableNames, PreciseShardingValue<Integer> shardingValue) { for (String each : tableNames) { if (each.endsWith(shardingValue.getValue() % tableNames.size() + "")) { return each; } } throw new IllegalArgumentException(); } } ``` RangeShardingTableAlgorithm.java: ``` public final class RangeShardingTableAlgorithm implements RangeShardingAlgorithm<Integer> { @Override public Collection<String> doSharding(Collection<String> tableNames, RangeShardingValue<Integer> shardingValue) { Collection<String> result = new LinkedHashSet<>(); for (Integer i = shardingValue.getValueRange().lowerEndpoint(); i <= shardingValue.getValueRange().upperEndpoint(); i++) { for (String each : tableNames) { if (each.endsWith(i % tableNames.size() + "")) { result.add(each); } } } return result; } } ``` 这样,就可以使用ShardingSphere实现分库分表了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值