首先要明确为什么要使用连接池
由于建立数据库连接是一种非常耗时、耗资源的行为,所以通过连接池预先同数据库建立一些连接,放在内存中,应用程序需要建立数据库连接时直接到连接池中申请一个就行,使用完毕后再归还到连接池中。
网上有3种方式与c3p0连接池连接,我用了配置c3p0-config.xml文件的方式,(最后详细描述一下这种方式)
二,硬编码方式,例如

private static ComboPooledDataSource ds;
	
	//静态初始化块进行初始化
	static{
		try {
			ds = new ComboPooledDataSource();//创建连接池实例
			
			ds.setDriverClass("com.mysql.jdbc.Driver");//设置连接池连接数据库所需的驱动
			
			ds.setJdbcUrl("jdbc:mysql://localhost:3306/cloudhospital");//设置连接数据库的URL
			
			ds.setUser("root");//设置连接数据库的用户名
			
			ds.setPassword("admin");//设置连接数据库的密码
			
			ds.setMaxPoolSize(40);//设置连接池的最大连接数
			
			ds.setMinPoolSize(2);//设置连接池的最小连接数
			
			ds.setInitialPoolSize(10);//设置连接池的初始连接数
			
			ds.setMaxStatements(100);//设置连接池的缓存Statement的最大数			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

三,还有就是采用配置文件,从配置文件中逐条获取参数,例如

//加载配置文件
			Properties props = new Properties();
			props.load(C3P0Properties.class.getClassLoader().getResourceAsStream("config.properties"));
			
			cpds.setDriverClass(props.getProperty("DriverClass"));
			cpds.setJdbcUrl(props.getProperty("JdbcUrl"));
			cpds.setUser(props.getProperty("User"));
			cpds.setPassword(props.getProperty("Password"));
			
			cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
			cpds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
			cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
			cpds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
			cpds.setMaxIdleTime(Integer.parseInt(props.getProperty("MaxIdleTime")));

这两种方式其实差异不大,而且凸显不出编写配置文件的重要性,
步骤
1,需要在src中创建一个名为c3p0-config.xml(名字必须是这个,路径也必须在src下)
2,.xml文件中内容为(里边内容不懂网上有详细解释,就是配置连接池的一些属性)

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>	
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/first</property>
		<property name="user">root</property>
		<property name="password">199657</property>		
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">5</property>
		<property name="maxPoolSize">20</property> <!-- intergalactoApp adopts a different approach to configuring statement 
			caching -->
		<property name="maxStatements">0</property>
		<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
	</default-config>
	
	<named-config name="mysql">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/first</property>
		<property name="user">root</property>
		<property name="password">199657</property>
		
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">5</property>
		<property name="maxPoolSize">20</property> <!-- intergalactoApp adopts a different approach to configuring statement 
			caching -->
		<property name="maxStatements">0</property>
		<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
	</named-config>
</c3p0-config>

3,编写JDBCUtil.java工具类

public class JDBCUtil {


	// 获取数据连接池
	private static DataSource ds = null;
	static{
		ds = new ComboPooledDataSource();
	}

	// 获取连接
	public static Connection getConnection() throws SQLException {
		return ds.getConnection();
	}

	// 关闭连接
	public static void close(ResultSet resultSet, PreparedStatement pst, Connection connection) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst != null) {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}

4,然后编写数据库操作即可(已经配置完成,这步只是开始数据库操作了),例如

public static void insertOrDeleteOrUpdate(String sql) {
		try {
			Connection connection = JDBCUtil.getConnection();
			PreparedStatement pst = connection.prepareStatement(sql);
			int execute = pst.executeUpdate();
			System.out.println("执行语句:" + sql + "," + execute + "行数据受影响");
			JDBCUtil.close(null, pst, connection);
		} catch (SQLException e) {
			System.out.println("异常提醒:" + e);
		}
	}
 
	/**
	 *  * 查询,返回结果集  
	 */
	public static List<Map<String, Object>> select(String sql) {
		List<Map<String, Object>> returnResultToList = null;
		try {
			Connection connection = JDBCUtil.getConnection();
			PreparedStatement pst = connection.prepareStatement(sql);
			ResultSet resultSet = pst.executeQuery();
//	returnResultToList = returnResultToList(resultSet);
			JDBCUtil.close(resultSet, pst, connection);
		} catch (SQLException e) {
 
			System.out.println("异常提醒:" + e);
		}
		return returnResultToList;
	}
 
	/**
	 *  * 数据返回集合  * @param resultSet  * @return  * @throws SQLException  
	 */
	public static List<Map<String, Object>> returnResultToList(ResultSet resultSet) {
		List<Map<String, Object>> values = null;
		try {
			// 键: 存放列的别名, 值: 存放列的值.
			values = new ArrayList<>();
			// 存放字段名
			List<String> columnName = new ArrayList<>();
			ResultSetMetaData rsmd = resultSet.getMetaData();
			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				// 字段名
				columnName.add(rsmd.getColumnLabel(i + 1));
			}
 
			System.out.println("表字段为:");
			System.out.println(columnName);
			System.out.println("表数据为:");
			Map<String, Object> map = null;
			// 处理 ResultSet, 使用 while 循环
			while (resultSet.next()) {
				map = new HashMap<>();
				for (String column : columnName) {
					Object value = resultSet.getObject(column);
					map.put(column, value);
					System.out.print(value + "\t");
				}
				// 把一条记录的 Map 对象放入准备的 List 中
				values.add(map);
				System.out.println();
			}
		} catch (SQLException e) {
			System.out.println("异常提醒:" + e);
		}
		return values;
	}

还可以

public static void main(String[] args) throws SQLException, ClassNotFoundException {
		Connection conn=JDBCUtil.getConnection();
		String command="insert into book(id,name,author,sum)values(?,?,?,?)";
        java.sql.PreparedStatement preparedStatement=conn.prepareStatement(command);
       // System.out.println(book.getId());
        preparedStatement.setString(1,"1");
        preparedStatement.setString(2, "2");
        preparedStatement.setString(3, "3");
        preparedStatement.setString(4, "4");
        preparedStatement.executeUpdate();
	}

但是运行报错
com.mchange.v2.c3p0.DriverManagerDataSourceensureDriverLoaded
Could not load driverClass com.mysql.jdbc.Driver

在网上差半天也没解决,后来看到一篇文章写只要是数据库连接肯定不能缺
mysql-connector-java-5.1.26-bin.jar,结果我加入这个jar包后,就可以加载驱动了
所以不只是单单需要这两个jar包,
c3p0-0.9.5.1.jar
mchange-commons-java-0.2.10.jar

完美解决!!!!!