jdbc连接池的使用


import java.sql.*;
import javax.sql.*;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;
public class jdbc {

    
public static void main(String args[]) {

        
try {
            Class.forName(
"oracle.jdbc.driver.OracleDriver");
        }
 catch (Exception e) {
            System.out.println(
"异常" + e.getMessage());
            System.exit(
1);
        }


        
try {
            
//create connection pool data source object
            OracleConnectionPoolDataSource myOCPDS=new OracleConnectionPoolDataSource();
            
            myOCPDS.setServerName(
"192.168.12.20");
            myOCPDS.setDatabaseName(
"progather");
            myOCPDS.setPortNumber(
1521);
            myOCPDS.setDriverType(
"thin");
            myOCPDS.setUser(
"username");
            myOCPDS.setPassword(
"password");
            
            System.out.println(
"create a pool connection");
            PooledConnection myPooledConnection
=myOCPDS.getPooledConnection();
            
            System.out.println(
"request a connection instance");
            Connection myconnection
=myPooledConnection.getConnection();
            Displaycustomer(myconnection,
401203410);
            myconnection.close();
            System.out.println(
"close the connection instance");
            
            System.out.println(
"request another connection");
            myconnection
=myPooledConnection.getConnection();
            Displaycustomer(myconnection,
401203411);
            myconnection.close();
            System.out.println(
"close another connection instance");
            myPooledConnection.close();
            

        }
 catch (SQLException e) {
            System.out.println(
"Darn! A SQL error: " + e.getMessage());
        }
 
    }

    
    
public static void Displaycustomer(Connection myconnection,int id) throws SQLException
    
{
        Statement mystatement
=myconnection.createStatement();
        String sql;
        
int org_uni_code;
        String chi_short_name;
        String org_chi_name;
        sql
="select org_uni_code,chi_short_name,org_chi_name from ccxe2.pub_org_info where area_uni_code="+id;
        ResultSet myresultset
=mystatement.executeQuery(sql);
        
while(myresultset.next())
        
{
            org_uni_code
=myresultset.getInt("org_uni_code");
            chi_short_name
=myresultset.getString("chi_short_name");
            org_chi_name
=myresultset.getString("org_chi_name");
            System.out.println(
"org_unit_code is "+org_uni_code+"    chi_short_name:" + chi_short_name+
                    
"    org_chi_name:"+ org_chi_name);
            }

        myresultset.close();
        mystatement.close();
    }

}

posted on 2012-04-05 22:23 snowhill 阅读(201) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜