SubList分页-006-jdbcUtils类

  1. 导入c3p0-0.9.1.2、commons-dbutils-1.4、mysql-connector-java-5.1.7-bin

  2. 新建c3p0-config.xml,配置数据库连接信息




    
   
        
        root
        
        123123
        
        com.mysql.jdbc.Driver
        
        jdbc:mysql://127.0.0.1:3306/Mushroom?useUnicode=true&characterEncoding=utf-8
        
       
        5
        
       10
       
        10 
        
        50
        
        20 
        
        5
        
   
    

  1. 实现jdbcUtils类
package top.itcourse.page.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/*
 * JDBCUtils类构建:
 *      1.导包
 *      2.配置c3p0-config.xml
 *      3.新建静态DataSource对象
 *      4.新建获取Connection的静态方法getConnection
 *      5.新建释放Connection的静态方法relase
 */
public class JDBCUtils {
    
    // 3.新建静态DataSource对象
    private static DataSource dataSource = null;
    static {
        // newMem是c3p0-config.xml中的named-config
        dataSource = new ComboPooledDataSource("newMem");
    }
    
    // 4.新建获取Connection的静态方法getConnection
    public static Connection getConnection() {
        
        Connection connection = null;
        
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            System.err.println("新建Connection时出现异常~~~~~~~~~~~");
        } 
            
        return connection;
    }
    
    // 5.新建释放Connection的静态方法relase
    public static void relase(Connection connection) {
        if (connection != null ) {
            try {
                connection.close();
            } catch (SQLException e) {
                System.err.println("释放Connection时出现异常~~~~~~~");
            }
            
        }
    }
}
 

源码下载

关注下方的微信公众号,回复:java_div_page.code



SubList分页-006-jdbcUtils类_第1张图片



欢迎加入交流群:451826376


更多信息:www.itcourse.top

完整教程PDF版本下载

你可能感兴趣的:(SubList分页-006-jdbcUtils类)