日期:2014-05-16 浏览次数:20432 次
?
使用junit 4 测试。
?
TestJDBCCore
?
?
import java.sql.Types; import org.junit.Test; public class TestJDBCCore { @Test public 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); } } @Test public 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"); } } } @Test public 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"); } } } } @Test public 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); } @Test public 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); } @Test public 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); } @Test public 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"); } } }