zoukankan      html  css  js  c++  java
  • 链接数据库超级简单的工具类C3P0谁用谁知道

    首先导入两个jar包一个C3P0的

    c3p0-0.9.1.2.jar

    另一个是你所使用的数据库的

    mysql-connector-java-5.1.7-bin.jar

    接下来就是直接上代码:

    package com.c3p0.cn;
    
    import org.junit.Test;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class C3P0_Test {
        /**
         * 硬链接
         * @throws Exception
         */
        @Test
        public void c3p0_Test_old() throws Exception {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();//数据源端口池
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/staff");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            dataSource.setInitialPoolSize(3);//设置初始链接数
            dataSource.setMaxPoolSize(6);//设置最大连接数
            dataSource.setMaxIdleTime(3000);//设置最大活跃时间
            dataSource.getConnection().prepareStatement("delete from user where id = 18").executeUpdate();
        }
        
        /**
         * 配置方式链接
         * @throws Exception
         */
        @Test
        public void c3p0_Test() throws Exception {
            ComboPooledDataSource dataSource = new ComboPooledDataSource();//默认会调用src下面的c3p0_config.xml配置文件
            dataSource.getConnection().prepareStatement("delete from user where id = 19").executeUpdate();
        }
    }

    作为JDBC使用的时候可以结合DBUtils的包一起使用,而且代码实现非常方便(下面就是采用两个jar包,结合使用的查询数据库中元素的方式)

    package com.xinzhi.utils;
    
    import org.apache.commons.dbutils.QueryRunner;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class JdbcUtils {
        public static QueryRunner getQueryRunner(){
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
                QueryRunner queryRunner = new QueryRunner(comboPooledDataSource);
                return queryRunner;
        }
    }
    package com.xinzhi.dao.impl;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import com.xinzhi.dao.UserDao;
    import com.xinzhi.empity.User;
    import com.xinzhi.utils.JdbcUtils;
    
    public class UserDaoImpl implements UserDao {
    
        public List<User> getAll() {
            QueryRunner queryRunner = JdbcUtils.getQueryRunner();
            String sql = "select * from userempity";
            List<User> users = new ArrayList<User>();
            try {
                users = queryRunner.query(sql,
                        new BeanListHandler<User>(User.class));
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return users;
        }
    
    }

    硬链接是不需要配置文件的,而配置链接是需要配置文件的,而c3p0在创建对象的时候默认会在src文件夹下面寻找一个叫c3p0_config.xml的配置文件

    而这个默认的配置文件是有固定的格式参考如下,其中提供了可链接多个数据库的方式:

    c3p0-config.xml

    <c3p0-config>
    
        <default-config>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/staff</property>
            <property name="user">root</property>
            <property name="password">123456</property>
        </default-config>
    
        <named-config name="oracle_database">
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3306/staff</property>
            <property name="user">root</property>
            <property name="password">123456</property>
        </named-config>
    
    </c3p0-config>
  • 相关阅读:
    js_未结束的字符串常量
    [转]关于项目管理的思考
    Nhibernate理解
    Visual Studio 2005常用插件搜罗
    基本概念
    resharper 2.0
    Nhibernate资源
    [转]关于项目管理的知识点
    style
    带分数 蓝桥杯
  • 原文地址:https://www.cnblogs.com/ShaoXin/p/6841256.html
Copyright © 2011-2022 走看看