@Test public void testExecuteUpdate() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("update xxxx set name = 'newname'"); int affect = stat.executeUpdate(); Assert.assertEquals(affect, 1); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testExecuteQuery() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("select * from xxx where id=?"); stat.setByte(1, (byte) 5); MockDataSource.addPreData("id:1,name:2"); ResultSet result = stat.executeQuery(); Assert.assertEquals(result.next(), true); Assert.assertEquals(result.getLong(1), 1L); Assert.assertEquals(result.getLong(2), 2L); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
/** 同一个PreparedStatement先查询后更新 */ @Test public void testExecute2() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("select * from xxx where id=?"); stat.setByte(1, (byte) 5); boolean res = stat.execute(); Assert.assertEquals(res, true); res = stat.execute("update t set name = 'newName'"); Assert.assertEquals(res, false); MockDataSource.showTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testAddBatchSql() throws SQLException { TGroupConnection conn = null; Statement stat = null; try { conn = tgds.getConnection(); stat = conn.createStatement(); stat.addBatch("update t set name = 'newName' "); stat.addBatch("update t set type = 2 "); int[] affectedRow = stat.executeBatch(); System.out.println(Arrays.toString(affectedRow)); MockDataSource.showTrace(); Assert.assertTrue(MockDataSource.hasMethod("db", "db1", "executeBatch")); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void testAddBatch() throws SQLException { TGroupConnection conn = null; PreparedStatement stat = null; try { conn = tgds.getConnection(); stat = conn.prepareStatement("update test set type=? where id = ?"); stat.setInt(1, 1); stat.setString(2, "2askjfoue33"); stat.addBatch(); stat.setInt(1, 2); stat.setString(2, "retrtorut48"); stat.addBatch(); int[] affectedRow = stat.executeBatch(); System.out.println(Arrays.toString(affectedRow)); MockDataSource.showTrace(); Assert.assertTrue(MockDataSource.hasMethod("db", "db1", "executeBatch")); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stat != null) { try { stat.close(); } catch (SQLException e) { } } } } }
@Test public void java_sql_Statement_api_support() throws Exception { TGroupConnection conn = null; Statement stmt = null; try { conn = tgds.getConnection(); stmt = conn.createStatement(); String insertSQL = "insert into crud(f1,f2) values(10,'str')"; String updateSQL = "update crud set f2='str2'"; String selectSQL = "select * from crud"; String showSQL = "show create table crud"; assertFalse(stmt.execute(insertSQL)); assertTrue(stmt.execute(selectSQL)); assertTrue(stmt.execute(showSQL)); assertFalse(stmt.execute(insertSQL, Statement.RETURN_GENERATED_KEYS)); assertFalse(stmt.execute(insertSQL, new int[] {1})); assertFalse(stmt.execute(insertSQL, new String[] {"col"})); stmt.addBatch(insertSQL); stmt.addBatch(updateSQL); int[] updateCounts = stmt.executeBatch(); assertEquals(updateCounts.length, 2); MockDataSource.addPreData("id:1,name:2"); assertTrue(stmt.executeQuery(selectSQL).next()); assertEquals(stmt.executeUpdate(insertSQL), 1); assertEquals(stmt.executeUpdate(insertSQL, Statement.RETURN_GENERATED_KEYS), 1); assertEquals(stmt.executeUpdate(insertSQL, new int[] {1}), 1); assertEquals(stmt.executeUpdate(insertSQL, new String[] {"col"}), 1); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { } } } } }