配置c3p0数据源的时候提示的错误

最近自己在学习的过程中,需要配置c3p0数据源,于是按照网上的教程进行配置,中间无疑会遇到很多错误,挑选几个重要的进行说明

1,java.lang.ClassNotFoundException: com.mchange.v2.ser.Indirector ,空指针异常,

解决办法:这个错误出现的原因是缺少mchange-commons-java-0.2.3.4.jar 文件导致的,读者可以将该文件放在WEB-INF/lib文件下即可

2.提示

java.lang.NullPointerException
	at com.mysql.fabric.jdbc.FabricMySQLDriver.parseFabricURL(FabricMySQLDriver.java:97)
	at com.mysql.fabric.jdbc.FabricMySQLDriver.acceptsURL(FabricMySQLDriver.java:93)
	at java.sql.DriverManager.getDriver(Unknown Source)
	at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:240)
	at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:131)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:156)
	at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:145)
	at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:200)
	at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1086)
	at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1073)
	at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:44)
	at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1810)
	at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:648)

   这个的原因,我这里是因为配置的时候,本来应该是,我写成了

       3.此外,我们可以在配置c3p0的时候指定多个数据源,在实例化数据源的时候通过name属性指定,废话少说,上代码

c3p0-config.xml



	
	
		10
		30
		100
		10
		200
		10
	

	
		com.mysql.jdbc.Driver
		jdbc:mysql://localhost:3306/file
		root
		
		10
		30
		100
		10
		200
		10
	
	
	
		oracle.jdbc.driver.OracleDriver
		jdbc:oracle:thin:@localhost:1521:l
		
		
		10
		30
		100
		10
		200
		10
 
  然后是c3p0数据源的控制类 
  

package com.practice.javaweb.fileupload.db;

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

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {

	private static ComboPooledDataSource c3p0DataSource;
	static {
		c3p0DataSource = new ComboPooledDataSource("mysql");
	}

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection connection = null;
		try {
			connection = c3p0DataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

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

	public static void main(String[] args) {
		Connection conn = getConnection();
		System.out.println(conn.getClass().getName());
		close(conn, null, null);
	}
}



你可能感兴趣的:(Javaweb,jdbc,c3p0)