1. 准备Jar包(导包操作),配置文件
<c3p0-config>
<named-config name="myc3p0">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///day0904</property>
<property name="user">root</property>
<property name="password">1234</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">5</property>
<property name="checkoutTimeout">3000</property>
</named-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///day0903</property>
<property name="user">root</property>
<property name="password">1234</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">3000</property>
</default-config>
</c3p0-config>
2. 获取连接池
package com.fy.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
public class JdbcUtils {
private static ComboPooledDataSource ds = new ComboPooledDataSource("myc3p0");
public static DataSource getDataSource(){
return ds;
}
}
项目总览
3. 增删改的功能实现:创建dbutils的核心类对象queryRunner
package com.fy.test;
import com.fy.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import java.sql.SQLClientInfoException;
import java.sql.SQLException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
Scanner sc = new Scanner(System.in);
System.out.println("输入姓名:");
String username = sc.next();
System.out.println("输入新密码:");
String password = sc.next();
String sql = "update user set upassword = ? where username = ?";
Object[] params = {
password,username};
qr.update(sql,params);
int row = qr.update(sql, params);
System.out.println(row);
}
}
查询的功能实现
1. 创建User实例
package com.fy.domain;
public class User {
private int id;
private String username;
private String password;
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
package com.fy.vo;
public class QueryVo {
private int avg;
private int max;
public int getAvg() {
return avg;
}
public void setAvg(int avg) {
this.avg = avg;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
}
2. 查询功能代码实现
package com.fy.test;
import com.fy.domain.User;
import com.fy.utils.JdbcUtils;
import com.fy.vo.QueryVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import javax.tools.ForwardingFileObject;
import java.util.*;
public class TestQuery {
public static void main(String[] args) throws Exception {
f9();
}
public static void f1() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id, username,upassword password from user where uid = 8";
User u = qr.query(sql,new BeanHandler<User>(User.class));
System.out.println(u);
}
public static void f2() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id, username,upassword password from user";
List<User> users = qr.query(sql,new BeanListHandler<User>(User.class));
for(User user:users){
System.out.println(user);
}
}
public static void f3() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select avg(uid),max(uid) from user";
Object o = qr.query(sql,new ScalarHandler(2));
Object z = qr.query(sql,new ScalarHandler("avg(uid)"));
System.out.println(o);
System.out.println(z);
}
public static void f4() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select avg(uid) avg,max(uid) max from user";
QueryVo vo = qr.query(sql, new BeanHandler<QueryVo>(QueryVo.class));
System.out.println(vo.getAvg());
System.out.println(vo.getMax());
}
public static void f5() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id,username,upassword password from user where uid = ?";
Object[] o = qr.query(sql, new ArrayHandler(), 15);
System.out.println(Arrays.toString(o));
}
public static void f6() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id,username,upassword password from user";
List<Object[]> o = qr.query(sql, new ArrayListHandler());
for(Object[] objects:o){
for(Object object:objects){
System.out.println(Arrays.toString(objects));
}
}
}
public static void f7() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id,username,upassword password from user";
List<Object> o = qr.query(sql, new ColumnListHandler(3));
for(Object object:o){
System.out.println(object);
}
}
public static void f8() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id,username,upassword password from user where uid = ?";
Map<String, Object> map = qr.query(sql, new MapHandler(), 15);
Set<Map.Entry<String, Object>> set = map.entrySet();
Iterator<Map.Entry<String, Object>> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, Object> en = it.next();
System.out.println(en.getKey()+"---"+en.getValue());
}
}
public static void f9() throws Exception {
QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
String sql = "select uid id,username,upassword password from user";
List<Map<String, Object>> list = qr.query(sql, new MapListHandler());
for(Map<String, Object> map:list) {
System.out.println(map);
}
}
}
3. 运行结果
3.1 数据库数据
3.2 BeanHandler对象查询
3.3 BeanListHandler对象查询
3.4 ScalarHandler对象查询
3.5 MapListHandler对象查询