import java.io.IOException; 
    import java.io.InputStream; 
    import java.sql.Connection; 
    import java.sql.SQLException; 
    import java.util.Properties; 

    import com.alibaba.druid.pool.DruidDataSource; 


    public class SingletonDruidDataSource extends DruidDataSource { 
    private static final long serialVersionUID = 8847473526624938692L; 
    private static final SingletonDruidDataSource INSTANCE = new SingletonDruidDataSource(); 

    private SingletonDruidDataSource(){ 

    //InputStream propInputStream1 = this.getClass().getClassLoader().getResourceAsStream("ApplicationResources.properties"); 
    InputStream propInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("ApplicationResources.properties"); 
    Properties properties = new Properties(); 
    try { 
    properties.load(propInputStream); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    }
    this.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
    this.setUsername(properties.getProperty("dbuser")); 
    this.setPassword(properties.getProperty("dbpwd"));
    this.setUrl(properties.getProperty("dburl")); 
    this.setInitialSize(5); 
    this.setMinIdle(1); 
    this.setMaxActive(10); 
    this.setPoolPreparedStatements(false); 
    } 

    public static SingletonDruidDataSource getInstance(){ 
    return INSTANCE; 
    } 

    public static void main(String[] args) { 
    if (SingletonDruidDataSource.getInstance() == SingletonDruidDataSource.getInstance()){ 
    System.out.println("单例"); 
    } 
    DruidDataSource ds = SingletonDruidDataSource.getInstance(); 
    try { 
    Connection connection = ds.getConnection(); 
    connection.close(); 
    } catch (SQLException e) { 
    e.printStackTrace(); 
    } 
    } 

    本文为风林火山博客原创,转载请注明出处:www.flcoder.com