C3P0使用小例子

C3P0是一个开放源代码的JDBC连接池 ,下面是一个小例子。

下载地址:http://sourceforge.net/projects/c3p0

引入依赖的jar包,包括:

代码:
package com.cp.mytest;

import java.sql.*;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class LinkMysql {

/**
   * @param args
   */
public static void main(String[] args) {
   System.out.println(">>>> ");
   try {
    ComboPooledDataSource cpds = new ComboPooledDataSource(); 
    cpds.setDriverClass( "org.gjt.mm.mysql.Driver" );
    cpds.setJdbcUrl( "jdbc:mysql://localhost/test" ); 
    cpds.setUser("root"); 
    cpds.setPassword("1234"); 
    cpds.setMaxStatements( 180 ); 
    cpds.setMaxPoolSize(100);
   
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    con=cpds.getConnection();
    stmt=con.createStatement();
    rs=stmt.executeQuery("select * from user_table");
    while(rs.next()){
     System.out.println(rs.getString(1));
    }


   } catch (Exception e) {
    e.printStackTrace();
   }
}

}




你可能感兴趣的:(java,sql,.net,mysql,jdbc)