======================== 答案 ========================
=====================================================
=====================================================
B, F. The Driver
and PreparedStatement
interfaces are part of the JDK, making options A and E incorrect. Option C is incorrect because we made it up. The concrete DriverManager
class is also part of the JDK, making option D incorrect. Options B and F are correct since the implementation of these interfaces is part of the database-specific driver JAR file.
======================== 答案 ========================
=====================================================
=====================================================
A. A JDBC URL has three main parts separated by single colons, making options B, C, E, and F incorrect. The first part is always jdbc
, making option D incorrect. Therefore, the correct answer is option A. Notice that you can get this right even if you've never heard of the Sybase database before.
======================== 答案 ========================
=====================================================
=====================================================
B, D. When setting parameters on a PreparedStatement
, there are only options that take an index, making options C and F incorrect. The indexing starts with 1
, making option A incorrect. This query has only one parameter, so option E is also incorrect. Option B is correct because it simply sets the parameter. Option D is also correct because it sets the parameter and then immediately overwrites it with the same value.
======================== 答案 ========================
=====================================================
=====================================================
C. A Connection
is created using a static
method on DriverManager
. It does not use a constructor. Therefore, option C is correct. If the Connection
was created properly, the answer would be option B.
======================== 答案 ========================
=====================================================
=====================================================
B. The first line has a return type of boolean
, making it an execute()
call. The second line returns the number of modified rows, making it an executeUpdate()
call. The third line returns the results of a query, making it an executeQuery()
call. Therefore, option B is the answer.
======================== 答案 ========================
=====================================================
=====================================================
B. The first line enables autocommit mode. This is the default and means to commit immediately after each update. When the rollback()
runs, there are no uncommitted statements, so there is nothing to roll back. This gives us the initial two rows in addition to the inserted one making option B correct. If setAutoCommit(false)
were called, option A would be the answer. The ResultSet
types are just there to mislead you. Any types are valid for executeUpdate()
since no ResultSet
is involved.
======================== 答案 ========================
=====================================================
=====================================================
C. This code works as expected. It updates each of the five rows in the table and returns the number of rows updated. Therefore, option C is correct.
======================== 答案 ========================
=====================================================
=====================================================
A, B. Option A is one of the answers because you are supposed to use braces ({}
) for all SQL in a CallableStatement
. Option B is the other answer because each parameter should be passed with a question mark (?
). The rest of the code is correct. Note that your database might not behave the way that's described here, but you still need to know this syntax for the exam.
======================== 答案 ========================
=====================================================
=====================================================
E. This code declares a bind variable with ? but never assigns a value to it. The compiler does not enforce bind variables have values, so the code compiles, but produces a SQLException at runtime, making option E correct.
======================== 答案 ========================
=====================================================
=====================================================
D. JDBC code throws a SQLException
, which is a checked exception. The code does not handle or declare this exception, and therefore it doesn't compile. Since the code doesn't compile, option D is correct. If the exception were handled or declared, the answer would be option C.
======================== 答案 ========================
=====================================================
=====================================================
D. JDBC resources should be closed in the reverse order from that in which they were opened. The order for opening is Connection
, CallableStatement
, and ResultSet
. The order for closing is ResultSet
, CallableStatement
, and Connection,
which is option D.
======================== 答案 ========================
=====================================================
=====================================================
C. This code calls the PreparedStatement
twice. The first time, it gets the numbers greater than 3. Since there are two such numbers, it prints two lines. The second time, it gets the numbers greater than 100. There are no such numbers, so the ResultSet
is empty. Two lines are printed in total, making option C correct. The ResultSet
options are just there to trick you since only the default settings are used by the rest of the code.
======================== 答案 ========================
=====================================================
=====================================================
B, F. In a ResultSet
, columns are indexed starting with 1, not 0. Therefore, options A, C, and E are incorrect. There are methods to get the column as a String
or Object
. However, option D is incorrect because an Object
cannot be assigned to a String
without a cast.
======================== 答案 ========================
=====================================================
=====================================================
C. Since an OUT
parameter is used, the code should call registerOutParameter()
. Since this is missing, option C is correct.
======================== 答案 ========================
=====================================================
=====================================================
C, D. Rolling back to a point invalidates any savepoints created after it. Options A and E are incorrect because they roll back to lines 19 and 17, respectively. Option B is incorrect because you cannot roll back to the same savepoint twice. Options C and D are the answers because those savepoints were created after curly
.
======================== 答案 ========================
=====================================================
=====================================================
E. First, notice that this code uses a PreparedStatement
. Options A, B, and C are incorrect because they are for a CallableStatement
. Next, remember that the number of parameters must be an exact match, making option E correct. Remember that you will not be tested on SQL syntax. When you see a question that appears to be about SQL, think about what it might be trying to test you on.
======================== 答案 ========================
=====================================================
=====================================================
D. This code calls the PreparedStatement
twice. The first time, it gets the numbers greater than 3. Since there are two such numbers, it prints two lines. Since the parameter is not set between the first and second calls, the second attempt also prints two rows. Four lines are printed in total, making option D correct.
======================== 答案 ========================
=====================================================
=====================================================
D. Before accessing data from a ResultSet
, the cursor needs to be positioned. The call to rs.next()
is missing from this code causing a SQLException
and option D to be correct.
======================== 答案 ========================
=====================================================
=====================================================
E. This code should call prepareStatement()
instead of prepareCall()
since it is not executing a stored procedure. Since we are using var
, it does compile. Java will happily create a CallableStatement
for you. Since this compile safety is lost, the code will not cause issues until runtime. At that point, Java will complain that you are trying to execute SQL as if it were a stored procedure, making option E correct.
======================== 答案 ========================
=====================================================
=====================================================
B. The prepareStatement()
method requires SQL to be passed in. Since this parameter is omitted, line 27 does not compile, and option B is correct.
======================== 答案 ========================
=====================================================
=====================================================
B, D. The code starts with autocommit off. As written, we turn autocommit mode back on and immediately commit the transaction. This is option B. When line W is commented out, the update gets lost, making option D the other answer.