Search in sources :

Example 11 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBResultSet method testExecutableProcedure.

@Test
public void testExecutableProcedure() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    LocalTransaction t = c.getLocalTransaction();
    t.begin();
    try {
        s.execute("DROP PROCEDURE testproc");
    } catch (Exception e) {
    }
    t.commit();
    t.begin();
    s.execute(CREATE_PROCEDURE);
    t.commit();
    t.begin();
    CallableStatement p = c.prepareCall("EXECUTE PROCEDURE testproc(?)");
    p.setInt(1, 5);
    assertTrue("execute returned false for execute procedure statement", p.execute());
    assertEquals("wrong answer from sp invocation", 5, p.getInt(1));
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP PROCEDURE testproc");
    s.close();
    t.commit();
    c.close();
}
Also used : LocalTransaction(javax.resource.spi.LocalTransaction) FBConnection(org.firebirdsql.jdbc.FBConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 12 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBResultSet method testUseResultSetWithPreparedStatement.

@Test
public void testUseResultSetWithPreparedStatement() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    LocalTransaction t = c.getLocalTransaction();
    Exception ex = null;
    t.begin();
    try {
        s.execute("CREATE TABLE T1 ( C1 INTEGER not null primary key, C2 SMALLINT, C3 DECIMAL(18,0), C4 FLOAT, C5 DOUBLE PRECISION, C6 CHAR(10), C7 VARCHAR(20))");
    } catch (Exception e) {
        ex = e;
    }
    t.commit();
    t.begin();
    PreparedStatement p = c.prepareStatement("insert into T1 values (?, ?, ?, ?, ?, ?, ?)");
    p.setInt(1, 1);
    p.setShort(2, (short) 1);
    p.setLong(3, 1);
    p.setFloat(4, (float) 1.0);
    p.setDouble(5, 1.0);
    p.setString(6, "one");
    p.setString(7, "one");
    assertFalse("execute returned true for insert statement", p.execute());
    p.setInt(1, 2);
    p.setShort(2, (short) 2);
    p.setLong(3, 2);
    p.setFloat(4, (float) 2.0);
    p.setDouble(5, 2.0);
    p.setString(6, "two");
    p.setString(7, "two");
    assertEquals("executeUpdate count != 1", 1, p.executeUpdate());
    p.close();
    p = c.prepareStatement("select * from T1 where C1 = ?");
    p.setInt(1, 1);
    ResultSet rs = p.executeQuery();
    while (rs.next()) {
        if (log != null)
            log.info("C1: " + rs.getInt(1) + " C2: " + rs.getShort(2) + " C3: " + rs.getLong(3) + " C4: " + rs.getFloat(4) + " C5: " + rs.getDouble(5) + " C6: " + rs.getString(6) + " C7: " + rs.getString(7));
        if (log != null)
            log.info("C1: " + rs.getInt("C1") + " C2: " + rs.getShort("C2") + " C3: " + rs.getLong("C3") + " C4: " + rs.getFloat("C4") + " C5: " + rs.getDouble("C5") + " C6: " + rs.getString("C6") + " C7: " + rs.getString("C7"));
    }
    p.setInt(1, 2);
    rs = p.executeQuery();
    while (rs.next()) {
        if (log != null)
            log.info("C1: " + rs.getInt(1) + " C2: " + rs.getShort(2) + " C3: " + rs.getLong(3) + " C4: " + rs.getFloat(4) + " C5: " + rs.getDouble(5) + " C6: " + rs.getString(6) + " C7: " + rs.getString(7));
        if (log != null)
            log.info("C1: " + rs.getInt("C1") + " C2: " + rs.getShort("C2") + " C3: " + rs.getLong("C3") + " C4: " + rs.getFloat("C4") + " C5: " + rs.getDouble("C5") + " C6: " + rs.getString("C6") + " C7: " + rs.getString("C7"));
    }
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP TABLE T1");
    s.close();
    t.commit();
    c.close();
    if (ex != null) {
        throw ex;
    }
}
Also used : LocalTransaction(javax.resource.spi.LocalTransaction) FBConnection(org.firebirdsql.jdbc.FBConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 13 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBResultSet method testUseResultSetWithCount.

@Test
public void testUseResultSetWithCount() throws Exception {
    FBManagedConnectionFactory mcf = initMcf();
    DataSource ds = (DataSource) mcf.createConnectionFactory();
    FBConnection c = (FBConnection) ds.getConnection();
    Statement s = c.createStatement();
    LocalTransaction t = c.getLocalTransaction();
    Exception ex = null;
    t.begin();
    try {
        s.execute(" CREATE TABLE Customer (name VARCHAR(256),accounts VARCHAR(2000),id VARCHAR(256))");
    } catch (Exception e) {
        ex = e;
    }
    t.commit();
    t.begin();
    PreparedStatement p = c.prepareStatement("SELECT COUNT(*) FROM Customer WHERE id=? AND name=?");
    p.setString(1, "1");
    p.setString(2, "First Customer");
    assertTrue("execute returned false for select statement", p.execute());
    ResultSet rs = p.getResultSet();
    while (rs.next()) {
        if (log != null)
            log.info("count: " + rs.getInt(1));
    }
    p.close();
    t.commit();
    t.begin();
    s.execute("DROP TABLE Customer");
    s.close();
    t.commit();
    c.close();
    if (ex != null) {
        throw ex;
    }
}
Also used : LocalTransaction(javax.resource.spi.LocalTransaction) FBConnection(org.firebirdsql.jdbc.FBConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 14 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestBackupManager method testRestorePageSize16384.

/**
 * Test if restoring a database to page size 16384 works.
 */
@Test
public void testRestorePageSize16384() throws Exception {
    usesDatabase.createDefaultDatabase();
    backupManager.backupDatabase();
    backupManager.setRestoreReplace(true);
    backupManager.setRestorePageSize(PageSizeConstants.SIZE_16K);
    backupManager.restoreDatabase();
    try (Connection con = getConnectionViaDriverManager()) {
        GDSHelper gdsHelper = ((FBConnection) con).getGDSHelper();
        final FbDatabase currentDatabase = gdsHelper.getCurrentDatabase();
        final byte[] databaseInfo = currentDatabase.getDatabaseInfo(new byte[] { ISCConstants.isc_info_page_size }, 10);
        assertEquals("Unexpected info item", ISCConstants.isc_info_page_size, databaseInfo[0]);
        int length = iscVaxInteger2(databaseInfo, 1);
        int pageSize = iscVaxInteger(databaseInfo, 3, length);
        assertEquals("Unexpected page size", 16384, pageSize);
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) Connection(java.sql.Connection) FBConnection(org.firebirdsql.jdbc.FBConnection) GDSHelper(org.firebirdsql.gds.impl.GDSHelper) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) Test(org.junit.Test)

Example 15 with FBConnection

use of org.firebirdsql.jdbc.FBConnection in project jaybird by FirebirdSQL.

the class TestFBMaintenanceManager method createLimboTransaction.

private void createLimboTransaction(int count) throws Exception {
    try (FBConnection conn = (FBConnection) getConnectionViaDriverManager()) {
        final FbDatabase fbDatabase = conn.getFbDatabase();
        for (int i = 0; i < count; i++) {
            TransactionParameterBuffer tpBuf = conn.createTransactionParameterBuffer();
            FbTransaction transaction = fbDatabase.startTransaction(tpBuf);
            transaction.prepare(null);
        }
    }
}
Also used : FBConnection(org.firebirdsql.jdbc.FBConnection) TransactionParameterBuffer(org.firebirdsql.gds.TransactionParameterBuffer) FbDatabase(org.firebirdsql.gds.ng.FbDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction)

Aggregations

FBConnection (org.firebirdsql.jdbc.FBConnection)15 Test (org.junit.Test)10 FbDatabase (org.firebirdsql.gds.ng.FbDatabase)6 DataSource (javax.sql.DataSource)5 LocalTransaction (javax.resource.spi.LocalTransaction)4 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 GDSHelper (org.firebirdsql.gds.impl.GDSHelper)2 Statement (java.sql.Statement)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TransactionParameterBuffer (org.firebirdsql.gds.TransactionParameterBuffer)1 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)1 SQLExceptionChainBuilder (org.firebirdsql.util.SQLExceptionChainBuilder)1