@Test public void set() throws SQLException { ResultSet rs; PreparedStatement prep = conn.prepareStatement("select ?, ?, ?;"); // integers prep.setInt(1, Integer.MIN_VALUE); prep.setInt(2, Integer.MAX_VALUE); prep.setInt(3, 0); rs = prep.executeQuery(); assertTrue(rs.next()); assertEquals(Integer.MIN_VALUE, rs.getInt(1)); assertEquals(Integer.MAX_VALUE, rs.getInt(2)); assertEquals(0, rs.getInt(3)); // strings String name = "Winston Leonard Churchill"; String fn = name.substring(0, 7), mn = name.substring(8, 15), sn = name.substring(16, 25); prep.clearParameters(); prep.setString(1, fn); prep.setString(2, mn); prep.setString(3, sn); prep.executeQuery(); assertTrue(rs.next()); assertEquals(fn, rs.getString(1)); assertEquals(mn, rs.getString(2)); assertEquals(sn, rs.getString(3)); // mixed prep.setString(1, name); prep.setString(2, null); prep.setLong(3, Long.MAX_VALUE); prep.executeQuery(); assertTrue(rs.next()); assertEquals(name, rs.getString(1)); assertNull(rs.getString(2)); assertTrue(rs.wasNull()); assertEquals(Long.MAX_VALUE, rs.getLong(3)); // bytes prep.setBytes(1, b1); prep.setBytes(2, b2); prep.setBytes(3, b3); prep.executeQuery(); assertTrue(rs.next()); assertArrayEq(rs.getBytes(1), b1); assertArrayEq(rs.getBytes(2), b2); assertArrayEq(rs.getBytes(3), b3); assertFalse(rs.next()); rs.close(); }
@Test public void colNameAccess() throws SQLException { PreparedStatement prep = conn.prepareStatement("select ? as col1, ? as col2, ? as bingo;"); prep.setNull(1, 0); prep.setFloat(2, Float.MIN_VALUE); prep.setShort(3, Short.MIN_VALUE); prep.executeQuery(); ResultSet rs = prep.executeQuery(); assertTrue(rs.next()); assertNull(rs.getString("col1")); assertTrue(rs.wasNull()); assertEquals(Float.MIN_VALUE, rs.getFloat("col2")); assertEquals(Short.MIN_VALUE, rs.getShort("bingo")); rs.close(); prep.close(); }