c3p0

c3p0

c3p0是一个开源的JDBC连接池,它相对于DBCP和Druid来说有一个更方便的配置连接方式,就是自动读取配置文件。配置文件名:c3p0-config.xml,放置与src目录下。配置文件的demo如下:

<?xml version="1.0" encoding="UTF-8"?>
 <c3p0-config> 
    <!-- 默认读的配置文件 -->
     <default-config> 
         <property name="driverClass">com.mysql.jdbc.Driver</property> 
         <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/testjdbc?characterEncoding=utf-8</property> 
         <property name="user">root</property> 
         <property name="password">123</property> 
         <property name="minPoolSize">5</property> 
         <property name="initialPoolSize">5</property> 
     </default-config>
     
     <!-- 额外的,备用的数据库,比如oracle -->
     <named-config name="mydb"> 
         <property name="driverClass">com.mysql.jdbc.Driver</property> 
         <property name="jdbcUrl">jdbc:mysql://192.168.200.200:3306/mydb?characterEncoding=utf-8</property> 
         <property name="user">root</property> 
         <property name="password">root</property> 
         <property name="minPoolSize">5</property> 
         <property name="initialPoolSize">5</property> 
     </named-config>
</c3p0-config>

默认的数据库连接使用的是default-config标签里的
当需要连接其它的数据库时,不需要再重新编辑配置文件,直接选择其它的数据库即可
在空参的时候是选择默认的default-config数据库连接
传入named-config的名字就可以直接连接其它数据库。

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import utils.JDBCUtils;

public class C3p0Demo {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            //创建数据库连接池对象
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            /*
            //必选参数的设置
            cpds.setDriverClass("com.mysql.jdbc.Driver");//和Druid不同,Druid传的是一个对象dds.setDriver(new Driver())
            cpds.setJdbcUrl("jdbc:mysql:///testjdbc");
            cpds.setUser("root");
            cpds.setPassword("123");
            */
            //从数据库连接池中获取连接对象
            conn = cpds.getConnection();
            stat = conn.createStatement();
            String sql = "select * from users;";
            rs = stat.executeQuery(sql);
            while(rs.next()) {
                System.out.println(rs.getString("username") + "---" + rs.getString("password"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(conn, stat, rs);
        }
    }
}

发布了11 篇原创文章 · 获赞 12 · 访问量 1369
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

c3p0报错,版本为最新,求大神帮忙!!!!!!!!

11-05

详细:之前都是好使的,因为需要,所配置的事务,加上事务后,运行到加注解的方法就报错。 依赖 ``` <!--c3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!--aop依赖--> <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.9</version> </dependency> <!--aop依赖,shiro权限需要--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.google.zxing/javase --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.3</version> </dependency> ``` 配置 ``` <!--开启AOP的对类代理--> <aop:aspectj-autoproxy proxy-target-class="true"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${db.driver}"/> <property name="jdbcUrl" value="${db.url}"/> <property name="user" value="${db.username}"/> <property name="password" value="${db.password}"/> <property name="maxIdleTime" value="${db.maxIdleTime}"/> <property name="maxPoolSize" value="${db.maxPoolSize}"/> <property name="minPoolSize" value="${db.minPoolSize}"/> <property name="initialPoolSize" value="${db.initialPoolSize}"/> <property name="acquireRetryDelay" value="${db.acquireRetryDelay}"/> <property name="acquireIncrement" value="${db.acquireIncrement}"/> <property name="acquireRetryAttempts" value="${db.acquireRetryAttempts}"/> <property name="breakAfterAcquireFailure" value="${db.breakAfterAcquireFailure}"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.chao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 注解方式配置事物 --> <tx:annotation-driven transaction-manager="transactionManager" /> ``` 错误信息: 严重: Servlet.service() for servlet [SpringServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract] with root cause java.lang.AbstractMethodError: Method com/mchange/v2/c3p0/impl/NewProxyPreparedStatement.isClosed()Z is abstract at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.isClosed(NewProxyPreparedStatement.java) at org.apache.ibatis.executor.BaseExecutor.closeStatement(BaseExecutor.java:285) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:65) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) at com.github.pagehelper.PageInterceptor.executeAutoCount(PageInterceptor.java:201) at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:113) at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61) at com.sun.proxy.$Proxy31.query(Unknown Source) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) at com.sun.proxy.$Proxy27.selectList(Unknown Source) at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览