Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Medium
Question 54
Given structure of EMPLOYEE table:
EMPLOYEE (ID integer, FIRSTNAME varchar(100), LASTNAME varchar(100), SALARY real, PRIMARY KEY (ID))
EMPLOYEE table contains below records:
- 101 John Smith 12000
- 102 Sean Smith 15000
- 103 Regina Williams 15500
- 104 Natasha George 14600
Given code of Test.java file:
- package com.udayan.ocp;
- import java.sql.*;
- public class Test {
- public static void main(String[] args) {
- String url = "jdbc:mysql://localhost:3306/ocp";
- String user = "root";
- String password = "password";
- String query = "Select ID, FIRSTNAME, LASTNAME, SALARY FROM EMPLOYEE WHERE SALARY > 14900 ORDER BY ID";
- try (Connection con = DriverManager.getConnection(url, user, password);
- Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
- ResultSet rs = stmt.executeQuery(query);) {
- rs.absolute(2);
- rs.updateDouble("SALARY", 20000);
- } catch (SQLException ex) {
- System.out.println("Error");
- }
- }
- }
Also assume:
URL, username and password are correct.
SQL query is correct and valid.
The JDBC 4.2 driver jar is configured in the classpath.
What will be the result of compiling and executing Test class?
-
A
'Error' is printed on to the console
-
B
Program executes successfully but no record is updated in the database
-
C
Program executes successfully and salary of Sean Smith is updated to 20000
-
D
Program executes successfully and salary of Regina Williams is updated to 20000
Reveal correct answer
Correct answer: B
Explanation
Given sql statement returns below records:
102 Sean Smith 15000
103 Regina Williams 15500
'rs.absolute(2);' moves the cursor pointer to 2nd record.
'rs.updateDouble("SALARY", 20000);' updates the salary of 2nd record to 20000 but to update the records in the database, 'rs.updateRow();' statement must be invoked.
As 'rs.updateRow()' statement is missing hence no record is updated in the database.
Please note: there is no need to invoke con.commit(); method as by default Connection object is in auto-commit mode.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
