两个文件
1\ JdbcUtils.java 的源代码
package com.gyarmy.dbutils; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import com.gyarmy.domain.*; import com.gyramy.utils.JdbcUtils; public class TestDBUtils { @Test public void test2() throws Exception{ //查询 QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); List<Acount> list = runner.query("select * from account", new BeanListHandler<Acount>(Acount.class)); //输出数据 for (Acount acount : list) { System.out.print("id="+acount.getId()+",name="+acount.getName()+",money="+acount.getMoney()); System.out.println(); } } @Test public void test1() throws Exception{ //修改 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); //qr.update("insert into account values(?,?,?)", new Object[]{21,"ee",3215005}); qr.update("insert into account values(?,?,?)", null,"kiki",6454); } }
2\dbutils 框架的测试使用
package com.gyarmy.dbutils; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import com.gyarmy.domain.*; import com.gyramy.utils.JdbcUtils; public class TestDBUtils { @Test public void test2() throws Exception{ //查询 QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); List<Acount> list = runner.query("select * from account", new BeanListHandler<Acount>(Acount.class)); //输出数据 for (Acount acount : list) { System.out.print("id="+acount.getId()+",name="+acount.getName()+",money="+acount.getMoney()); System.out.println(); } } @Test public void test1() throws Exception{ //修改 QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); //qr.update("insert into account values(?,?,?)", new Object[]{21,"ee",3215005}); qr.update("insert into account values(?,?,?)", null,"kiki",6454); } }
0则评论给“dbutils简单测试案例”