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>
<!-- &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();
}
}
}
文件目录如下: