package com.java.myapp;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class MyClass {
public static void main(String[] args){
Connection connect = null;
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
connect.setAutoCommit(false);
st = connect.createStatement();
// Statement 1
String sql1 = "INSERT INTO customer " +
"(CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Chai Surachai','[email protected]'" +
",'TH','1000000','0') ";
st.execute(sql1);
// Statement 2
String sql2 = "INSERT INTO customer " +
"(CustomerID,Name,Email,CountryCode,Budget,Used) " +
"VALUES ('C005','Chai Surachai','[email protected]'" +
",'TH','1000000','0') ";
st.execute(sql2);
System.out.println("Record Inserted Successfully");
} catch (Exception e) {
// TODO Auto-generated catch block
if (connect != null) {
try {
st.close();
connect.rollback();
System.err.print("Transaction is being rolled back");
} catch(SQLException e2) {
e2.printStackTrace();
}
}
} finally {
if (st != null) {
try {
st.close();
connect.setAutoCommit(true);
} catch(SQLException e) {
e.printStackTrace();
}
}
}
try {
connect.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}