今天利用C3P0连接池、自己做的DataSourceUtils工具类做了一个转账操作,运行的时候出现这个错误,都是出现了强转了的,比如:
connection=(Connection)DataSourceUtils.getConnection();
这是报错说的就是类型不匹配,想着可能是导的包不对,然后发现工具类里面导的是 java.sql.Connection ,而dao里面导的是com.mysql.jdbc.connection,
尝试把com.mysql.jdbc.connection改成java.sql.Connection ,就不报错了。
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="day15"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///day15</property> <property name="user">root</property> <property name="password">root</property> <property name="acquireIncrement">10</property> <property name="initialPoolSize">30</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">40</property> <property name="maxStatements">1000</property> <property name="maxStatementsPerConnection">100</property> </named-config> </c3p0-config> |
import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JdbcUtil { private static ComboPooledDataSource ds=new ComboPooledDataSource("day15"); public static DataSource getDataSource(){ return ds; } public static Connection getConnection() { try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void release(Connection conn) throws SQLException{ if(conn!=null){ conn.close(); } } } |