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);
}
}
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");
}
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);
}
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();
}
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();
}
Aggregations