• c3p0连接池


    c3p0连接池

    需要的架包
    1.mysql-connector-java-5.1.7-bin.jar
    2.c3p0-0.9.2.1.jar
    3.mchange-commons-java-0.2.3.4.jar
    c3p0-config.xml配置文件
    <!-- 
    *这里为了演示只配置了最基本的,和普通连接数据库类似
    *这里的name属性名字不能自己杜撰
    *这里的named-config的name属性一定要写,不然找不到配置
    *如果出现java.sql.SQLException: 
    Connections could not be acquired from the underlying database!
    错误,通常是c3p0-config.xml文件配置错误,检查账号,密码,驱动是否正确

     -->

    <c3p0-config>
            <named-config name="tang">
                <property name="driverClass">com.mysql.jdbc.Driver</property>
                <property name="jdbcUrl">jdbc:mysql://localhost:3306/tang</property>
                <property name="user">root</property>
                <property name="password">jarrem</property>
                <property name="maxActive">20</property>
                <property name="maxIdle">10</property>
                <property name="minIdle">1</property>
                <property name="initialSize">2</property>
            </named-config>
        </c3p0-config>

    java代码

    package com.tang.c3p0;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import javax.sql.DataSource;
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    public class c3p0Test {
        private Connection connection=null;
        private DataSource ds=null;
        public c3p0Test(){
            //创建数据源,通过named-config的name属性加载xml配置文件
            ds=new ComboPooledDataSource("tang");
             try {
                 //获取Connection对象
                connection = ds.getConnection();
                PreparedStatement statement = connection.prepareStatement("select * from student;");
                ResultSet result = statement.executeQuery();
                while(result.next()){
                    System.out.println(result.getString(5)+"    "+result.getString(3));
                }
             } catch (SQLException e) {
                    e.printStackTrace();
            }
        }
        public static void main(String args[]){
            new c3p0Test();
        }
    }



  • 相关阅读:
    HashMap实现分析
    序列化与transient
    MySQL计划任务(事件调度器)(Event Scheduler)[转]
    利用innodb_force_recovery修复MySQL数据页损坏
    Java对Jar文件的操作[转]
    聚集索引与非聚集索引
    JVM学习(二)
    一句道破所有的springmvc(面试必备)
    springboot中的外界jar的引入:
    springboot中的springSession的存储和获取
  • 原文地址:https://www.cnblogs.com/jarrem/p/5910824.html
Copyright © 2020-2023  润新知