C3P0 的使用 1、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&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.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&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.java
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.java
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.java
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.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(); } } } }
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(); } } }
原文地址:https://www.cnblogs.com/HiJackykun/p/11182587.html
时间: 2024-11-09 00:58:25