01.
package
com.java.myapp;
02.
03.
import
java.sql.SQLException;
04.
import
java.sql.Statement;
05.
import
java.sql.Connection;
06.
import
java.sql.DriverManager;
07.
08.
public
class
MyClass {
09.
10.
public
static
void
main(String[] args){
11.
12.
Connection connect =
null
;
13.
Statement st =
null
;
14.
15.
try
{
16.
Class.forName(
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
);
17.
connect = DriverManager.getConnection(
""
+
19.
20.
connect.setAutoCommit(
false
);
21.
22.
st = connect.createStatement();
23.
24.
25.
String sql1 =
"INSERT INTO customer "
+
26.
"(CustomerID,Name,Email,CountryCode,Budget,Used) "
+
27.
"VALUES ('C005','Chai Surachai','chai.surachai@thaicreate.com'"
+
28.
",'TH','1000000','0') "
;
29.
st.execute(sql1);
30.
31.
32.
String sql2 =
"INSERT INTO customer "
+
33.
"(CustomerID,Name,Email,CountryCode,Budget,Used) "
+
34.
"VALUES ('C005','Chai Surachai','chai.surachai@thaicreate.com'"
+
35.
",'TH','1000000','0') "
;
36.
st.execute(sql2);
37.
38.
System.out.println(
"Record Inserted Successfully"
);
39.
40.
41.
}
catch
(Exception e) {
42.
43.
if
(connect !=
null
) {
44.
try
{
45.
st.close();
46.
connect.rollback();
47.
System.err.print(
"Transaction is being rolled back"
);
48.
}
catch
(SQLException e2) {
49.
e2.printStackTrace();
50.
}
51.
}
52.
53.
}
finally
{
54.
if
(st !=
null
) {
55.
try
{
56.
st.close();
57.
connect.setAutoCommit(
true
);
58.
}
catch
(SQLException e) {
59.
e.printStackTrace();
60.
}
61.
}
62.
}
63.
64.
try
{
65.
connect.close();
66.
}
catch
(SQLException e) {
67.
68.
e.printStackTrace();
69.
}
70.
}
71.
72.
}