69期-Java SE-040_JDBC-4

 

C3P0 的使用

一、c3p0-config.xml 名稱不能自定義

```xml
<c3p0-config>
    <named-config name="testc3p0">
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="initialPoolSize">20</property>
        <property name="acquireIncrement">5</property>
        <property name="maxPoolSize">40</property>
        <property name="minPoolSize">2</property>
    </named-config>
</c3p0-config>
```

2、Java

```java
package com.southwind;

import com.mchange.v2.c3p0.ComboPooledDataSource;

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

public class Test2 {
    public static void main(String[] args) {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
```



### DBUtils

DBUtils 是一個能夠幫助開發者自動完成數據封裝的工具,查詢的結果直接返回一個對象或者一個集合。

ResultSetHandler 接口的實現類

- BeanHandler            將結果集映射成一個 POJO,適用於查詢結果是一條記錄的
- BeanListHandler      將結果集映射成一個 List,適用於查詢結果是多條記錄的
- MapHandler              將結果集映射成一個 Map,用 Map 的結構來描述數據
- MapListHandler        將結果集映射成一個 MapList,返回一個裝載了 Map 對象的 List 集合

```java
package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.southwind.entity.People;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

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

public class Test3 {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            connection = dataSource.getConnection();
            String sql = "select * from people";
            QueryRunner queryRunner = new QueryRunner();
//            List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class),1);
//            System.out.println(list);
//            People people = queryRunner.query(connection,sql, new BeanHandler<>(People.class),1);
//            System.out.println(people);
//            String sql = "select * from people";
//            People people = queryRunner.query(connection,sql,new BeanHandler<>(People.class));
//            System.out.println(people);
//            List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class));
//            System.out.println(list);
//            String sql = "select * from people where id = ?";
//            Map<String,Object> map = queryRunner.query(connection,sql,new MapHandler());
//            System.out.println(map.get("id"));
//            System.out.println(map.get("name"));
//            System.out.println(map.get("money"));
            List<Map<String,Object>> list = queryRunner.query(connection,sql,new MapListHandler());
            for(Map<String,Object> map:list){
                System.out.println(map.get("id")+"-"+map.get("name")+"-"+map.get("money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
```

QueryRunner 除了能夠簡化查詢操做,同時還能夠簡化增刪改操做。

```java
package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;

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

public class Test4 {
    public static void main(String[] args) {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            Connection connection = dataSource.getConnection();
//            String sql = "insert into people(name,money) values(?,?)";
//            QueryRunner queryRunner = new QueryRunner();
//            int row = queryRunner.update(connection,sql,"小明",600);
//            System.out.println(row);

//            String sql = "update people set name = ?,money = ? where id = ?";
//            QueryRunner queryRunner = new QueryRunner();
//            int row = queryRunner.update(connection,sql,"小紅",800,4);
//            System.out.println(row);

            String sql = "delete from people where id = ?";
            QueryRunner queryRunner = new QueryRunner();
            int row = queryRunner.update(connection,sql,4);
            System.out.println(row);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
```

c3p0-config.xmljava

<c3p0-config>
    <named-config name="testc3p0">
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
        <property name="initialPoolSize">20</property>
        <property name="acquireIncrement">5</property>
        <property name="maxPoolSize">40</property>
        <property name="minPoolSize">2</property>
    </named-config>
</c3p0-config>

People.javamysql

package com.southwind.entity;

public class People {
    private Integer id;
    private String name;
    private Integer money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

    public People(Integer id, String name, Integer money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    public People() {
    }
}

Test.javasql

package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

public class Test {
    public static void main(String[] args) {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8");
            dataSource.setUser("root");
            dataSource.setPassword("root");
            dataSource.setInitialPoolSize(20);
            dataSource.setAcquireIncrement(5);
            dataSource.setMaxPoolSize(40);
            dataSource.setMinPoolSize(2);
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        } catch (SQLException e){
            e.printStackTrace();
        }
    }
}

 

Test2.javaapache

package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.southwind.entity.People;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test2 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            connection = dataSource.getConnection();
            String sql = "select * from people where id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,1);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                int money = resultSet.getInt(3);
                People people = new People(id,name,money);
                System.out.println(people);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(connection!=null){
                    connection.close();
                }
                if(preparedStatement!=null){
                    preparedStatement.close();
                }
                if(resultSet!=null){
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Test3.javaide

package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.southwind.entity.People;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

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

public class Test3 {
    public static void main(String[] args) {
        Connection connection = null;
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            connection = dataSource.getConnection();
            String sql = "select * from people";
            QueryRunner queryRunner = new QueryRunner();
//            List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class),1);
//            System.out.println(list);
//            People people = queryRunner.query(connection,sql, new BeanHandler<>(People.class),1);
//            System.out.println(people);
//            String sql = "select * from people";
//            People people = queryRunner.query(connection,sql,new BeanHandler<>(People.class));
//            System.out.println(people);
//            List<People> list = queryRunner.query(connection,sql,new BeanListHandler<>(People.class));
//            System.out.println(list);
//            String sql = "select * from people where id = ?";
//            Map<String,Object> map = queryRunner.query(connection,sql,new MapHandler());
//            System.out.println(map.get("id"));
//            System.out.println(map.get("name"));
//            System.out.println(map.get("money"));
            List<Map<String,Object>> list = queryRunner.query(connection,sql,new MapListHandler());
            for(Map<String,Object> map:list){
                System.out.println(map.get("id")+"-"+map.get("name")+"-"+map.get("money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(connection != null){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 

Test4.java工具

package com.southwind.test;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;

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

public class Test4 {
    public static void main(String[] args) {
        try {
            ComboPooledDataSource dataSource = new ComboPooledDataSource("testc3p0");
            Connection connection = dataSource.getConnection();
//            String sql = "insert into people(name,money) values(?,?)";
//            QueryRunner queryRunner = new QueryRunner();
//            int row = queryRunner.update(connection,sql,"小明",600);
//            System.out.println(row);

//            String sql = "update people set name = ?,money = ? where id = ?";
//            QueryRunner queryRunner = new QueryRunner();
//            int row = queryRunner.update(connection,sql,"小紅",800,4);
//            System.out.println(row);

            String sql = "delete from people where id = ?";
            QueryRunner queryRunner = new QueryRunner();
            int row = queryRunner.update(connection,sql,4);
            System.out.println(row);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
相關文章
相關標籤/搜索