DBCP连接池的最简单应用(用于Oracle数据库)

php中文网
发布: 2016-06-07 17:23:26
原创
1386人浏览过

sean-m700是主机名,ora92是oracle数据库的instance ID. 我手头的机器上没有安装oracle数据库,用的是很早以前的一个oracle9.2的

鉴于有人问起dbcp直接用于jdbc连接的问题,我做了一个最简单的示例。所有资源来源于网上。它不需要什么web容器,就是一简单的控制台应用。

资源:



当然,还有Oracle jdbc要用的ojdbc14.jar (适用于oracle9i及以上版本)

工程文件:放到这里了。

数据库连接信息:
jdbc:oracle:thin:scott/tiger@sean-m700:1521:ora92
sean-m700是主机名,ora92是oracle数据库的instance ID. 我手头的机器上没有安装oracle数据库,用的是很早以前的一个oracle9.2的拷贝,重新安装实例和相应服务得来的。

AppMall应用商店
AppMall应用商店

AI应用商店,提供即时交付、按需付费的人工智能应用服务

AppMall应用商店 56
查看详情 AppMall应用商店


源码如下:借化献佛,,源码也是从网上得来的。(?revision=1100136&view=markup)

/* 
// 
33  // Here's a simple example of how to use the BasicDataSource. 
34  // 
35   
36  // 
37  // Note that this example is very similiar to the PoolingDriver 
38  // example. 
39   
40  // 
41  // To compile this example, you'll want: 
42  //  * commons-pool-1.5.6.jar 
43  //  * commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+) 
44  //  * j2ee.jar (for the javax.sql classes) 
45  // in your classpath. 
46  // 
47  // To run this example, you'll want: 
48  //  * commons-pool-1.5.6.jar 
49  //  * commons-dbcp-1.3.jar (JDK 1.4-1.5) or commons-dbcp-1.4 (JDK 1.6+) 
50  //  * j2ee.jar (for the javax.sql classes) 
51  //  * the classes for your (underlying) JDBC driver 
52  // in your classpath. 
53  // 
54  // Invoke the class using two arguments: 
55  //  * the connect string for your underlying JDBC driver 
56  //  * the query you'd like to execute 
57  // You'll also want to ensure your underlying JDBC driver 
58  // is registered.  You can use the "jdbc.drivers" 
59  // property to do this. 
60  // 
61  // For example: 
62  //  java -Djdbc.drivers=oracle.jdbc.driver.OracleDriver \ 
63  //      -classpath commons-pool-1.5.6.jar:commons-dbcp-1.4.jar:j2ee.jar:oracle-jdbc.jar:. \ 
64  //      PoolingDataSourceExample 
65  //      "jdbc:oracle:thin:scott/tiger@myhost:1521:mysid" 
66  //      "SELECT * FROM DUAL" 
*/   
/* 
The Oracle connection URL for the thin client-side driver ojdbc14.jar has the following format: 
jdbc:oracle:thin:[user/password]@[host][:port]:SID 
jdbc:oracle:thin:[user/password]@//[host][:port]/SID 
 
  user - The login user name defined in the Oracle server. 
 
  password - The password for the login user. 
 
  host - The host name where Oracle server is running. 
        Default is 127.0.0.1 - the IP address of localhost. 
 
  port - The port number where Oracle is listening for connection. 
        Default is 1521. 
 
  SID  - System ID of the Oracle server database instance. 
        SID is a required value. By default, Oracle Database 10g Express 
        Edition creates one database instance called XE. 
*/   
   
import org.apache.commons.dbcp.BasicDataSource;   
import javax.sql.*;   
import java.sql.*;   
   
public class TestDataSource   
{   
   
    /** 
    * @param args 
    */   
    public static void main(String[] args)   
    {   
        System.out.println("Setting up data source.");   
        String url = "jdbc:oracle:thin:scott/tiger@sean-m700:1521:ora92";   
        DataSource dataSource = setupDataSource(url);   
        System.out.println("Done...");   
   
        // Now, we can use JDBC DataSource as we normally would.   
        //   
        Connection conn = null;   
        Statement stmt = null;   
        ResultSet rset = null;   
   
        try {   
            System.out.println("Creating connection.");   
            conn = dataSource.getConnection();   
            System.out.println("Creating statement.");   
            stmt = conn.createStatement();   
            System.out.println("Executing statement.");   
            rset = stmt.executeQuery("select 1 from DUAL");   
            System.out.println("Results:");   
            int numcols = rset.getMetaData().getColumnCount();   
            while(rset.next()) {   
                for(int i=1;i                    System.out.print("\t" + rset.getString(i));   
                }   
                System.out.println("");   
            }   
        } catch(SQLException e) {   
            e.printStackTrace();   
        } finally {   
            try { if (rset != null) rset.close(); } catch(Exception e) { }   
            try { if (stmt != null) stmt.close(); } catch(Exception e) { }   
            try { if (conn != null) conn.close(); } catch(Exception e) { }   
        }   
    }   
   
    public static DataSource setupDataSource(String connectURI) {   
        BasicDataSource ds = new BasicDataSource();   
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");   
        ds.setUsername("scott");   
        ds.setPassword("tiger");   
        ds.setUrl(connectURI);   
        return ds;   
    }   
   
    public static void printDataSourceStats(DataSource ds) {   
        BasicDataSource bds = (BasicDataSource) ds;   
        System.out.println("NumActive: " + bds.getNumActive());   
        System.out.println("NumIdle: " + bds.getNumIdle());   
    }   
   
    public static void shutdownDataSource(DataSource ds) throws SQLException {   
        BasicDataSource bds = (BasicDataSource) ds;   
        bds.close();   
    }   
   
}   

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号