C3P0與QueryRunner的結合使用

依賴包

         c3p0-0.9.1.2.jar

        commons-dbutils-1.6.jar

        mysql-connector-java-5.1.12-bin.jar

C3P0的配置文件c3p0-config.xml

<c3p0-config>
    
    <!-- c3p0默認配置,下面還可以配置多個數據庫 -->
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test
        </property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">6</property>
        <property name="maxPoolSize">50</property>
        <property name="maxIdleTime">1000</property>
    </default-config>

</c3p0-config>

BserUtils類,c3p0和QueryRunner的核心類

import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class BaseUtils {

    //初始化c3p0
    private static DataSource dataSource=null;
    
    static{
        //自動加載src目錄下面的c3p0的配置文件,【c3p0-config.xml】
        dataSource = new ComboPooledDataSource();
    }
    
    public static QueryRunner getQueryRunner(){
        //第一步:創建QueryRunner對象,傳入連接池對象
        //在創建QueryRunner對象的時候,如果傳入數據對象dataSource,
        //那麼在使用QueryRunner對象的方法時候,就不需要傳入連接對象
        QueryRunner query=new QueryRunner(dataSource);
        //第二步:會自動從數據源中獲取連接(不用關閉連接)
        return query;
    }
    
    /***
     * 實現增刪改的公共方法
     * @param sql
     * @param arr
     * @return
     */
    public static boolean addUpdateDelete(String sql,Object[] arr){
        QueryRunner qr=getQueryRunner();
        int count;
        try {
            count = qr.update(sql, arr);
            if(count>0){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
}

在DAO層調用BaseUtils

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.dao.UserDao;
import com.entity.User;
import com.util.BaseUtils;


public class UserDaoImpl implements UserDao{


    @Override
    public List<User> selectUser() {
        //創建QueryRunner
        //記住查詢是BeanListHandler區別增刪改的方法BeanHandler
        QueryRunner qr=BaseUtils.getQueryRunner();
        try {
            String sql="select * from user ";           
            List<User> list =(List<User>)qr.query(sql, new BeanListHandler(User.class));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    //如果沒用c3p0連接池,而是普通連接方式則稍有變化
    @Override
    public List<User> selectUser() {
        //連接數據庫
        Connection con = Conn.getConnection;//Conn類在下邊
        QueryRunner qr=new QueryRunner();
        try {
            String sql="select * from user ";           
            List<User> list =(List<User>)qr.query(con,sql, new BeanListHandler(User.class));
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


}

連接數據庫

public class Conn{
    static Connection conn;
    public static Connection getConnection(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
    try{
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true","root","password");
    }catch(SQLException e){
        e.printStackTrace();
    }
    return conn;
    }
}

該文有部分內容從其他網站轉載,加上自己的一些理解及日常應用整理至此

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章