00 dbutils之前 c3p0连接数据库

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载,博客地址:http://blog.csdn.net/xpala https://blog.csdn.net/xpala/article/details/89116197

数据库准备

CREATE DATABASE dbutils_learn;
USE dbutils_learn;

create table account(
	id int primary key auto_increment,
	name varchar(50),
	money double
);

insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);

SELECT * FROM account;

c3p0的配置文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/2002/xmlspec/dtd/2.10/xmlspec.dtd">
<c3p0-config>
  <default-config>
	<property name="driverClass">com.mysql.jdbc.Driver</property>
	<property name="jdbcUrl">jdbc:mysql://localhost:3306/dbutils_learn</property>
	<property name="user">root</property>
	<property name="password">123456</property>
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
</c3p0-config>

c3p0utils.java

public class C3P0Utils {
	private static DataSource ds=new ComboPooledDataSource();
	
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	public static void closeAll(Connection conn,Statement statement,ResultSet resultSet)

befor_dbutils.java

public class before_dbutils {

	
	@Test
	public void test1() throws SQLException{
		//1.获取连接对象
		Connection con=C3P0Utils.getConnection();
		
		//2.获取statement对象
		Statement stmt=con.createStatement();
		
		//3.执行查询
		ResultSet rs=stmt.executeQuery("select * from account");
		
		//4.遍历
		while(rs.next()){
			System.out.println(rs.getString("name"));
		}
		//5.关闭
		C3P0Utils.closeAll(con, stmt, rs);
		
	}

猜你喜欢

转载自blog.csdn.net/xpala/article/details/89116197