Simple Use of JDBC and c3p0

Keywords: MySQL JDBC Database Java

dbcp is an open source connection pool for apache.

C3P0 is an open source JDBC connection pool, which implements data source and JNDI binding, supports JDBC3 specification and JDBC2 standard extension. At present, open source projects using it include Hibernate, Spring, etc.

Simple use of dbcp

First, you must import the relevant jar packages when you use them. You need to import the following jar packages:

commons-dbcp-1.4.jar
commons-pool-1.5.6.jar
mysql-connector-java-5.1.28-bin.jar

commons-dbcp and commons-pool are jar packages that dbcp needs to use.
mysql-connector-java is the database driver package provided by mysql.

Import all three jar packages above into WebRoot/WEB-INF/lib of the java web project.

Manual Configuration Use

public void test1() throws SQLException {
    // 1. Manual configuration
    BasicDataSource bds = new BasicDataSource();
    // Four basic conditions for connecting to a database need to be set
    bds.setDriverClassName("com.mysql.jdbc.Driver"); // Loading Mysql Driver
    bds.setUrl("jdbc:mysql:/// test ";// test is the name of the database
    bds.setUsername("root");
    bds.setPassword("123");

    // Get a Connection
    Connection con = bds.getConnection();

    ResultSet rs = con.createStatement().executeQuery(
            "select * from employee"); // Query the employee table in the database

    while (rs.next()) {

        System.out.println(rs.getDouble("salary") + "  "
                + rs.getString("name"));
    }

    rs.close();
    con.close(); // Reload the Connection object into the connection pool.
}

Automatic configuration (using configuration files)

Create dbcp.properties and place them in the src root directory of the project, as follows

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test
username=root
password=123

Then the code uses:

    public void test3() throws Exception {

        Properties props = new Properties();

        //FileInputStream fis = new FileInputStream(
        //D: MyDocument Documents Documents Myeclipse day18-c3p0 src dbcp.properties;//create DBCP properties under src.

        String path = this.getClass().getResource("/").getPath();
        FileInputStream fis = new FileInputStream(
                path + "dbcp.properties");  // Create dbcp.properties under src

        props.load(fis);

        DataSource ds = BasicDataSourceFactory.createDataSource(props);

        // Get a Connection
        Connection con = ds.getConnection();

        ResultSet rs = con.createStatement().executeQuery(
                "select * from employee");

        while (rs.next()) {

            System.out.println(rs.getDouble("salary") + "  "
                    + rs.getString("name"));
        }

        rs.close();
        con.close(); // Reload the Connection object into the connection pool.
    }

This is how dbcp is used

C3P0 Simple Use

C3P0 is basically used in the same way as DBCP. First, we need to import the following two jar packages:

mysql-connector-java-5.1.28-bin.jar     // mysql driver package
c3p0-0.9.1.2.jar        //C3P0 jar package

Manual use

public void test1() throws PropertyVetoException, SQLException {
    ComboPooledDataSource cpds = new ComboPooledDataSource();

    cpds.setDriverClass("com.mysql.jdbc.Driver");
    cpds.setJdbcUrl("jdbc:mysql:/// test ";// test is the database in mysql
    cpds.setUser("root");   
    cpds.setPassword("123");

    // Get a Connection
    Connection con = cpds.getConnection();

    ResultSet rs = con.createStatement().executeQuery(
            "select * from employee");

    while (rs.next()) {

        System.out.println(rs.getDouble("salary") + "  "
                + rs.getString("name"));
    }

    rs.close();
    con.close(); // Reload the Connection object into the connection pool.

}

Automation (using configuration files)

The configuration file of c3p0 can be either properties or xml.

If the configuration file of c3p0 is called c3p0.properties or c3p0-config.xml and placed in the classpath path path path (for web applications, the classes directory), then c3p0 will be automatically searched.

Note: We only need to place the configuration file under src at that time.

Use:
ComboPooledDataSource cpds = new ComboPooledDataSource();
// It finds the configuration file with the specified name in the specified directory and loads its contents.

Create c3p0-config.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql:/// test </property > <! -- test is a database - > test
        <property name="user">root</property>
        <property name="password">123</property>
    </default-config>

</c3p0-config>

Then use in the code:

public void test2() throws PropertyVetoException, SQLException {
    ComboPooledDataSource cpds = new ComboPooledDataSource();

    // Get a Connection
    Connection con = cpds.getConnection();

    ResultSet rs = con.createStatement().executeQuery(
            "select * from employee");

    while (rs.next()) {

        System.out.println(rs.getDouble("salary") + "  "
                + rs.getString("name"));
    }

    rs.close();
    con.close(); // Reload the Connection object into the connection pool.

    // String path = this.getClass().getResource("/").getPath();
    // System.out.println(path);
}

These are the simple uses of these two open source connection pools

Posted by grayscale2005. on Sun, 31 Mar 2019 17:51:28 -0700