Search in sources :

Example 71 with Statement

use of java.sql.Statement in project flyway by flyway.

the class JdbcTemplate method executeStatement.

/**
     * Executes this sql statement using an ordinary Statement.
     *
     * @param sql The statement to execute.
     * @throws SQLException when the execution failed.
     */
public void executeStatement(String sql) throws SQLException {
    Statement statement = null;
    try {
        statement = connection.createStatement();
        statement.setEscapeProcessing(false);
        boolean hasResults = false;
        try {
            hasResults = statement.execute(sql);
        } finally {
            @SuppressWarnings("ThrowableResultOfMethodCallIgnored") SQLWarning warning = statement.getWarnings();
            while (warning != null) {
                if ("00000".equals(warning.getSQLState())) {
                    LOG.info("DB: " + warning.getMessage());
                } else {
                    LOG.warn("DB: " + warning.getMessage() + " (SQL State: " + warning.getSQLState() + " - Error Code: " + warning.getErrorCode() + ")");
                }
                warning = warning.getNextWarning();
            }
            // retrieve all results to ensure all errors are detected
            int updateCount = -1;
            while (hasResults || (updateCount = statement.getUpdateCount()) != -1) {
                if (updateCount != -1) {
                    LOG.debug("Update Count: " + updateCount);
                }
                hasResults = statement.getMoreResults();
            }
        }
    } finally {
        JdbcUtils.closeStatement(statement);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement)

Example 72 with Statement

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

the class StatementTestCase method testIdentityMerge.

private void testIdentityMerge() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("drop table if exists test1");
    stat.execute("create table test1(id identity, x int)");
    stat.execute("drop table if exists test2");
    stat.execute("create table test2(id identity, x int)");
    stat.execute("merge into test1(x) key(x) values(5)");
    ResultSet keys;
    keys = stat.getGeneratedKeys();
    keys.next();
    assertEquals(1, keys.getInt(1));
    stat.execute("insert into test2(x) values(10), (11), (12)");
    stat.execute("merge into test1(x) key(x) values(5)");
    keys = stat.getGeneratedKeys();
    assertFalse(keys.next());
    stat.execute("merge into test1(x) key(x) values(6)");
    keys = stat.getGeneratedKeys();
    keys.next();
    assertEquals(2, keys.getInt(1));
    stat.execute("drop table test1, test2");
}
Also used : JdbcStatement(com.wplatform.ddal.jdbc.JdbcStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet)

Example 73 with Statement

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

the class StatementTestCase method testUnwrap.

private void testUnwrap() throws SQLException {
    Statement stat = conn.createStatement();
    assertTrue(stat.isWrapperFor(Object.class));
    assertTrue(stat.isWrapperFor(Statement.class));
    assertTrue(stat.isWrapperFor(stat.getClass()));
    assertFalse(stat.isWrapperFor(Integer.class));
    assertTrue(stat == stat.unwrap(Object.class));
    assertTrue(stat == stat.unwrap(Statement.class));
    assertTrue(stat == stat.unwrap(stat.getClass()));
    assertThrows(ErrorCode.INVALID_VALUE_2, stat).unwrap(Integer.class);
}
Also used : JdbcStatement(com.wplatform.ddal.jdbc.JdbcStatement) Statement(java.sql.Statement)

Example 74 with Statement

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

the class UpdatableResultSetTestCase method testDetectUpdatable.

private void testDetectUpdatable() throws SQLException {
    Connection conn = getConnection();
    Statement stat;
    ResultSet rs;
    stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    stat.execute("create table test(id int primary key, name varchar)");
    rs = stat.executeQuery("select * from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    stat.execute("drop table test");
    stat.execute("create table test(a int, b int, " + "name varchar, primary key(a, b))");
    rs = stat.executeQuery("select * from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select a, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select b, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select b, name, a from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    stat.execute("drop table test");
    stat.execute("create table test(a int, b int, name varchar)");
    stat.execute("create unique index on test(b, a)");
    rs = stat.executeQuery("select * from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select a, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select b, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select b, name, a from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    stat.execute("drop table test");
    stat.execute("create table test(a int, b int, c int unique, " + "name varchar, primary key(a, b))");
    rs = stat.executeQuery("select * from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select a, name, c from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select b, a, name, c from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    stat.execute("drop table test");
    stat.execute("create table test(id int primary key, " + "a int, b int, i int, j int, k int, name varchar)");
    stat.execute("create unique index on test(b, a)");
    stat.execute("create unique index on test(i, j)");
    stat.execute("create unique index on test(a, j)");
    rs = stat.executeQuery("select * from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select a, name, b from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select a, name, b from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    rs = stat.executeQuery("select i, b, k, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select a, i, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select b, i, k, name from test");
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    rs = stat.executeQuery("select a, k, j, name from test");
    assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
    stat.execute("drop table test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 75 with Statement

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

the class UpdatableResultSetTestCase method testUpdateDeleteInsert.

private void testUpdateDeleteInsert() throws SQLException {
    Connection c1 = getConnection();
    Connection c2 = getConnection();
    Statement stat = c1.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    stat.execute("DROP TABLE IF EXISTS TEST");
    stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
    int max = 8;
    for (int i = 0; i < max; i++) {
        stat.execute("INSERT INTO TEST VALUES(" + i + ", 'Hello" + i + "')");
    }
    ResultSet rs;
    rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
    rs.next();
    assertEquals(0, rs.getInt(1));
    rs.moveToInsertRow();
    rs.updateInt(1, 100);
    rs.moveToCurrentRow();
    assertEquals(0, rs.getInt(1));
    rs = stat.executeQuery("SELECT * FROM TEST");
    int j = max;
    while (rs.next()) {
        int id = rs.getInt(1);
        if (id % 2 == 0) {
            Statement s2 = c2.createStatement();
            s2.execute("UPDATE TEST SET NAME = NAME || '+' WHERE ID = " + rs.getInt(1));
            if (id % 4 == 0) {
                rs.refreshRow();
            }
            rs.updateString(2, "Updated " + rs.getString(2));
            rs.updateRow();
        } else {
            rs.deleteRow();
        }
        // the driver does not detect it in any case
        assertFalse(rs.rowUpdated());
        assertFalse(rs.rowInserted());
        assertFalse(rs.rowDeleted());
        rs.moveToInsertRow();
        rs.updateString(2, "Inserted " + j);
        rs.updateInt(1, j);
        j += 2;
        rs.insertRow();
        // the driver does not detect it in any case
        assertFalse(rs.rowUpdated());
        assertFalse(rs.rowInserted());
        assertFalse(rs.rowDeleted());
    }
    rs = stat.executeQuery("SELECT * FROM TEST ORDER BY ID");
    while (rs.next()) {
        int id = rs.getInt(1);
        String name = rs.getString(2);
        assertEquals(0, id % 2);
        if (id >= max) {
            assertEquals("Inserted " + id, rs.getString(2));
        } else {
            if (id % 4 == 0) {
                assertEquals("Updated Hello" + id + "+", rs.getString(2));
            } else {
                assertEquals("Updated Hello" + id, rs.getString(2));
            }
        }
        trace("id=" + id + " name=" + name);
    }
    c2.close();
    c1.close();
    // test scrollable result sets
    Connection conn = getConnection();
    for (int i = 0; i < 5; i++) {
        testScrollable(conn, i);
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

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