首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

JDBC公共操作方法(7):测试

2012-09-01 
JDBC公共操作方法(七):测试?使用junit 4 测试。?TestJDBCCore??import java.sql.Typesimport org.junit.Te

JDBC公共操作方法(七):测试

?

使用junit 4 测试。

?

TestJDBCCore

?

?

import java.sql.Types;import org.junit.Test;public class TestJDBCCore {@Testpublic void queryById() {String sql = "select empno,ename from emp where empno=?";String[] params = new String[] { "7369" };String[][] result = JDBCCore.getInstance().query(sql, params);if (null != result) {String empno = result[0][0];String ename = result[0][1];System.out.println(empno + "\t" + ename);}}@Testpublic void queryAll() {String sql = "select empno,ename from emp order by empno";String[][] result = JDBCCore.getInstance().query(sql, null);if (null != result) {String empno = null, ename = null;for (int i = 0; i < result.length; i++) {empno = result[i][0];ename = result[i][1];System.out.print(empno + "\t" + ename + "\n");}}}@Testpublic void queryByName() {String sql = "select empno,ename from emp where ename=?";String[] params = new String[] { "test_add" };String[][] result = JDBCCore.getInstance().query(sql, params);if (null != result) {int resultLength = result.length;if (resultLength == 1) {String empno = result[0][0];String ename = result[0][1];System.out.println(empno + "\t" + ename);} else if (resultLength > 1) {String empno = null, ename = null;for (int i = 0; i < result.length; i++) {empno = result[i][0];ename = result[i][1];System.out.print(empno + "\t" + ename + "\n");}}}}@Testpublic void insert() {String sql = "insert into emp(empno,ename) values(?,?)";String[] params = new String[] { "2", "emp_2" };int rows = JDBCCore.getInstance().operate(sql, params);System.out.println(rows);}@Testpublic void update() {String sql = "update emp set ename=? where empno=?";String[] params = new String[] { "2_emp", "2" };int rows = JDBCCore.getInstance().operate(sql, params);System.out.println(rows);}@Testpublic void delete() {String sql = "delete from emp where empno=?";String[] params = new String[] { "2" };int rows = JDBCCore.getInstance().operate(sql, params);System.out.println(rows);}@Testpublic void callProcedure() {String sql = "{call P_ADD_EMP(?,?,?,?,?,?,?,?,?)}";String[][] params = new String[][] { { "in", "1" }, { "IN", "emp_1" },{ "IN", null }, { "IN", null }, { "IN", null }, { "IN", null },{ "IN", null }, { "IN", "30" },{ "out", String.valueOf(Types.INTEGER) } };String[] result = JDBCCore.getInstance().callProcedure(sql, params);if (null == result || 1 > result.length) {System.out.println("call Procedure faile.");return;}int res = -1;try {res = Integer.valueOf(result[0]);} catch (NumberFormatException e) {res = -11;}if (-1 == res) {System.out.println("the empno you want to add can not be null");} else if (1 == res) {System.out.println("the empno you want to add has existed");} else if (2 == res) {System.out.println("the deptno you want to add does not exist");} else if (3 == res) {System.out.println("occur exception.");} else if(0 == res) {System.out.println("insert success");} else {System.out.println("insert faile");}}}

热点排行