Search in sources :

Example 6 with JdbcStatement

use of org.h2.jdbc.JdbcStatement in project h2database by h2database.

the class JdbcConnection method createStatement.

/**
 * Creates a new statement.
 *
 * @return the new statement
 * @throws SQLException if the connection is closed
 */
@Override
public Statement createStatement() throws SQLException {
    try {
        int id = getNextId(TraceObject.STATEMENT);
        if (isDebugEnabled()) {
            debugCodeAssign("Statement", TraceObject.STATEMENT, id, "createStatement()");
        }
        checkClosed();
        return new JdbcStatement(this, id, ResultSet.TYPE_FORWARD_ONLY, Constants.DEFAULT_RESULT_SET_CONCURRENCY, false);
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 7 with JdbcStatement

use of org.h2.jdbc.JdbcStatement in project h2database by h2database.

the class TestGetGeneratedKeys method testStatementExecuteLargeUpdate_int.

/**
 * Test method for {@link Statement#executeLargeUpdate(String, int)}.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testStatementExecuteLargeUpdate_int(Connection conn) throws Exception {
    JdbcStatement stat = (JdbcStatement) conn.createStatement();
    stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL, OTHER INT DEFAULT 0)");
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (10)", Statement.NO_GENERATED_KEYS);
    ResultSet rs = stat.getGeneratedKeys();
    assertFalse(rs.next());
    rs.close();
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (20)", Statement.RETURN_GENERATED_KEYS);
    rs = stat.getGeneratedKeys();
    assertEquals(2, rs.getMetaData().getColumnCount());
    assertEquals("ID", rs.getMetaData().getColumnName(1));
    assertEquals("UID", rs.getMetaData().getColumnName(2));
    assertTrue(rs.next());
    assertEquals(2L, rs.getLong(1));
    assertEquals(UUID.class, rs.getObject(2).getClass());
    assertFalse(rs.next());
    rs.close();
    stat.execute("DROP TABLE TEST");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) ResultSet(java.sql.ResultSet)

Example 8 with JdbcStatement

use of org.h2.jdbc.JdbcStatement in project h2database by h2database.

the class TestGetGeneratedKeys method testStatementExecuteLargeUpdate.

/**
 * Test method for {@link Statement#executeLargeUpdate(String)}.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testStatementExecuteLargeUpdate(Connection conn) throws Exception {
    JdbcStatement stat = (JdbcStatement) conn.createStatement();
    stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL)");
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (10)");
    ResultSet rs = stat.getGeneratedKeys();
    assertFalse(rs.next());
    rs.close();
    stat.execute("DROP TABLE TEST");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) ResultSet(java.sql.ResultSet)

Example 9 with JdbcStatement

use of org.h2.jdbc.JdbcStatement in project h2database by h2database.

the class TestGetGeneratedKeys method testStatementExecuteLargeUpdate_intArray.

/**
 * Test method for {@link Statement#executeLargeUpdate(String, int[])}.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testStatementExecuteLargeUpdate_intArray(Connection conn) throws Exception {
    JdbcStatement stat = (JdbcStatement) conn.createStatement();
    stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL)");
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (10)", new int[0]);
    ResultSet rs = stat.getGeneratedKeys();
    assertFalse(rs.next());
    rs.close();
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (20)", new int[] { 1, 2 });
    rs = stat.getGeneratedKeys();
    assertEquals(2, rs.getMetaData().getColumnCount());
    assertEquals("ID", rs.getMetaData().getColumnName(1));
    assertEquals("UID", rs.getMetaData().getColumnName(2));
    assertTrue(rs.next());
    assertEquals(2L, rs.getLong(1));
    assertEquals(UUID.class, rs.getObject(2).getClass());
    assertFalse(rs.next());
    rs.close();
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (30)", new int[] { 2, 1 });
    rs = stat.getGeneratedKeys();
    assertEquals(2, rs.getMetaData().getColumnCount());
    assertEquals("UID", rs.getMetaData().getColumnName(1));
    assertEquals("ID", rs.getMetaData().getColumnName(2));
    assertTrue(rs.next());
    assertEquals(UUID.class, rs.getObject(1).getClass());
    assertEquals(3L, rs.getLong(2));
    assertFalse(rs.next());
    rs.close();
    stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (40)", new int[] { 2 });
    rs = stat.getGeneratedKeys();
    assertEquals(1, rs.getMetaData().getColumnCount());
    assertEquals("UID", rs.getMetaData().getColumnName(1));
    assertTrue(rs.next());
    assertEquals(UUID.class, rs.getObject(1).getClass());
    assertFalse(rs.next());
    rs.close();
    stat.execute("DROP TABLE TEST");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) ResultSet(java.sql.ResultSet)

Aggregations

ResultSet (java.sql.ResultSet)6 JdbcStatement (org.h2.jdbc.JdbcStatement)6 SQLException (java.sql.SQLException)3 Savepoint (java.sql.Savepoint)3 DbException (org.h2.message.DbException)3 PreparedStatement (java.sql.PreparedStatement)2 SQLClientInfoException (java.sql.SQLClientInfoException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ParameterMetaData (java.sql.ParameterMetaData)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 Statement (java.sql.Statement)1 Properties (java.util.Properties)1 ConnectionInfo (org.h2.engine.ConnectionInfo)1 SysProperties (org.h2.engine.SysProperties)1 JdbcConnection (org.h2.jdbc.JdbcConnection)1 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)1