dbutils简单测试案例

两个文件

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);	
	}
	
}


原文链接: dbutils简单测试案例 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-78.html )

发表评论

0则评论给“dbutils简单测试案例”