jdbc savePoint
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class TestSavePoint {
public static void main(String[] args) throws Exception {
TestSavePoint tsp=new TestSavePoint();
Statement stmt=conn.createStatement();
Savepoint sp1=conn.setSavepoint("p1");
stmt.executeUpdate("insert into student(name,sex,grade) values('fengbei','m','2008811237')");
stmt.executeUpdate("insert into student(name,sex,grade) values('lisong','m','2008811221')");
Savepoint sp2=conn.setSavepoint("p2");
stmt.executeUpdate("insert into student(name,sex,grade) values('heyanjie','m','2008811216')");
Savepoint sp3=conn.setSavepoint("p3");
ResultSet rs=stmt.executeQuery("select * from student where name='wangshuhui'");
if(!rs.next()){
conn.rollback(sp1);
}
conn.commit();
rs=stmt.executeQuery("select * from student");
while(rs.next()) {
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
stmt.close();
conn.close();
}
//獲取連線
public Connection getConn() {
Connection conn=null;
String url="jdbc:mysql://localhost/dandan?user=root&password=dada";
try {
Class.forName("org.gjt.mm.mysql.Driver");
try {
conn=DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
public class TestSavePoint {
public static void main(String[] args) throws Exception {
TestSavePoint tsp=new TestSavePoint();
Connection conn=tsp.getConn();
//設定自動提交失效
conn.setAutoCommit(false);Statement stmt=conn.createStatement();
Savepoint sp1=conn.setSavepoint("p1");
stmt.executeUpdate("insert into student(name,sex,grade) values('fengbei','m','2008811237')");
stmt.executeUpdate("insert into student(name,sex,grade) values('lisong','m','2008811221')");
Savepoint sp2=conn.setSavepoint("p2");
stmt.executeUpdate("insert into student(name,sex,grade) values('heyanjie','m','2008811216')");
Savepoint sp3=conn.setSavepoint("p3");
ResultSet rs=stmt.executeQuery("select * from student where name='wangshuhui'");
if(!rs.next()){
conn.rollback(sp1);
}
conn.commit();
rs=stmt.executeQuery("select * from student");
while(rs.next()) {
System.out.println("Row: "+rs.getRow()+"\tId: "+rs.getInt(1)+"\tName: "+rs.getString(2)
+"\tSex: "+rs.getString(3)+"\tGrade: "+rs.getString(4));
}
stmt.close();
conn.close();
}
//獲取連線
public Connection getConn() {
Connection conn=null;
String url="jdbc:mysql://localhost/dandan?user=root&password=dada";
try {
Class.forName("org.gjt.mm.mysql.Driver");
try {
conn=DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
相關文章
- mysql & oracle savepoint 使用方法MySqlOracle
- MySQL儲存過程SAVEPOINT ROLLBACK toMySql儲存過程
- 關於SAVEPOINT和自定義異常使用的一點看法
- JDBCJDBC
- tcbs_批量儲存過程_plsql事務_savepoint_異常儲存過程SQL
- JDBC學習1:詳解JDBC使用JDBC
- JavaWeb——JDBCJavaWebJDBC
- JDBC概述JDBC
- jdbc【oracle】JDBCOracle
- JDBC codeJDBC
- jdbc作用JDBC
- jdbc batchJDBCBAT
- JDBC differences between the DB2 Universal JDBC Driver and other DB2 JDBC driversJDBCDB2
- [20180222]Oracle中如何追蹤savepoint.txtOracle
- Java 之 JDBCJavaJDBC
- SpringData JDBCSpringJDBC
- Java之JDBCJavaJDBC
- JDBC詳解JDBC
- JDBC快速上手JDBC
- JDBC技術JDBC
- Spark JDBC To MySQLSparkJDBCMySql
- JDBC 簡介JDBC
- java-jdbcJavaJDBC
- weblogic jdbcWebJDBC
- JDBC-MySqlJDBCMySql
- JDBC的使用JDBC
- jdbc編碼?JDBC
- Java之JDBC①JavaJDBC
- 【Spring】jdbcSpringJDBC
- Doris JDBC 外表JDBC
- 9.JDBCJDBC
- JDBC學習JDBC
- JDBC【事務、後設資料、改造JDBC工具類】JDBC
- JDBC學習筆記-----jdbc效能最佳化 (轉)JDBC筆記
- oracle plsql採用for loop和rollback savepoint實現重複值可繼續插入exceptionOracleSQLOOPException
- Java 之 JDBC(二)JavaJDBC
- Spring JDBC框架SpringJDBC框架
- Mysql入門【JDBC】MySqlJDBC