c3p0 实现

闲话免谈,天天向上

这个项目在另一边 sprin+mybaitis里面有,觉得分开更干净一些。就贴上来了。

https://www.cnblogs.com/adao21/p/13167864.html 里面 db文件夹部分

application.properties

#socket server config
socket.server.address = 192.168.127.220
socket.server.port = 2000
socket.connect.timeout = 3000

#db config
db.driverClassName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@192.168.128.59:1521:ORCL
db.username=coll_platform
db.password=coll_platform

# c3p0
c3p0.acquireIncrement=5
c3p0.initialPoolSize=10
c3p0.idleConnectionTestPeriod=60
c3p0.minPoolSize=5
c3p0.maxPoolSize=50
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=60

PropertiesUtil

package com.adao.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 配置文件内容获取
 * @author Administrator
 *
 */
public class PropertiesUtil {
    
    public static Properties loadProperties() throws IOException {
        Properties properties = new Properties();
//              使用ClassLoader加载properties配置文件生成对应的输入流
        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("application.properties");
        // 使用properties对象加载输入流
        properties.load(in);
        in.close();
        return properties;

    }
     
    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
//              使用ClassLoader加载properties配置文件生成对应的输入流
//        InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream("socketClient.properties");
        InputStream in = ClassLoader.getSystemResourceAsStream("socketClient.properties");
        // 使用properties对象加载输入流
        properties.load(in);
        in.close();
        // 获取key对应的value值

        for (String keyString : properties.stringPropertyNames()) {
            System.out.println(keyString + " = " + properties.getProperty(keyString));
        }
        
//        String pString = properties.getProperty("socket.server.address");
//        System.out.println(pString);

        // 资源文件目录下配置文件获取
//        Properties properties1 = new Properties();
//        FileInputStream inputStream = new FileInputStream("config/socketClient.properties"); // 注意路径
//        properties1.load(inputStream);
//        System.out.println(properties1.getProperty("socket.server.address"));

        // 配置文件需要放在当前包目录下
//        InputStream ips = PropertiesMain.class.getResourceAsStream("socketClient.properties");
//
//        Properties props = new Properties();
//        props.load(ips);
//        ips.close();
//        String className = props.getProperty("className");
//         System.out.println(className);

    }

}

DataBasePool

package com.adao.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.adao.common.PropertiesUtil;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataBasePool {
    private static Properties resource = null;

    private static DataBasePool instance;

    private ComboPooledDataSource dataSource;

    static {
        instance = new DataBasePool();
    }

    private DataBasePool() {

        try {
            resource = PropertiesUtil.loadProperties();
            final String url = resource.getProperty("db.url");
            final String user = resource.getProperty("db.username");
            final String password = resource.getProperty("db.password");
            final int initialPoolSize = Integer.valueOf(resource.getProperty("c3p0.initialPoolSize"));
            final int maxIdleTime = Integer.valueOf(resource.getProperty("c3p0.maxIdleTime"));
            final int minPoolSize = Integer.valueOf(resource.getProperty("c3p0.minPoolSize"));
            final int maxPoolSize = Integer.valueOf(resource.getProperty("c3p0.maxPoolSize"));
            final int acquireIncrement = Integer.valueOf(resource.getProperty("c3p0.acquireIncrement"));

            dataSource = new ComboPooledDataSource();
            dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
            dataSource.setJdbcUrl(url);
            dataSource.setUser(user);
            dataSource.setPassword(password);
            dataSource.setInitialPoolSize(initialPoolSize);
            dataSource.setMaxIdleTime(maxIdleTime);
            dataSource.setMinPoolSize(minPoolSize);
            dataSource.setMaxPoolSize(maxPoolSize);
            dataSource.setAcquireIncrement(acquireIncrement);
        } catch (Exception e) {
        }
    }

    public static DataBasePool getInstance() {
        return instance;
    }

    public Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }
}

OracleDBHelper

package com.adao.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
  
public class OracleDBHelper {  
  
    public Connection conn = null ;
    public PreparedStatement pst = null;  
  
    public OracleDBHelper(String sql) {  
        try {   
            conn = DataBasePool.getInstance().getConnection() ;  
            pst = conn.prepareStatement(sql); 
        } catch (Exception e) {   
        }  
    }  
  
    public void close(ResultSet rs) {  
        try {  
            if (rs != null) {
                rs.close();
            }
            if (this.conn != null) {
                this.conn.close();  
            }
            if (this.pst != null) {
                this.pst.close();  
            }
        } catch (SQLException e) {   
        }  
    } 
   
    
}

dbTest

package com.adao.db;

import java.sql.ResultSet;
import java.sql.SQLException;

public class dbTest {
    public static void main(String[] args) {
        String sql = "SELECT * FROM T_COLL_TASK_INFO where TASK_ID =1433142404804";
        OracleDBHelper dbHelper = new OracleDBHelper(sql);
        ResultSet rs = null;
        try {
            rs = dbHelper.pst.executeQuery();
            while (rs.next()) {
//                ruleIds.add(rs.getInt(""));
                System.out.println(rs.getObject("TASK_NAME"));
                System.out.println(rs.getObject("TASK_TYPE"));
            }
        } catch (SQLException e) {
        } catch (Exception e) {
        } finally {
            dbHelper.close(rs);
        }
    }
    
    

}

jar包: 版本随意

c3p0-0.9.2.jar

mchange-commons-java-0.2.3.3.jar

执行结果:

猜你喜欢

转载自www.cnblogs.com/adao21/p/13167907.html
今日推荐