1.1、c3p0连接池基本使用(源码配置方式、xml文件配置方式、properties文件配置方式)

操作步骤

1、设置连接数据库的基本信息
2、从连接池中获取一个连接对象
3、获取传输器
4、利用传输器执行SQL语句,返回执行结果
5、处理执行结果
6、释放资源

第一种方式:源代码中配置

	@Test
	public void testC3P0(){
		Connection connection = null;
		PreparedStatement pStat = null;
		ResultSet resultSet = null;
		//创建连接池对象
		ComboPooledDataSource pooledDataSource = new ComboPooledDataSource();
		try {
			//设置连接数据库的基本信息
			pooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
			pooledDataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/jt_db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
			pooledDataSource.setUser("root");
			pooledDataSource.setPassword("root");
			//从连接池中获取一个连接对象
			connection = pooledDataSource.getConnection();
			//3、获取传输器
			String s = "select * from account;";
			pStat = connection.prepareStatement(s);
			//4、利用传输器执行SQL语句,返回执行结果
			resultSet  = pStat.executeQuery();
			//5、处理执行结果
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				double money = resultSet.getDouble("money");
				System.out.println("id:" + id + "\tname:" + name + "\tmoney:" + money);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//6、释放资源
			JDBC.close(connection,pStat,resultSet);
		}
		System.out.println("------------------------------");
	}

第二种方式:xml文件配置

1、新建xml文件路径
2、将c3p0-config.xml文件放入路径中

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<default-config>
		<property name="driverClass">
			com.mysql.cj.jdbc.Driver
		</property>
		<property name="jdbcUrl">
			jdbc:mysql://127.0.0.1:3306/jt_db?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8&amp;useSSL=false
		</property>
		<property name="user">
			root
		</property>
		<property name="password">
			root
		</property>
	</default-config>
</c3p0-config>

3、添加配置,创建连接池时,加入配置文件路径

		/*第二种方式xml文件中配置*/
		ComboPooledDataSource pooledDataSource = new ComboPooledDataSource("/resource/c3p0-config.xml");

若在idea中将resource目录已经设置为资源更目录,则可以不写配置文件路径
1.1、c3p0连接池基本使用(源码配置方式、xml文件配置方式、properties文件配置方式)_第1张图片
4、后续直接从连接池中获取连接对象

			//从连接池中获取一个连接对象
			connection = pooledDataSource.getConnection();
			//3、获取传输器
			String s = "select * from account;";
			pStat = connection.prepareStatement(s);
			//4、利用传输器执行SQL语句,返回执行结果
			resultSet  = pStat.executeQuery();
			//5、处理执行结果
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				double money = resultSet.getDouble("money");
				System.out.println("id:" + id + "\tname:" + name + "\tmoney:" + money);
			}

第三种方式:properties文件配置

1、新建properties对象
2、获取properties配置文件路径
3、设置文件路径编码,获取文件
4、读取文件内容
5、配置连接池(配置同第一种方式,相关参数从properties对象中获得)

			/*第三种方式:properties文件中配置*/
			//新建properties对象
			Properties properties = new Properties();
			//获得src下的c3p0.properties的路径
			String c3p0Properties = this.getClass().getClassLoader().getResource("config/c3p0.properties").getPath();
			//路径的编码是UTF-8
			c3p0Properties = URLDecoder.decode(c3p0Properties, "utf-8");
			//得到文件c3p0.properties文件
			java.io.File c3p0File = new java.io.File(c3p0Properties);
			//读取c3p0文件的内容
			properties.load(new FileInputStream(c3p0File));

			//properties方式配置
			pooledDataSource.setDriverClass(properties.getProperty("c3p0.driverClass"));
			pooledDataSource.setJdbcUrl(properties.getProperty("c3p0.jdbcUrl"));
			pooledDataSource.setUser(properties.getProperty("c3p0.user"));
			pooledDataSource.setPassword(properties.getProperty("c3p0.password"));

			//从连接池中获取一个连接对象
			connection = pooledDataSource.getConnection();
			//3、获取传输器
			String s = "select * from account;";
			pStat = connection.prepareStatement(s);
			//4、利用传输器执行SQL语句,返回执行结果
			resultSet  = pStat.executeQuery();
			//5、处理执行结果
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				double money = resultSet.getDouble("money");
				System.out.println("id:" + id + "\tname:" + name + "\tmoney:" + money);
			}

properties配置文件

c3p0.driverClass=com.mysql.cj.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/jt_db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
c3p0.user=root
c3p0.password=root

你可能感兴趣的:(JavaWeb基础)