DruidDataSource根据传入参数连接多个不同数据库MYSQL、ORACLE、GBASE

DruidDataSource

Druid是阿里巴巴开源的一款高性能的Java数据库连接池,它不仅提供了数据库连接池的功能,还包含了SQL监控、SQL防泄漏、SQL执行日志等功能,是Java应用中常用的数据库连接池之一。DruidDataSource是Druid中的核心类,用于创建和管理数据库连接。

多个数据库

包含MYSQL、ORACLE、GBASE

public enum DbTypeEnum {
    MYSQL,
    ORACLE,
    GBASE
}

新建DataSource

包含MYSQL、ORACLE、GBASE

    private static DruidDataSource getDataSource(int type) {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(MYSQL_DRIVER_6);
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("root");

        if (ORACLE.ordinal() == type) {
            dataSource.setDriverClassName(ORACLE_DRIVER);
            // jdbc:oracle:thin@[hostname]:[port]/[DB service name]
            dataSource.setUrl("jdbc:oracle:thin:@localhost:1521/FREEPDB1");
            dataSource.setUsername("system");
            dataSource.setPassword("root");
        }

        if (GBASE.ordinal() == type) {
            dataSource.setDriverClassName(GBASE_DRIVER);
            // jdbc:oracle:thin@[hostname]:[port]/[DB service name]
            dataSource.setUrl("jdbc:gbasedbt-sqli://172.20.208.2:9088/appdev:GBASEDBTSERVER=gbase01;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=10");
            dataSource.setUsername("gbasedbt");
            dataSource.setPassword("GBase123$%");
        }
        return dataSource;
    }

测试连接

包含MYSQL、ORACLE、GBASE

    private static void testDataSource(DruidDataSource dataSource, int type) {
        try {
            // 获取连接
            Connection conn = dataSource.getConnection();
            System.out.println("Connection successful!");
            if (0 == type) {
                PreparedStatement stmt = conn.prepareStatement("SELECT id, code, name FROM report;");
                ResultSet rslt = stmt.executeQuery();
                while (rslt.next()) {
                    System.out.println(rslt.getString(1) + " | " + rslt.getString(2)
                            + " | " + rslt.getString(3));
                }
            }
            if (1 == type) {
                PreparedStatement stmt = conn.prepareStatement("SELECT id, code, name FROM ROOT.REPORT");
                ResultSet rslt = stmt.executeQuery();
                while (rslt.next()) {
//                    System.out.println(rslt.getString(1));
                    System.out.println(rslt.getString(1) + " | " + rslt.getString(2)
                            + " | " + rslt.getString(3));
                }
            }
            if (2 == type) {
                PreparedStatement stmt = conn.prepareStatement("SELECT id, code, name FROM REPORT");
                ResultSet rslt = stmt.executeQuery();
                while (rslt.next()) {
//                    System.out.println(rslt.getString(1));
                    System.out.println(rslt.getString(1) + " | " + rslt.getString(2)
                            + " | " + rslt.getString(3));
                }
            }
            // 关闭连接
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

主函数

    public static void main(String[] args) {
        int type = GBASE.ordinal();
        DruidDataSource dataSource = getDataSource(type);

        // 设置初始连接数
        dataSource.setInitialSize(5);
        // 设置最大连接数
        dataSource.setMaxActive(20);
        // 设置最小空闲连接数
        dataSource.setMinIdle(5);

        // 测试连接池
        testDataSource(dataSource, type);

        dataSource.close();
    }
在使用Spring Boot连接Oracle数据库时,我们可以使用DruidDataSource进行配置。 首先,我们需要在pom.xml文件中添加Oracle数据库的驱动依赖项,例如ojdbc6或ojdbc8。 然后,在application.properties或application.yml文件中进行数据库连接配置。以下是一些常用的连接属性: ```yaml spring.datasource.url=jdbc:oracle:thin:@host:port:sid spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=oracle.jdbc.OracleDriver ``` 在配置文件中,我们需要替换`host`、`port`和`sid`为实际的连接信息。`your_username`和`your_password`代表你的数据库用户名和密码。 接下来,在Spring Boot的配置类中,我们可以使用@Bean注解来定义一个DruidDataSource对象。 ```java @Configuration public class DataSourceConfig { @Value("${spring.datasource.url}") private String dbUrl; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(dbUrl); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); return dataSource; } } ``` 在这个配置类中,我们注入了配置文件中的属性,并将其设置到DruidDataSource对象中。然后,我们将该对象定义为一个Spring Bean,并在其他类中使用@Autowired注解进行注入。 最后,我们就可以在其他类中使用DruidDataSource来进行数据库操作了。 使用Spring Boot连接Oracle数据库的详细步骤就是这样。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值