DbUtils

  DbUtils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。
  使用commons-dbutils 的核心工具类:QueryRunner,该类定义了所有操作数据库的方法
  如方法:T query(Connection conn ,String sql, ResultSetHandler<T> rsh, Object... params)

使用:

前提:实体类必须符合javabean规范,并且实体类中的字段必须与数据库表的字段相同。
引入jar文件 :commons-dbutils-1.6.jar

1、简单创建一个工具类JdbcUtil,方便代码调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class jdbcUtil {
private static String url="jdbc:mysql://localhost/test";
private static String user = "root";
private static String password = "root";
//获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner();
}
//获取连接
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

2、调用query方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Test {
public static void main(String[] args) {
//创建一个接受数据库的数组
List<testEntity> list = null;
try {
//获得连接
Connection conn = jdbcUtil.getConnection();
//调用query方法
list = jdbcUtil.getQueryRunner().query(conn, "select * from test"
, new BeanListHandler<testEntity>(testEntity.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (testEntity student : list) {
System.out.println(student.getId());
}
}
}
}

使用C3P0数据库连接池组件优化程序性能

C3P0核心类:ComboPooledDataSource

使用:

前提:引入jar文件 : c3p0-0.9.1.2.jar

方式一:不使用配置文件

1、简单建立一个jdbc工具类,方便方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class JdbcUtil {
private static String url="jdbc:mysql://localhost/test";
private static String user = "root";
private static String password = "root";
private static ComboPooledDataSource dataSource = null; //存储数据源接口池
static{
//初始化操作
dataSource = new ComboPooledDataSource();// 使用默认的配置
dataSource.setJdbcUrl(url);//设置连接字符串
try {
dataSource.setDriverClass("com.mysql.jdbc.Driver");//获取驱动
} catch (PropertyVetoException e) {
e.printStackTrace();
}
dataSource.setUser(user);//用户名
dataSource.setPassword(password);//密码
dataSource.setInitialPoolSize(3);//初始化时获取三个连接
dataSource.setMaxPoolSize(6);//连接池中保留的最大连接数
dataSource.setMaxIdleTime(60); //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃
}

//获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);
}
//获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
public static java.sql.Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
2、执行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class 不使用配置文件 {
public static void main(String[] args) {
//创建一个接受数据库的数组
List<testEntity> list = null;
try {
//获得连接
Connection conn = JdbcUtil.getConnection();
//调用query方法
list = JdbcUtil.getQueryRunner().query(conn, "select * from test"
, new BeanListHandler<testEntity>(testEntity.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (testEntity student : list) {
System.out.println(student.getId());
}
}
}

}

方式二:使用配置文件来初始化

1、将C3P0配置文件c3p0-config.xml放置在工程src目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<c3p0-config>
<!-- 默认加载配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/test</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>

</default-config>

<!-- 指定名称加载配置 -->
<named-config name="oracleConfig">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/fas</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</named-config>

</c3p0-config>
2、简单编写一个工具类,方便代码调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class JdbcUtil2 {
private static ComboPooledDataSource dataSource = null;
static{
//初始化操作
// 自动加载src目录下c3p0的配置文件【c3p0-config.xml】
dataSource = new ComboPooledDataSource();// 使用默认的配置
//使用c3p0-config.xml配置文件中named-config的name属性为C3P0TestName的配置
//dataSource = new ComboPooledDataSource("C3P0TestName");
}

//获取QueryRunner对象
public static QueryRunner getQueryRunner(){
return new QueryRunner(dataSource);
}
//获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
public static java.sql.Connection getConnection(){
try {
return dataSource.getConnection();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
3、执行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class 使用配置文件 {
public static void main(String[] args) {
//创建一个接受数据库的数组
List<testEntity> list = null;
try {
//获得连接
Connection conn = JdbcUtil2.getConnection();
//调用query方法
list = JdbcUtil2.getQueryRunner().query(conn, "select * from test"
, new BeanListHandler<testEntity>(testEntity.class));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
if(list !=null){
for (testEntity student : list) {
System.out.println(student.getId());
}
}
}
}

ps:因作者能力有限,有错误的地方请见谅

  • 喜欢这篇文章的话可以用快捷键 Ctrl + D 来收藏本页
× 请我吃糖~
打赏二维码