Page 1 of 1

About Question enthuware.ocpjp.v8.2.1196 :

Posted: Mon Apr 18, 2016 1:23 am
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

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

Posted: Mon Apr 18, 2016 7:45 pm
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.

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

Posted: Tue Apr 19, 2016 1:19 am
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();
}

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

Posted: Tue Apr 19, 2016 3:24 am
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.

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

Posted: Tue Apr 19, 2016 11:22 am
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

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

Posted: Sun Mar 21, 2021 10:19 am
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?

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

Posted: Sun Mar 21, 2021 12:27 pm
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.