Repository navigation
Expand file tree
/
Copy pathJDBCTransaction.java
More file actions
50 lines (40 loc) · 1.92 KB
/
Copy pathJDBCTransaction.java
File metadata and controls
50 lines (40 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* JDBC transaction Example-1
*/
public class JDBCTransaction {
private static final String URL = "jdbc:mysql://localhost:3306/db1";
private static final String USER = "root";
private static final String PASSWORD = "root";
public static void main(String[] args) throws SQLException {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
try {
connection.setAutoCommit(false);
Statement st = connection.createStatement();
//operation-1-starts
st.executeUpdate(
"insert into user(user_id,name,password,date) values('3','ankit','$2a$12$CwEEzf51ZE10DjFULErUUeMBPkVSa7dMOODrpbUtJkp61TtY95MMe','2019-04-28 20:15:15')");
//operation -2 - starts
Statement st1 = connection.createStatement();
st1.executeUpdate(
"insert into user(user_id,name,password,date) values('4','admin','$2a$12$CwEEzf51ZE10DjFULErUUeMBPkVSa7dMOODrpbUtJkp61TtY95MMe','2019-04-28 20:15:15')");
//operation -3 - starts - adding duplicate primary key - it will throw an error.
Statement st2 = connection.createStatement();
st2.executeUpdate(
"insert into user(user_id,name,password,date) values('3','ankit','$2a$12$CwEEzf51ZE10DjFULErUUeMBPkVSa7dMOODrpbUtJkp61TtY95MMe','2019-04-28 20:15:15')");
// commit all operations
connection.commit();
} catch (SQLException ex) {
//In case of error, transaction will be rolledback.
if (null != connection) {
connection.rollback();
}
ex.printStackTrace();
}
}
}
}