PASS - TAKER : Anonymos

Java SE 8 OCP - JDBC

Correct : 7

38% Complete (success)

38 %

False : 11

61% Complete (success)

61 %



Anonymos 2018-12-08T01:05:49

Correct

1. Given: {code}
Assuming "org.gjt.mm.mysql.Driver" is a legitimate class, which line, when inserted at // INSERT CODE HERE, will correctly load this JDBC 3.0 driver?

B is correct. Prior to JDBC 4.0, JDBC drivers were required to register themselves with the DriverManager class by invoking DriverManager.register(this); after the driver was instantiated through a call from the classloader. The Class.forName() method calls the classloader, which in turn creates an instance of the class passed as a String to the method.

Not Correct

2. Given that you are working with a JDBC 4.0 driver, which three are required for this JDBC driver to be compliant?

To be JDBC 4.0 compliant, a JDBC driver must support the ability to autoload the driver by providing a file, META-INF/services/java.sql.Driver, that indicates the fully qualified class name of the Driver class that DriverManager should load upon start-up. The JDBC driver must implement the interfaces for Driver, Connection, Statement, ResultSet, and others. The driver must also support transactions.

Not Correct

3. Which three are available through an instance of DatabaseMetaData?

DatabaseMetaData provides data about the database and the Connection object. The name, version, and other JDBC driver information are available, plus information about the database, including the names of stored procedures, functions, SQL keywords, and more. Finally, the default transaction isolation level and data about what transaction levels are supported are also available through DatabaseMetaData.

Correct

4. Given: {code}
Assuming a Connection object has already been created (conn) and that the query produces a valid result, what is the result?

When the ResultSet returns, the cursor is pointing before the first row of the ResultSet. You must invoke the next() method to move to the next row of results before you can read any data from the columns. Trying to read a result using a getXXXX method will result in a SQLException when the cursor is before the first row or after the last row.

Not Correct

5. Given the SQL query: {code}
Assuming this is a valid SQL query and there is a valid Connection object (conn), which will compile correctly and execute this query?

Note that answer E is close, but will not compile because the executeUpdate(query) method returns an integer result. A will compile correctly, but throw a SQLException at runtime—the executeQuery method cannot be used on INSERT, UPDATE, DELETE, or DDL SQL queries. B will not compile because the createStatement method does not take a String argument for the query. C is incorrect because Statement does not have a setQuery method and this fragment will not compile.

Not Correct

6. Given: {code}
And assuming a valid Connection object (conn) and that the query will return results, what is the result?

Recall that the try-with-resources statement on line X will automatically close the resource specified at the close of the try block (when the closing curly brace is reached), and closing the Statement object automatically closes any open ResultSets associated with the Statement. The SQLException thrown is that the ResultSet is not open. To fix this code, move the while statement into the try-with-resources block.

Correct

7 Which interfaces must a vendor implement to be JDBC compliant? (Choose all that apply.)

A and F are incorrect. They are not interfaces required by JDBC. To query a database, the Statement interface is used, not the plausibly named, but mythical, Query interface.

Correct

8. Given this code fragment: {code}
Which query statements entered into <QUERY HERE> produce the output that follows the query string (in the following answer), assuming each query is valid? (Choose all that apply.)

All of the answers are correct (A, B, C, D, E). SELECT statements will produce a ResultSet even if there are no rows. INSERT, UPDATE, and DELETE statements all produce an update count, even when the number of rows affected is 0.

Correct

9. Given: {code}
Assuming the table name and column names are valid, what is the result?

Parameters are numbered from 1, not 0. When the program executes, a SQLException will be thrown by Line X. D is incorrect because the compiler cannot detect that the value of the method should be a 1 and not a zero. The compiler can only determine that the type of the argument is correct, and in this case, the type is correct as an integer. A and B are incorrect based on the above.

Correct

10. Given: {code}
And assuming that each pair of query elements in the array searchPair will return two rows and assuming a valid Connection object (conn), what is the result?

In the first iteration of the for loop, i = 0 and the pstmt.setString method index (the first parameter) is 1 and the second index is 2. But in the second iteration of the loop, the index value is now 3 and 4, respectively. It would be better to hard-code these two values as 1 and 2, respectively.

Not Correct

11. Given: {code}
If REMOVEBOOKS is a stored procedure that takes an INOUT integer parameter as its first argument and an IN String parameter as its second argument, which code blocks, when placed at the line // Code added here, could correctly execute the stored procedure and return a result?

Recall that to specify an IN parameter, you use a setXXXX method, and for an OUT parameter, you must register the parameter as an OUT before the call, and then use a getXXXX method to return the result from the stored procedure after executing the method.

Not Correct

12. Which creates a connected RowSet object?

This code fragment is creating an instance of a JdbcRowSet object—the only RowSet that is a connected RowSet object. This is the proper way to use the RowSetProvider static newFactory() method or obtain a RowSetFactory instance that is then used to create a JdbcRowSet instance.

Not Correct

13. Given: {code}
Assuming that the query produced a result set in Line Q and that the database goes offline on or before the line OFFLINE and comes back online on or before the line ONLINE, which statements are true?

First, the database being offline at any point after the execute() method is invoked is irrelevant, since this is a disconnected RowSet object (CachedRowSet). Thus, the results are cached in the object and changes can be made to the results, regardless of the status of the database. However, there is a critical error in this code: to write the changes made to the data due to the update, insert, and delete, the acceptChanges() method must be called in order to make a connection to the database and reconcile the results in the CachedRowSet with the database. Since this line of code is missing, the changes were only made to the in-memory object and not reflected in the database.

Not Correct

14. Given: {code}
And assuming the two queries are valid, what is the result of executing this fragment?

Because the Connection object conn was never set to setAutoCommit(false), there is no transaction context to rollback. All transactions are in auto-commit mode, so the first transaction is executed and completed, the second transaction is executed and completed, and when the conn.rollback() method is executed on line Z, a SQLException is thrown because there is no transaction to rollback.

Not Correct

15. Given: {code}
And given that the queries are valid, what is the result of executing this fragment?

Because there is no commit statement, the Connection closes when the try block completes, and the transaction created by setting setAutoCommit to false is rolled back.

Correct

16. Given: {code}
Assuming that the Order table was empty before this code fragment was executed and that the database supports multiple savepoints and that all of the queries are valid, what rows does Order contain?

The statement conn.rollback(sp1); rolls back the insertion of the row that contains the 'Fleece Jacket'. Then the transaction continues and processes the insertion of the row that contains 'Wool Scarf'.

Not Correct

17. Which are true about queries that throw SQLExceptions? (Choose all that apply.)

A is incorrect because a single query can throw many exceptions. C is incorrect; when many exceptions are thrown, you must use getNextException() to iterate through them.

Not Correct

18. Which are true about the results of a query?

For E you can use, for example, getRow(). B is incorrect; results are stored in a ResultSet object. C is incorrect; once a ResultSet is created, it does NOT stay synchronized with the database unless the ResultSet was created with one of the CONCUR_XXX cursor types. F is incorrect based on the above.