Search in sources :

Example 86 with Statement

use of java.sql.Statement in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testCasewhen.

private void testCasewhen(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT)");
    stat.execute("INSERT INTO TEST VALUES(1),(2),(3)");
    PreparedStatement prep;
    ResultSet rs;
    prep = conn.prepareStatement("EXPLAIN SELECT COUNT(*) FROM TEST " + "WHERE CASEWHEN(ID=1, ID, ID)=? GROUP BY ID");
    prep.setInt(1, 1);
    rs = prep.executeQuery();
    rs.next();
    String plan = rs.getString(1);
    trace(plan);
    rs.close();
    prep = conn.prepareStatement("EXPLAIN SELECT COUNT(*) FROM TEST " + "WHERE CASE ID WHEN 1 THEN ID WHEN 2 THEN ID " + "ELSE ID END=? GROUP BY ID");
    prep.setInt(1, 1);
    rs = prep.executeQuery();
    rs.next();
    plan = rs.getString(1);
    trace(plan);
    prep = conn.prepareStatement("SELECT COUNT(*) FROM TEST " + "WHERE CASEWHEN(ID=1, ID, ID)=? GROUP BY ID");
    prep.setInt(1, 1);
    rs = prep.executeQuery();
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertFalse(rs.next());
    prep = conn.prepareStatement("SELECT COUNT(*) FROM TEST " + "WHERE CASE ID WHEN 1 THEN ID WHEN 2 THEN ID " + "ELSE ID END=? GROUP BY ID");
    prep.setInt(1, 1);
    rs = prep.executeQuery();
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    assertFalse(rs.next());
    prep = conn.prepareStatement("SELECT * FROM TEST WHERE ? IS NULL");
    prep.setString(1, "Hello");
    rs = prep.executeQuery();
    assertFalse(rs.next());
    assertThrows(ErrorCode.UNKNOWN_DATA_TYPE_1, conn).prepareStatement("select ? from dual union select ? from dual");
    prep = conn.prepareStatement("select cast(? as varchar) " + "from dual union select ? from dual");
    assertEquals(2, prep.getParameterMetaData().getParameterCount());
    prep.setString(1, "a");
    prep.setString(2, "a");
    rs = prep.executeQuery();
    rs.next();
    assertEquals("a", rs.getString(1));
    assertEquals("a", rs.getString(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)

Example 87 with Statement

use of java.sql.Statement in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testPrepareRecompile.

private void testPrepareRecompile(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    PreparedStatement prep;
    ResultSet rs;
    prep = conn.prepareStatement("SELECT COUNT(*) " + "FROM DUAL WHERE ? IS NULL");
    prep.setString(1, null);
    prep.executeQuery();
    stat.execute("CREATE TABLE TEST(ID INT)");
    stat.execute("DROP TABLE TEST");
    prep.setString(1, null);
    prep.executeQuery();
    prep.setString(1, "X");
    rs = prep.executeQuery();
    rs.next();
    assertEquals(0, rs.getInt(1));
    stat.execute("CREATE TABLE t1 (c1 INT, c2 VARCHAR(10))");
    stat.execute("INSERT INTO t1 SELECT X, CONCAT('Test', X)  " + "FROM SYSTEM_RANGE(1, 5);");
    prep = conn.prepareStatement("SELECT c1, c2 FROM t1 WHERE c1 = ?");
    prep.setInt(1, 1);
    prep.executeQuery();
    stat.execute("CREATE TABLE t2 (x int PRIMARY KEY)");
    prep.setInt(1, 2);
    rs = prep.executeQuery();
    rs.next();
    assertEquals(2, rs.getInt(1));
    prep.setInt(1, 3);
    rs = prep.executeQuery();
    rs.next();
    assertEquals(3, rs.getInt(1));
    stat.execute("DROP TABLE t1, t2");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 88 with Statement

use of java.sql.Statement in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testColumnMetaDataWithEquals.

private void testColumnMetaDataWithEquals(Connection conn) throws SQLException {
    Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE TEST( id INT, someColumn INT )");
    PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST VALUES(?,?)");
    ps.setInt(1, 0);
    ps.setInt(2, 999);
    ps.execute();
    ps = conn.prepareStatement("SELECT * FROM TEST WHERE someColumn = ?");
    assertEquals(Types.INTEGER, ps.getParameterMetaData().getParameterType(1));
    stmt.execute("DROP TABLE TEST");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement)

Example 89 with Statement

use of java.sql.Statement in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testUUIDGeneratedKeys.

private void testUUIDGeneratedKeys(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST_UUID(id UUID DEFAULT " + "random_UUID() PRIMARY KEY)");
    stat.execute("INSERT INTO TEST_UUID() VALUES()");
    ResultSet rs = stat.getGeneratedKeys();
    rs.next();
    byte[] data = rs.getBytes(1);
    assertEquals(16, data.length);
    stat.execute("INSERT INTO TEST_UUID VALUES(random_UUID())");
    rs = stat.getGeneratedKeys();
    assertFalse(rs.next());
    stat.execute("DROP TABLE TEST_UUID");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 90 with Statement

use of java.sql.Statement in project jdbc-shards by wplatform.

the class ManyJdbcObjectsTestCase method testOneConnectionPrepare.

private void testOneConnectionPrepare() throws SQLException {
    Connection conn = getConnection();
    PreparedStatement prep;
    Statement stat;
    int size = 1000;
    for (int i = 0; i < size; i++) {
        conn.getMetaData();
    }
    for (int i = 0; i < size; i++) {
        conn.createStatement();
    }
    stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)");
    stat.execute("INSERT INTO TEST VALUES(1, 'Hello')");
    for (int i = 0; i < size; i++) {
        stat.executeQuery("SELECT * FROM TEST WHERE 1=0");
    }
    for (int i = 0; i < size; i++) {
        stat.executeQuery("SELECT * FROM TEST");
    }
    for (int i = 0; i < size; i++) {
        conn.prepareStatement("SELECT * FROM TEST");
    }
    prep = conn.prepareStatement("SELECT * FROM TEST WHERE 1=0");
    for (int i = 0; i < size; i++) {
        prep.executeQuery();
    }
    prep = conn.prepareStatement("SELECT * FROM TEST");
    for (int i = 0; i < size; i++) {
        prep.executeQuery();
    }
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) 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