zoukankan      html  css  js  c++  java
  • C3P0数据库连接池使用

    C3P0数据库连接池使用

    1、拷贝jar包:c3p0-0.9.1.2.jar     c3p0-0.9.1.2-jdk1.3.jar    c3p0-oracle-thin-extras-0.9.1.2.jar(oracle需要)

    2、书写配制文件放在src目录下:c3p0-config.xml(名字只能是这个,而且配置文件必须放在src根目录下)

    3、类C3P0Util

    c3p0-config.xml 内容:

     1     <?xml version="1.0" encoding="UTF-8"?>  
     2     <c3p0-config>  
     3         <!-- This is default config! -->  
     4         <default-config>  
     5             <property name="initialPoolSize">10</property>  
     6             <property name="maxIdleTime">30</property>  
     7             <property name="maxPoolSize">100</property>  
     8             <property name="minPoolSize">10</property>  
     9             <property name="maxStatements">200</property>  
    10         </default-config>  
    11       
    12         <!-- This is my config for mysql-->  
    13         <named-config name="mysql">  
    14             <property name="driverClass">com.mysql.jdbc.Driver</property>  
    15             <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>  
    16             <property name="user">root</property>  
    17             <property name="password"></property>  
    18             <property name="initialPoolSize">10</property>  
    19             <property name="maxIdleTime">30</property>  
    20             <property name="maxPoolSize">100</property>  
    21             <property name="minPoolSize">10</property>  
    22             <property name="maxStatements">200</property>  
    23         </named-config>  
    24           
    25           
    26         <!-- This is my config for oracle -->  
    27         <named-config name="oracle">  
    28             <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>  
    29             <property name="jdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</property>  
    30             <property name="user">scott</property>  
    31             <property name="password">liang</property>  
    32             <property name="initialPoolSize">10</property>  
    33             <property name="maxIdleTime">30</property>  
    34             <property name="maxPoolSize">100</property>  
    35             <property name="minPoolSize">10</property>  
    36             <property name="maxStatements">200</property>  
    37         </named-config>  
    38     </c3p0-config>  

    C3P0Util:

     1     package com.liang.util;  
     2       
     3     import java.io.InputStream;  
     4     import java.sql.Connection;  
     5     import java.sql.PreparedStatement;  
     6     import java.sql.ResultSet;  
     7     import java.sql.SQLException;  
     8     import java.util.Properties;  
     9       
    10     import com.mchange.v2.c3p0.ComboPooledDataSource;  
    11     /** 
    12      * 数据库工具类 
    13      * @author liang 
    14      * 
    15      */  
    16     public class C3P0Util {  
    17         static ComboPooledDataSource cpds=null;  
    18         static{  
    19             //这里有个优点,写好配置文件,想换数据库,简单  
    20             //cpds = new ComboPooledDataSource("oracle");//这是oracle数据库  
    21             cpds = new ComboPooledDataSource("mysql");//这是mysql数据库  
    22         }  
    23         /** 
    24          * 获得数据库连接 
    25          * @return   Connection 
    26          */  
    27         public static Connection getConnection(){  
    28             try {  
    29                 return cpds.getConnection();  
    30             } catch (SQLException e) {  
    31                 e.printStackTrace();  
    32                 return null;  
    33             }  
    34         }  
    35           
    36         /** 
    37          * 数据库关闭操作 
    38          * @param conn   
    39          * @param st     
    40          * @param pst 
    41          * @param rs 
    42          */  
    43         public static void close(Connection conn,PreparedStatement pst,ResultSet rs){  
    44             if(rs!=null){  
    45                 try {  
    46                     rs.close();  
    47                 } catch (SQLException e) {  
    48                     e.printStackTrace();  
    49                 }  
    50             }  
    51             if(pst!=null){  
    52                 try {  
    53                     pst.close();  
    54                 } catch (SQLException e) {  
    55                     e.printStackTrace();  
    56                 }  
    57             }  
    58       
    59             if(conn!=null){  
    60                 try {  
    61                     conn.close();  
    62                 } catch (SQLException e) {  
    63                     e.printStackTrace();  
    64                 }  
    65             }  
    66         }  
    67         /** 
    68          * 测试DBUtil类 
    69          * @param args 
    70          */  
    71         public static void main(String[] args) {  
    72             Connection conn=getConnection();  
    73             System.out.println(conn.getClass().getName());  
    74             close(conn,null,null);  
    75         }  
    76     }  

    转自:http://blog.csdn.net/liang5630/article/details/39055805

  • 相关阅读:
    修改Unity脚本模板的方法合计
    Mesh.Bake Scaled Mesh PhysX CollisionData的性能问题
    Scene的实时追踪显示
    基于Unity3D的AOP使用思路
    导出Unity场景为配置文件
    unity3d中namespace的使用注意问题
    [iOS]通过xib定义Cell然后关联UICollectionView
    swift之弹出一个背景半透明的UIViewController
    swift之xib关联UIView
    iOS坑爹的could not find any information for class named xxx
  • 原文地址:https://www.cnblogs.com/tooker/p/4707646.html
Copyright © 2011-2022 走看看