java当中JDBC当中请给出一个DataSource的单态模式(SingleTon)HelloWorld例子

本文通过具体示例展示了如何使用DataSource实现单态模式,确保在内存中仅创建一个实例,避免资源浪费。通过MysqlConnectionPoolDataSource类创建连接池,并在测试程序中调用getConnection()方法获取连接。

2.DataSource的单态模式(SingleTon)程序

咱们还接着上面的例子来说。1万个人要看书。千万确保要只建立一个图书馆。要是一不留神,建了两个或三个图书馆,那可就亏大发了。对于数据源来讲,最重要的是要保证在内存当中只有一个实例。如果存在多个实例,那就亏大了。 


例:6.2.1

import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import java.sql.*;
import javax.sql.*;
public class DatabaseConn {

    static private MysqlConnectionPoolDataSource ds;
    private Connection con;

    private DatabaseConn() {
        ds = new MysqlConnectionPoolDataSource();
        ds.setURL("jdbc:mysql://localhost:3306/test");
        ds.setUser("root");
        ds.setPassword("1234");
    }

    public static Connection getConnection() throws Exception {
        if (ds == null) {
/*DatabaseConn的目的是让ds有值,和con无关*/            
            new DatabaseConn();
        }
        Connection con = null;
        con = ds.getConnection();
        return con;
    }
}
}


以下是测试程序:

import java.sql.*;
import javax.sql.*;
public class TestMark_to_win {
    public static void main(String args[]) throws Exception {
        Connection con= DatabaseConn.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");

更多请见:https://blog.youkuaiyun.com/qq_44594249/article/details/100775217

以下是一个的使用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
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值