一个项目中配置多数据源 之 手动创建连接池(Oracle)

JdbcUtils1:

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils1 {
    private static DruidDataSource dataSource;

    static {
        Properties properties = new Properties();
        InputStream resourceAsStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc1.properties");
        //读取jdbc.properties属性配置文件
        try {
            properties.load(resourceAsStream);//从流中加载数据
            try {
                dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);//创建数据库连接池
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*
    获取数据库连接池中的连接
     */
    public static Connection getConnection() {
        Connection conn = null;

        try {
            conn = dataSource.getConnection();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }

        return conn;
    }

    /*
    关闭连接,放回数据库连接池
     */
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException throwable) {
                throwable.printStackTrace();
            }
        }
    }
}

   Connection connection = null;
        connection = JdbcUtils1.getConnection();//连接数据库
        List<DmdzDto> dmdzDtos = new ArrayList<>();
        List<AddressResultDto> addressResultDtos = new ArrayList<>();
        Map<String, Object> resMap = new HashMap<>();
        String sqls = "select * from test";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sqls);//创建PreparedStatement动态语句对象
            boolean execute = preparedStatement.execute();//返回结果集对象ResultSet
            while (execute){
                ResultSet resultSet = preparedStatement.getResultSet();
                while (resultSet.next()) {//处理返回结果
                 	String X = resultSet.getString("X");
                    }
                }
                execute = preparedStatement.getMoreResults();
            }   
            preparedStatement.close();//关闭preparedStatement语句对象
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils1.close(connection);//关闭数据库连接
        }
	username=test
	password=test
	url=jdbc:oracle:thin:@127.0.0.1:1521:helowin
	driverClassName=oracle.jdbc.driver.OracleDriver
	initialSize=5
	maxActive=20

pom文件添加依赖
   <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值