日期:2014-05-19  浏览次数:20797 次

JDBC连接池c3p0的问题
测试CP30连接池,怎么报这样的异常? 



import java.beans.PropertyVetoException; 
import java.sql.Connection; 
import java.sql.SQLException
import com.mchange.v2.c3p0.ComboPooledDataSource; 

public final class ConnectionManager { 

  private static ConnectionManager instance; 
  private static ComboPooledDataSource dataSource; 

  private ConnectionManager() throws SQLException, PropertyVetoException { 
  dataSource = new ComboPooledDataSource(); 

  dataSource.setUser("root"); 
  dataSource.setPassword("root"); 
  dataSource.setJdbcUrl("jdbc:mysql://loachost:3306/test"); 
  dataSource.setDriverClass("com.mysql.jdbc.Driver"); 
  dataSource.setInitialPoolSize(5); 
  dataSource.setMinPoolSize(1); 
  dataSource.setMaxPoolSize(10); 
  dataSource.setMaxStatements(50); 
  dataSource.setMaxIdleTime(60); 
  } 

  public static final ConnectionManager getInstance() { 
  if (instance == null) { 
  try { 
  instance = new ConnectionManager(); 
  } catch (Exception e) { 
  e.printStackTrace(); 
  } 
  } 
  return instance; 
  } 

  public synchronized final Connection getConnection() { 
  Connection conn = null; 
  try { 
  conn = dataSource.getConnection(); 
  } catch (SQLException e) { 
  e.printStackTrace(); 
  } 
  return conn; 
  } 



import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 


public class ConnectionDemo { 

  public static void main(String[] args) { 
  System.out.println("使用连接池................................"); 
  for (int i = 0; i < 20; i++) { 
  long beginTime = System.currentTimeMillis(); 
  Connection conn = ConnectionManager.getInstance().getConnection(); 
  try { 
  PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM t_fmscpy200"); 
  ResultSet rs = pstmt.executeQuery(); 
  while (rs.next()) { 
  } 
  } catch (SQLException e) { 
  e.printStackTrace(); 
  } finally { 
  try { 
  conn.close(); 
  } catch (SQLException e) { 
  e.printStackTrace(); 
  } 
  } 

  long endTime = System.currentTimeMillis(); 
  System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime)); 
  } 

  }