Java Code Examples for java.sql.CallableStatement.getGeneratedKeys()

The following are Jave code examples for showing how to use getGeneratedKeys() of the java.sql.CallableStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: the-vigilantes   File: StatementsTest.java   Source Code and License Vote up 5 votes
/**
 * Test for CallableStatement.executeLargeUpdate().
 * Validate update count returned and generated keys.
 */
public void testCallStmtExecuteLargeUpdate() throws Exception {
    createTable("testExecuteLargeUpdate", "(id BIGINT AUTO_INCREMENT PRIMARY KEY, n INT)");
    createProcedure("testExecuteLargeUpdateProc", "(IN n1 INT, IN n2 INT, IN n3 INT, IN n4 INT, IN n5 INT) BEGIN "
            + "INSERT INTO testExecuteLargeUpdate (n) VALUES (n1), (n2), (n3), (n4), (n5); END");

    CallableStatement testCstmt = this.conn.prepareCall("{CALL testExecuteLargeUpdateProc(?, ?, ?, ?, ?)}");
    testCstmt.setInt(1, 1);
    testCstmt.setInt(2, 2);
    testCstmt.setInt(3, 3);
    testCstmt.setInt(4, 4);
    testCstmt.setInt(5, 5);

    long count = testCstmt.executeLargeUpdate();
    assertEquals(5, count);
    assertEquals(5, testCstmt.getLargeUpdateCount());

    this.rs = testCstmt.getGeneratedKeys();

    // Although not requested, CallableStatements makes gerenated keys always available.
    ResultSetMetaData rsmd = this.rs.getMetaData();
    assertEquals(1, rsmd.getColumnCount());
    assertEquals(JDBCType.BIGINT.getVendorTypeNumber().intValue(), rsmd.getColumnType(1));
    assertEquals(20, rsmd.getColumnDisplaySize(1));

    // We can't check the generated keys as they are not returned correctly in this case (last_insert_id is missing from OK_PACKET when executing inserts
    // within a stored procedure - Bug#21792359).
    //        long generatedKey = 0;
    //        while (this.rs.next()) {
    //            assertEquals(++generatedKey, this.rs.getLong(1));
    //        }
    //        assertEquals(5, generatedKey);
    this.rs.close();
}
 
Example 2
Project: OpenVertretung   File: StatementsTest.java   Source Code and License Vote up 5 votes
/**
 * Test for CallableStatement.executeLargeUpdate().
 * Validate update count returned and generated keys.
 */
public void testCallStmtExecuteLargeUpdate() throws Exception {
    createTable("testExecuteLargeUpdate", "(id BIGINT AUTO_INCREMENT PRIMARY KEY, n INT)");
    createProcedure("testExecuteLargeUpdateProc", "(IN n1 INT, IN n2 INT, IN n3 INT, IN n4 INT, IN n5 INT) BEGIN "
            + "INSERT INTO testExecuteLargeUpdate (n) VALUES (n1), (n2), (n3), (n4), (n5); END");

    CallableStatement testCstmt = this.conn.prepareCall("{CALL testExecuteLargeUpdateProc(?, ?, ?, ?, ?)}");
    testCstmt.setInt(1, 1);
    testCstmt.setInt(2, 2);
    testCstmt.setInt(3, 3);
    testCstmt.setInt(4, 4);
    testCstmt.setInt(5, 5);

    long count = testCstmt.executeLargeUpdate();
    assertEquals(5, count);
    assertEquals(5, testCstmt.getLargeUpdateCount());

    this.rs = testCstmt.getGeneratedKeys();

    // Although not requested, CallableStatements makes gerenated keys always available.
    ResultSetMetaData rsmd = this.rs.getMetaData();
    assertEquals(1, rsmd.getColumnCount());
    assertEquals(JDBCType.BIGINT.getVendorTypeNumber().intValue(), rsmd.getColumnType(1));
    assertEquals(20, rsmd.getColumnDisplaySize(1));

    // We can't check the generated keys as they are not returned correctly in this case (last_insert_id is missing from OK_PACKET when executing inserts
    // within a stored procedure - Bug#21792359).
    //        long generatedKey = 0;
    //        while (this.rs.next()) {
    //            assertEquals(++generatedKey, this.rs.getLong(1));
    //        }
    //        assertEquals(5, generatedKey);
    this.rs.close();
}
 
Example 3
Project: ProyectoPacientes   File: StatementsTest.java   Source Code and License Vote up 5 votes
/**
 * Test for CallableStatement.executeLargeUpdate().
 * Validate update count returned and generated keys.
 */
public void testCallStmtExecuteLargeUpdate() throws Exception {
    createTable("testExecuteLargeUpdate", "(id BIGINT AUTO_INCREMENT PRIMARY KEY, n INT)");
    createProcedure("testExecuteLargeUpdateProc", "(IN n1 INT, IN n2 INT, IN n3 INT, IN n4 INT, IN n5 INT) BEGIN "
            + "INSERT INTO testExecuteLargeUpdate (n) VALUES (n1), (n2), (n3), (n4), (n5); END");

    CallableStatement testCstmt = this.conn.prepareCall("{CALL testExecuteLargeUpdateProc(?, ?, ?, ?, ?)}");
    testCstmt.setInt(1, 1);
    testCstmt.setInt(2, 2);
    testCstmt.setInt(3, 3);
    testCstmt.setInt(4, 4);
    testCstmt.setInt(5, 5);

    long count = testCstmt.executeLargeUpdate();
    assertEquals(5, count);
    assertEquals(5, testCstmt.getLargeUpdateCount());

    this.rs = testCstmt.getGeneratedKeys();

    // Although not requested, CallableStatements makes gerenated keys always available.
    ResultSetMetaData rsmd = this.rs.getMetaData();
    assertEquals(1, rsmd.getColumnCount());
    assertEquals(JDBCType.BIGINT.getVendorTypeNumber().intValue(), rsmd.getColumnType(1));
    assertEquals(20, rsmd.getColumnDisplaySize(1));

    // We can't check the generated keys as they are not returned correctly in this case (last_insert_id is missing from OK_PACKET when executing inserts
    // within a stored procedure - Bug#21792359).
    //        long generatedKey = 0;
    //        while (this.rs.next()) {
    //            assertEquals(++generatedKey, this.rs.getLong(1));
    //        }
    //        assertEquals(5, generatedKey);
    this.rs.close();
}
 
Example 4
Project: BibliotecaPS   File: StatementsTest.java   Source Code and License Vote up 5 votes
/**
 * Test for CallableStatement.executeLargeUpdate().
 * Validate update count returned and generated keys.
 */
public void testCallStmtExecuteLargeUpdate() throws Exception {
    createTable("testExecuteLargeUpdate", "(id BIGINT AUTO_INCREMENT PRIMARY KEY, n INT)");
    createProcedure("testExecuteLargeUpdateProc", "(IN n1 INT, IN n2 INT, IN n3 INT, IN n4 INT, IN n5 INT) BEGIN "
            + "INSERT INTO testExecuteLargeUpdate (n) VALUES (n1), (n2), (n3), (n4), (n5); END");

    CallableStatement testCstmt = this.conn.prepareCall("{CALL testExecuteLargeUpdateProc(?, ?, ?, ?, ?)}");
    testCstmt.setInt(1, 1);
    testCstmt.setInt(2, 2);
    testCstmt.setInt(3, 3);
    testCstmt.setInt(4, 4);
    testCstmt.setInt(5, 5);

    long count = testCstmt.executeLargeUpdate();
    assertEquals(5, count);
    assertEquals(5, testCstmt.getLargeUpdateCount());

    this.rs = testCstmt.getGeneratedKeys();

    // Although not requested, CallableStatements makes gerenated keys always available.
    ResultSetMetaData rsmd = this.rs.getMetaData();
    assertEquals(1, rsmd.getColumnCount());
    assertEquals(JDBCType.BIGINT.getVendorTypeNumber().intValue(), rsmd.getColumnType(1));
    assertEquals(20, rsmd.getColumnDisplaySize(1));

    // We can't check the generated keys as they are not returned correctly in this case (last_insert_id is missing from OK_PACKET when executing inserts
    // within a stored procedure - Bug#21792359).
    //        long generatedKey = 0;
    //        while (this.rs.next()) {
    //            assertEquals(++generatedKey, this.rs.getLong(1));
    //        }
    //        assertEquals(5, generatedKey);
    this.rs.close();
}