JavaSE模式使用c3p0进行mysql数据库连接

 c3p0-config.xml:


<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<!-- <property name="jdbcUrl">jdbc:mysql:///web15</property> -->
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/pachong?serverTimezone=UTC</property>
		<!-- &amp;useSSL=false -->
		<property name="user">root</property>
		<property name="password">123456</property>
		<property name="initialPoolSize">5</property>
		<property name="maxPoolSize">20</property>
	</default-config>
//下面表示数据库可以进行选择
	<named-config name="itheima">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///web15</property>
		<property name="user">root</property>
		<property name="password">123456</property>
	</named-config>


</c3p0-config>

sql连接代码:

package jingdong.main;

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

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import jingdong.model.JdModel;


public class JdongMain {
	@Test
	public void connection() {
		
		System.out.println("nihao");
		Connection conn = null;
		System.setProperty("com.mchange.v2.c3p0.cfg.xml", this.getClass().getClassLoader().getResource("").getPath() + "c3p0-config.xml");
		//src下的c3p0路径,这句代码在SE下需要额外加上
		DataSource dataSource = new ComboPooledDataSource();
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		QueryRunner runner = new QueryRunner();
		
		String sql = "select * from bookInfo";
		try {
			List<JdModel> all = runner.query(conn, sql,new BeanListHandler<JdModel>(JdModel.class));
		/*	//Shop shop = runner.query(conn, sql, new BeanHandler<Shop>(Shop.class));
*/			System.out.println(all);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

文件目录如下:

©️2020 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值