About Question enthuware.ocpjp.v8.2.1196 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1196 :

Post by schchen2000 »

c.rollback(sp1); // passing the Savepoint variable here
Had we passed sp1 into the rollback(), we would have only lost the 1st record insertion.

We would have counted the 2nd insertion and therefore our final answer would have been 3 instead of 2.

Is that correct? Many thanks.

Schmichael

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by admin »

Could you please post complete code that you want to discuss? Without that, it is difficult to figure out the changes you are trying to make to code that is given with the question.

-Paul.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by schchen2000 »

Code: Select all

Assuming that the table student2 does not have any row, how many rows will be present in this table after the following code is executed?          

try (Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/sample", "app", "app");){
	c.setAutoCommit(false);
	Statement stmt = c.createStatement();
	int a1 = stmt.executeUpdate("insert into STUDENT2 values (1, 'aaa', 1.1)");
	Savepoint sp1 = c.setSavepoint();
	int a2 = stmt.executeUpdate("insert into STUDENT2 values (2, 'bbb', 2.1)");

	******c.rollback(sp1);***** <--- pass sp1 to rollback

	int a3 = stmt.executeUpdate("insert into STUDENT2 values (3, 'ccc', 3.1)");
	c.setAutoCommit(true);
	int a4 = stmt.executeUpdate("insert into STUDENT2 values (4, 'ddd', 4.1)");
}catch (SQLException e){
	e.printStackTrace();
}
Last edited by admin on Tue Apr 19, 2016 3:22 am, edited 3 times in total.
Reason: Updated the code to show the change

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by admin »

Assuming that you are trying to change the code as shown above, the call to rollback will roll back the row inserted by int a2 = stmt.executeUpdate("insert into STUDENT2 values (2, 'bbb', 2.1)");
So finally, the table should have records for 1, 3, and 4.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by schchen2000 »

admin wrote:Assuming that you are trying to change the code as shown above, the call to rollback will roll back the row inserted by int a2 = stmt.executeUpdate("insert into STUDENT2 values (2, 'bbb', 2.1)");
So finally, the table should have records for 1, 3, and 4.
Thanks for the reply.

Schmichael

jme_chg
Posts: 29
Joined: Sun Feb 07, 2021 6:30 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by jme_chg »

By the way, what would happen if we replaced:

int a4 = stmt.executeUpdate("insert into STUDENT2 values (4, 'ddd', 4.1)");

with:

int a4 = stmt.executeUpdate("insert into STUDENT2 values (3, 'ddd', 4.1)");

would there be an SQLException or would the row with primary key 3 be overwritten?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1196 :

Post by admin »

An insert query tries to insert a new row and if a row by the same pk exists, the db will reject the query and you will end up with an SQLException in the java code.
You can try it out with JavaDB that comes bundled with IDEs such as NetBeans.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 10 guests