@Ignore @Test public void getObject() throws SQLException { stat.executeUpdate( "create table testobj (" + "c1 integer, c2 float, c3, c4 varchar, c5 bit, c6, c7);"); PreparedStatement prep = conn.prepareStatement("insert into testobj values (?,?,?,?,?,?,?);"); prep.setInt(1, Integer.MAX_VALUE); prep.setFloat(2, Float.MAX_VALUE); prep.setDouble(3, Double.MAX_VALUE); prep.setLong(4, Long.MAX_VALUE); prep.setBoolean(5, false); prep.setByte(6, (byte) 7); prep.setBytes(7, b1); prep.executeUpdate(); ResultSet rs = stat.executeQuery("select c1,c2,c3,c4,c5,c6,c7 from testobj;"); assertTrue(rs.next()); assertEquals(Integer.MAX_VALUE, rs.getInt(1)); assertEquals(Integer.MAX_VALUE, (int) rs.getLong(1)); assertEquals(Float.MAX_VALUE, rs.getFloat(2)); assertEquals(Double.MAX_VALUE, rs.getDouble(3)); assertEquals(Long.MAX_VALUE, rs.getLong(4)); assertFalse(rs.getBoolean(5)); assertEquals((byte) 7, rs.getByte(6)); assertArrayEq(rs.getBytes(7), b1); assertNotNull(rs.getObject(1)); assertNotNull(rs.getObject(2)); assertNotNull(rs.getObject(3)); assertNotNull(rs.getObject(4)); assertNotNull(rs.getObject(5)); assertNotNull(rs.getObject(6)); assertNotNull(rs.getObject(7)); assertTrue(rs.getObject(1) instanceof Integer); assertTrue(rs.getObject(2) instanceof Double); assertTrue(rs.getObject(3) instanceof Double); assertTrue(rs.getObject(4) instanceof String); assertTrue(rs.getObject(5) instanceof Integer); assertTrue(rs.getObject(6) instanceof Integer); assertTrue(rs.getObject(7) instanceof byte[]); rs.close(); }
@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 date2() throws SQLException { Date d1 = new Date(1092941466000L); stat.execute("create table t (c1);"); PreparedStatement prep = conn.prepareStatement("insert into t values (datetime(?/1000, 'unixepoch'));"); prep.setDate(1, d1); prep.executeUpdate(); ResultSet rs = stat.executeQuery("select strftime('%s', c1) * 1000 from t;"); assertTrue(rs.next()); assertEquals(d1.getTime(), rs.getLong(1)); assertTrue(rs.getDate(1).equals(d1)); }
@Test public void date1() throws SQLException { Date d1 = new Date(987654321); stat.execute("create table t (c1);"); PreparedStatement prep = conn.prepareStatement("insert into t values(?);"); prep.setDate(1, d1); prep.executeUpdate(); prep.setDate(1, null); prep.executeUpdate(); ResultSet rs = stat.executeQuery("select c1 from t;"); assertTrue(rs.next()); assertEquals(d1.getTime(), rs.getLong(1)); assertTrue(rs.getDate(1).equals(d1)); assertTrue(rs.next()); assertEquals(null, rs.getDate(1)); rs.close(); }