DBUtils

70 阅读1分钟

c3p0-config

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <named-config name="testc3p0">
        <property name="driverClass" >com.mysql.jdbc.Driver</property>
        <property name="user" >root</property>
        <property name="password" >12345678</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/student?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false</property>
    </named-config>
</c3p0-config>

JDBC Tool

package com.jp.JDBC;

import com.mchange.v2.c3p0.ComboPooledDataSource;

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


public class JDBCTool {

    private static ComboPooledDataSource comboPooledDataSource = null;
    private static Connection connection = null;

    static {
        comboPooledDataSource = new ComboPooledDataSource("testc3p0");

    }

    public static Connection getConnecion()  {
        try {
            connection = comboPooledDataSource.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
}
public static List<Student> newFindAllFunc() {
        Connection connection = null;
        List<Student> studentList = null;
        try {
            connection = JDBCTool.getConnecion();
            String sql = "select * from student";
            QueryRunner queryRunner = new QueryRunner();
            studentList = queryRunner.query(connection, sql, new BeanListHandler<>(Student.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return studentList;
    }