use of java.sql.Statement in project jdbc-shards by wplatform.
the class MetaDataTestCase method testUnwrap.
private void testUnwrap() throws SQLException {
Connection conn = getConnection();
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select 1 as x from dual");
ResultSetMetaData meta = rs.getMetaData();
assertTrue(meta.isWrapperFor(Object.class));
assertTrue(meta.isWrapperFor(ResultSetMetaData.class));
assertTrue(meta.isWrapperFor(meta.getClass()));
assertTrue(meta == meta.unwrap(Object.class));
assertTrue(meta == meta.unwrap(ResultSetMetaData.class));
assertTrue(meta == meta.unwrap(meta.getClass()));
assertFalse(meta.isWrapperFor(Integer.class));
assertThrows(ErrorCode.INVALID_VALUE_2, meta).unwrap(Integer.class);
conn.close();
}
use of java.sql.Statement in project jdbc-shards by wplatform.
the class PreparedStatementTestCase method testBlob.
private void testBlob(Connection conn) throws SQLException {
trace("testBlob");
Statement stat = conn.createStatement();
PreparedStatement prep;
ResultSet rs;
stat.execute("CREATE TABLE T_BLOB(ID INT PRIMARY KEY,V1 BLOB,V2 BLOB)");
trace("table created");
prep = conn.prepareStatement("INSERT INTO T_BLOB VALUES(?,?,?)");
prep.setInt(1, 1);
prep.setBytes(2, null);
prep.setNull(3, Types.BINARY);
prep.executeUpdate();
prep.setInt(1, 2);
prep.setBinaryStream(2, null, 0);
prep.setNull(3, Types.BLOB);
prep.executeUpdate();
int length = LOB_SIZE;
byte[] big1 = new byte[length];
byte[] big2 = new byte[length];
for (int i = 0; i < big1.length; i++) {
big1[i] = (byte) ((i * 11) % 254);
big2[i] = (byte) ((i * 17) % 251);
}
prep.setInt(1, 3);
prep.setBytes(2, big1);
prep.setBytes(3, big2);
prep.executeUpdate();
prep.setInt(1, 4);
ByteArrayInputStream buffer;
buffer = new ByteArrayInputStream(big2);
prep.setBinaryStream(2, buffer, big2.length);
buffer = new ByteArrayInputStream(big1);
prep.setBinaryStream(3, buffer, big1.length);
prep.executeUpdate();
try {
buffer.close();
trace("buffer not closed");
} catch (IOException e) {
trace("buffer closed");
}
prep.setInt(1, 5);
buffer = new ByteArrayInputStream(big2);
prep.setObject(2, buffer, Types.BLOB, 0);
buffer = new ByteArrayInputStream(big1);
prep.setObject(3, buffer);
prep.executeUpdate();
rs = stat.executeQuery("SELECT ID, V1, V2 FROM T_BLOB ORDER BY ID");
rs.next();
assertEquals(1, rs.getInt(1));
assertTrue(rs.getBytes(2) == null && rs.wasNull());
assertTrue(rs.getBytes(3) == null && rs.wasNull());
rs.next();
assertEquals(2, rs.getInt(1));
assertTrue(rs.getBytes(2) == null && rs.wasNull());
assertTrue(rs.getBytes(3) == null && rs.wasNull());
rs.next();
assertEquals(3, rs.getInt(1));
assertEquals(big1, rs.getBytes(2));
assertEquals(big2, rs.getBytes(3));
rs.next();
assertEquals(4, rs.getInt(1));
assertEquals(big2, rs.getBytes(2));
assertEquals(big1, rs.getBytes(3));
rs.next();
assertEquals(5, rs.getInt(1));
assertEquals(big2, rs.getBytes(2));
assertEquals(big1, rs.getBytes(3));
assertFalse(rs.next());
}
use of java.sql.Statement in project jdbc-shards by wplatform.
the class PreparedStatementTestCase method testPrepareExecute.
private void testPrepareExecute(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("prepare test(int, int) as select ?1*?2");
ResultSet rs = stat.executeQuery("execute test(3, 2)");
rs.next();
assertEquals(6, rs.getInt(1));
stat.execute("deallocate test");
}
use of java.sql.Statement in project jdbc-shards by wplatform.
the class PreparedStatementTestCase method testGetMoreResults.
private void testGetMoreResults(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
PreparedStatement prep;
ResultSet rs;
stat.execute("CREATE TABLE TEST(ID INT)");
stat.execute("INSERT INTO TEST VALUES(1)");
prep = conn.prepareStatement("SELECT * FROM TEST");
// just to check if it doesn't throw an exception - it may be null
prep.getMetaData();
assertTrue(prep.execute());
rs = prep.getResultSet();
assertFalse(prep.getMoreResults());
assertEquals(-1, prep.getUpdateCount());
// supposed to be closed now
assertThrows(ErrorCode.OBJECT_CLOSED, rs).next();
assertEquals(-1, prep.getUpdateCount());
prep = conn.prepareStatement("UPDATE TEST SET ID = 2");
assertFalse(prep.execute());
assertEquals(1, prep.getUpdateCount());
assertFalse(prep.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
assertEquals(-1, prep.getUpdateCount());
// supposed to be closed now
assertThrows(ErrorCode.OBJECT_CLOSED, rs).next();
assertEquals(-1, prep.getUpdateCount());
prep = conn.prepareStatement("DELETE FROM TEST");
prep.executeUpdate();
assertFalse(prep.getMoreResults());
assertEquals(-1, prep.getUpdateCount());
stat.execute("DROP TABLE TEST");
}
use of java.sql.Statement in project jdbc-shards by wplatform.
the class PreparedStatementTestCase method testIdentity.
private void testIdentity(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
stat.execute("CREATE SEQUENCE SEQ");
stat.execute("CREATE TABLE TEST(ID INT)");
PreparedStatement prep;
prep = conn.prepareStatement("INSERT INTO TEST VALUES(NEXT VALUE FOR SEQ)");
prep.execute();
ResultSet rs = prep.getGeneratedKeys();
rs.next();
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
prep = conn.prepareStatement("INSERT INTO TEST VALUES(NEXT VALUE FOR SEQ)", Statement.RETURN_GENERATED_KEYS);
prep.execute();
rs = prep.getGeneratedKeys();
rs.next();
assertEquals(2, rs.getInt(1));
assertFalse(rs.next());
prep = conn.prepareStatement("INSERT INTO TEST VALUES(NEXT VALUE FOR SEQ)", new int[] { 1 });
prep.execute();
rs = prep.getGeneratedKeys();
rs.next();
assertEquals(3, rs.getInt(1));
assertFalse(rs.next());
prep = conn.prepareStatement("INSERT INTO TEST VALUES(NEXT VALUE FOR SEQ)", new String[] { "ID" });
prep.execute();
rs = prep.getGeneratedKeys();
rs.next();
assertEquals(4, rs.getInt(1));
assertFalse(rs.next());
prep = conn.prepareStatement("INSERT INTO TEST VALUES(NEXT VALUE FOR SEQ)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
prep.execute();
rs = prep.getGeneratedKeys();
rs.next();
assertEquals(5, rs.getInt(1));
assertFalse(rs.next());
stat.execute("DROP TABLE TEST");
}
Aggregations