import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo09 {
final static String url = "jdbc:mysql://localhost/test";
final static String user = "root";
final static String password = "root";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
boolean autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
stmt
.executeUpdate("insert into user (username,userpassword,email) values('hehe','123','hehe@163.com')");
int i = 100 / 0;
stmt
.executeUpdate("insert into user (username,userpassword,email)values('hehe1','1234','hehe1@163.com')");
conn.commit();
conn.setAutoCommit(autoCommit);
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo09 {
final static String url = "jdbc:mysql://localhost/test";
final static String user = "root";
final static String password = "root";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
boolean autoCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
stmt
.executeUpdate("insert into user (username,userpassword,email) values('hehe','123','hehe@163.com')");
int i = 100 / 0;
stmt
.executeUpdate("insert into user (username,userpassword,email)values('hehe1','1234','hehe1@163.com')");
conn.commit();
conn.setAutoCommit(autoCommit);
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}