<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/java</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="mysql"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/java</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquireIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </named-config> </c3p0-config>
连接数据库,DBManager.java
package com.hbsi.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.sql.DataSource; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBManager { private static ComboPooledDataSource ds = null; static{ try{ //创建连接池 ds = new ComboPooledDataSource("mysql"); }catch(Exception e){ e.printStackTrace(); } } //获取链接 public static DataSource getDataSource(){ return ds; } }
表admin映射成class类,admin.java
package com.hbsi.domain; import java.io.Serializable; /** * 把一个表admin映射(mapping)成Class类 * * @author redarmy * */ //可序列化的接口 public class Admin implements Serializable { /** * id int(11) not null primary key auto_increment,---calss 成员属性 * name varchar(40) unique, * pass varchar(12), * sex enum('男','女'), * role int(11) * * 表 class * int int,Integer * varchar Stirng * enum Stirng * role int */ private static final long serialVersionUID = 1L; // 把admin表中的字段映射成Admin类的成员属性(字段类型的转换如下 int --int Integer,varchar--String ) private int id; private String name; private String pass; private String sex; private int role; public Admin(int id, String name, String pass, String sex, int role) { super(); this.id = id; this.name = name; this.pass = pass; this.sex = sex; this.role = role; } //构造器 public Admin() { super(); // TODO Auto-generated constructor stub } public Admin(String name, String pass, String sex, int role) { super(); this.name = name; this.pass = pass; this.sex = sex; this.role = role; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getRole() { return role; } public void setRole(int role) { this.role = role; } @Override public String toString() { return "Admin [id=" + id + ", name=" + name + ", pass=" + pass + ", role=" + role + ", sex=" + sex + "]"; } }
增删改查操作,ZSGC.java
package com.hbsi.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import com.hbsi.domain.Admin; import com.hbsi.util.DBManager; public class ZSGC { @Test public void insert(){ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql ="insert into admin(id,name,pass,sex,role)values(?,?,?,?,?)"; Object[] params ={13,"Jarry","123","男",1}; try { runner.update(sql,params); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void update() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql ="update admin set name=?,pass=? where id=?"; Object[] params ={"xxx","xxxx",13}; runner.update(sql,params); } @Test public void delete() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql ="delete from admin where id=?"; runner.update(sql,13); } @Test public void find() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql ="select id,name,pass from admin where id=?"; Admin admin; admin = (Admin)runner.query(sql, 5,new BeanHandler(Admin.class)); System.out.println(admin.getId()+"--"+admin.getName()+"--"+admin.getPass()+"--"+admin.getSex()+"--"+admin.getRole()); } @Test public void getAll() throws SQLException { QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin "; List<Admin> list = (List<Admin>) runner.query(sql, new BeanListHandler(Admin.class)); System.out.println(list.size()); for(Admin admin:list){ System.out.println(admin.getId()+"--"+admin.getName()+"---"+admin.getPass()+"---"+admin.getSex()+"---"+admin.getRole()); } } }
原表数据:
insert方法实现,表数据变化
update方法实现,表数据变化
delete方法实现,表数据变化
select方法实现,表数据查询显示结果
selectAll方法实现,表数据查询显示结果
package com.hbsi.dao; import java.sql.SQLException; import java.util.Arrays; import java.util.List; import java.util.Map; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.KeyedHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.junit.Test; import com.hbsi.util.DBManager; public class HandlerDemo { @Test public void testArrayHandler() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin where id=?"; Object[] params ={2}; Object[] result = runner.query(sql, params, new ArrayHandler()); System.out.println(Arrays.asList(result)); } @Test public void testArrayListHandler() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin "; List<Object[]> list =(List<Object[]>) runner.query(sql, new ArrayListHandler()); System.out.println(list.size()); } @Test public void testColumnListHandler() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin "; List list = (List)runner.query(sql, new ColumnListHandler("name")); System.out.println(list); } @Test public void testKeyedHandler() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin "; Map<Object, Map<String, Object>> map = runner.query(sql,new KeyedHandler("id")); for(Map.Entry<Object,Map<String,Object>> me :map.entrySet()){ Map<String,Object> innerme =me.getValue(); for(Map.Entry<String,Object> entry :innerme.entrySet()){ System.out.println( entry.getKey()+"="+entry.getValue()); } } } @Test public void testMapListHandler() throws SQLException{ QueryRunner runner = new QueryRunner(DBManager.getDataSource()); String sql = "select * from admin "; List list = runner.query(sql, new MapListHandler()); System.out.println(list); } }