C3P0配置

所需要的jar包,c3p0-0.9.5-pre9.jar,mchange-commons-java-0.2.8.jar,hsqldb.jar

 

//普通

    public static void main(String[] args) {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        try {
            cpds.setDataSourceName("j07");
            cpds.setDriverClass("org.hsqldb.jdbc.JDBCDriver"  );
             cpds.setJdbcUrl( "jdbc:hsqldb:mem:test" );
            cpds.setMaxPoolSize(20);
            cpds.setUser("sa");
            cpds.setPassword("");
             cpds.setMinPoolSize(5);
             cpds.setAcquireIncrement(5);
        } catch (PropertyVetoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        
    }

 

//读取properties,以c3p0作为文件名

c3p0.driverClass=org.hsqldb.jdbcDriver
c3p0.jdbcUrl=jdbc:hsqldb:hsql://localhost/
c3p0.user=sa
c3p0.password=

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            System.out.println(ds.getConnection());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

//读取properties,以c3作为文件名

c3p0.driverClass=org.hsqldb.jdbcDriver
c3p0.jdbcUrl=jdbc:hsqldb:hsql://localhost/
c3p0.user=sa
c3p0.password=

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ComboPooledDataSource ds=new ComboPooledDataSource();
        ds.setDataSourceName("c3.properties");
        try {
            System.out.println(ds.getConnection());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

//读取xml,注意xml文件名:c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">org.hsqldb.jdbcDriver</property>
<property name="jdbcUrl">jdbc:hsqldb:hsql://localhost/</property>
<property name="user">sa</property>
<property name="password"></property>
<property name="maxPoolSize">30</property>
<property name="minPoolSize">10</property>
 </default-config>
  <named-config name="intergalactoApp">

<property name="driverClass">org.hsqldb.jdbcDriver</property>
<property name="jdbcUrl">jdbc:hsqldb:hsql://localhost/</property>
<property name="user">sa</property>
<property name="password"></property>
       <property name="maxPoolSize">1000</property>
    <property name="minPoolSize">100</property>
    <property name="numHelperThreads">50</property>
   </named-config>
</c3p0-config>

public static void main(String[] args) {
        // TODO Auto-generated method stub
       // ComboPooledDataSource ds=new ComboPooledDataSource("intergalactoApp");//intergalactoApp配置里面的  <named-config name="intergalactoApp">
          ComboPooledDataSource ds=new ComboPooledDataSource();
        try {
            System.out.println(ds.getConnection());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

//web中配置

tomcat的配置

context.xml

<Resource
    auth="Container"
    description="DB Connection"
    driverClass="org.hsqldb.jdbc.JDBCDriver"
    maxPoolSize="4"
    minPoolSize="2"
    acquireIncrement="1"
    name="jdbc/hsql"
     user="sa"
    password=""
    factory="org.apache.naming.factory.BeanFactory"
    type="com.mchange.v2.c3p0.ComboPooledDataSource"
    jdbcUrl="jdbc:hsqldb:mem:test" />

获取

<%
            Context c=new InitialContext();
            DataSource ds=(DataSource)c.lookup("java:comp/env/jdbc/hsql");
             out.print(ds.getConnection());
         %>

posted @ 2014-10-23 21:22  单方面  阅读(356)  评论(0编辑  收藏  举报