zoukankan      html  css  js  c++  java
  • 数据库复用代码【c3p0配置文件、数据库连接池】

    前言

    为了复用,记载一些以前写过的工具类、方法

    c3p0配置文件【c3p0-config.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/zhongfucheng?characterEncoding=utf8</property>
            <property name="user">root</property>
            <property name="password">root</property>
    
            <property name="acquireIncrement">5</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">5</property>
            <property name="maxPoolSize">20</property>
        </default-config>
    
        <named-config name="mysql">
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/zhongfucheng</property>
            <property name="user">root</property>
            <property name="password">root</property>
    
            <property name="acquireIncrement">5</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">5</property>
            <property name="maxPoolSize">20</property>
        </named-config>
    
    
        <named-config name="oracle">
            <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
            <property name="jdbcUrl">jdbc:oracle:thin:@//localhost:1521/事例名...</property>
            <property name="user">用户名</property>
            <property name="password">密码</property>
    
            <property name="acquireIncrement">5</property>
            <property name="initialPoolSize">10</property>
            <property name="minPoolSize">5</property>
            <property name="maxPoolSize">20</property>
        </named-config>
    </c3p0-config>
    
    
    

    数据库连接池c3p0

    
        import com.mchange.v2.c3p0.ComboPooledDataSource;
    
        import javax.sql.DataSource;
        import java.sql.Connection;
        import java.sql.SQLException;
    
        /**
         * Created by ozc on 2017/2/22.
         */
        public class Utils2DB {
    
            private static ComboPooledDataSource comboPooledDataSource = null;
    
                static {
    
                    //它会自动寻找配置文件,节点为mysql的数据库
                    comboPooledDataSource = new ComboPooledDataSource();
                }
    
    
            public static DataSource getDataSource() {
                return comboPooledDataSource ;
            }
    
            public static Connection connection() {
                try {
                    return comboPooledDataSource.getConnection();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("数据库初始化失败了!");
                }
            }
        }
    
    如果您觉得这篇文章帮助到了您,可以给作者一点鼓励



  • 相关阅读:
    常用正则、正则表达式
    移动端虚拟手机键盘
    如何隐藏滚动条还能实现滚动的功能
    醉盏的第一篇博客-关于title的换行处理
    Mybatis-Generator自动生成Dao、Model、Mapping等相关映射文件(懒人版)
    java学习笔记----@Override的作用
    web项目生成web.xml的两种方式
    angular分页插件tm.pagination二次触发问题解决歪方案
    Ubuntu Server17.10配置静态IP
    cors 的深入细节
  • 原文地址:https://www.cnblogs.com/zhong-fucheng/p/7203012.html
Copyright © 2011-2022 走看看