Search in sources :

Example 81 with Statement

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();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 82 with Statement

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 83 with Statement

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");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 84 with Statement

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");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 85 with Statement

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");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Statement (java.sql.Statement)3054 Connection (java.sql.Connection)1634 ResultSet (java.sql.ResultSet)1631 SQLException (java.sql.SQLException)1529 PreparedStatement (java.sql.PreparedStatement)1329 Test (org.junit.Test)570 ArrayList (java.util.ArrayList)323 CallableStatement (java.sql.CallableStatement)135 ResultSetMetaData (java.sql.ResultSetMetaData)127 IOException (java.io.IOException)121 Properties (java.util.Properties)114 HashMap (java.util.HashMap)83 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)81 DruidPooledStatement (com.alibaba.druid.pool.DruidPooledStatement)71 StringPlus (mom.trd.opentheso.bdd.tools.StringPlus)63 DataSource (javax.sql.DataSource)62 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)61 Vector (java.util.Vector)61 DatabaseMetaData (java.sql.DatabaseMetaData)53 KeyValue (org.vcell.util.document.KeyValue)49