What is the result of the employees table has no records before the code executed?
A.
1 Sam
B.
4 Jack
C.
3 John
4 Jack
D.
1 Sam
3 John
4 Jack
Explanation:
AutoCommit is set to false. The two following statements will be within the same transaction.
stmt.executeUpdate(“insert into employees values(1,’Sam’)”);
stmt.executeUpdate(“insert into employees values(2,’Jane’)”);
These two statements are rolled-back through (the savepoint is ignored! the savepoint must be
specified (e.g.
conn.rollback(save1); ) in the rollback if you want to rollback to the savepoint):
conn.rollback() ;
The next two insert statements are executed fine. Their result will be in the output.
Answewr is D
Sorry Answer is not D, the correct answer is C because the rollback undo all the transactions, even if there is a savepoint.
public static void main(String[] args) {
String url = “jdbc:mysql://localhost:3306/employeedb?zeroDateTimeBehavior=convertToNull”;
String username = “userName”;
String password = “password”;
String query = “SELECT * FROM manager”;
try (Connection conn =
DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();) {
try {
conn.setAutoCommit(false);
stmt.executeUpdate(“INSERT INTO manager (first_name) VALUES(‘Sam’);”);
Savepoint save1 = conn.setSavepoint(“point1”);
stmt.executeUpdate(“INSERT INTO manager (first_name) VALUES(‘Jane’);”);
conn.rollback();
//conn.rollback(save1);
stmt.executeUpdate(“INSERT INTO manager (first_name) VALUES(‘Jonh’);”);
conn.setAutoCommit(true);
stmt.executeUpdate(“INSERT INTO manager (first_name) VALUES(‘Jack’);”);
ResultSet rs = stmt.executeQuery(“select * from manager”);
while (rs.next()) {
System.out.println(rs.getString(1) + ” ” + rs.getString(2));
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
} catch (SQLException e) {
System.out.println(“SQL Exception: ” + e);
} // end of try-with-resources
}
delimiter $$
CREATE TABLE `manager` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(45) DEFAULT NULL,
`last_name` varchar(45) DEFAULT NULL,
`birth_date` date DEFAULT NULL,
`salary` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8$$
C
Usually posts some extremely fascinating stuff like this. If youre new to this site.
The data talked about within the write-up are a number of the ideal out there
one of our guests a short while ago recommended the following website
please go to the internet sites we comply with, like this a single, because it represents our picks in the web
just beneath, are numerous entirely not connected websites to ours, having said that, they’re surely really worth going over
that may be the end of this article. Right here you will find some web-sites that we think youll appreciate, just click the hyperlinks over
although web-sites we backlink to below are considerably not connected to ours, we feel they’re basically worth a go via, so have a look
here are some hyperlinks to websites that we link to because we think they may be really worth visiting
Sites of interest we have a link to
Sites of interest we’ve a link to
Sites of interest we’ve a link to
always a big fan of linking to bloggers that I appreciate but do not get a great deal of link love from
here are some links to web pages that we link to since we assume they may be really worth visiting