OCP Java17 SE Developers 复习题15(完)

======================== 答案 ========================

=====================================================

=====================================================

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.

相关推荐
找不到、了2 分钟前
分布式理论:CAP、Base理论
java·分布式
天天摸鱼的java工程师5 分钟前
2025已过半,Java就业大环境究竟咋样了?
java·后端
人生在勤,不索何获-白大侠10 分钟前
day16——Java集合进阶(Collection、List、Set)
java·开发语言
Zedthm16 分钟前
LeetCode1004. 最大连续1的个数 III
java·算法·leetcode
艺杯羹27 分钟前
MyBatis之核心对象与工作流程及SqlSession操作
java·mybatis
神的孩子都在歌唱35 分钟前
3423. 循环数组中相邻元素的最大差值 — day97
java·数据结构·算法
喜欢吃豆1 小时前
深入企业内部的MCP知识(三):FastMCP工具转换(Tool Transformation)全解析:从适配到增强的工具进化指南
java·前端·人工智能·大模型·github·mcp
用户1551733938831 小时前
前后端处理 `multipart/form-data` 混合参数(实体对象+文件)方案
java
东阳马生架构1 小时前
订单初版—3.支付和履约链路中的技术问题说明文档
java
留不住丨晚霞2 小时前
说说SpringBoot常用的注解?
java·开发语言