jdbc事务处理

数据表student:

 

数据类型:

 

jdbc事务处理_第1张图片

 

 

数据字段:

jdbc事务处理_第2张图片

 

 

 

下面是jdbc事务处理列子:

 

清单如下:

 

import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Savepoint; public class MyTest { /** * jdbc事务级处理 * * @author caodehua * @exception * @version 2.0 */ public static Connection conn = null; public static PreparedStatement pst = null; public static ResultSet rst = null; // 加载jdbc驱动程序 static { try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // 建立数据库连接 public static Connection getConnection() { try { conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=root&useUnicode=true&characterEncoding=UTF-8"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } // 添加student数据 public static void insertStudentData() throws Exception { conn = getConnection(); pst = conn.prepareStatement("insert into student(name,age,sex,phone,mail) values(?,?,?,?,?)"); pst.setString(1, "zhangwin"); pst.setInt(2, 22); pst.setString(3, "1"); pst.setString(4, "13721165552"); pst.setString(5, "[email protected]"); conn.setAutoCommit(false); // conn.setSavepoint("A"); int count = pst.executeUpdate(); if (count > 0) { conn.commit(); conn.setAutoCommit(true); System.out.println("insert data successful"); } else { System.out.println("insert data failureful"); // conn.rollback(); } closeConnection(null, pst, conn); } // 检索student数据 public static void selectStudentData() throws Exception { conn = getConnection(); pst = conn.prepareStatement("select * from student"); rst = pst.executeQuery(); // conn.setSavepoint("A"); while (rst.next()) { if (rst.last()) { System.out.println(rst.getInt(1) + "/t" + rst.getString(2) + "/t" + rst.getInt(3) + "/t" + rst.getString(4) + "/t" + rst.getString(5) + "/t" + rst.getString(6)); } else { break; } } closeConnection(rst, pst, conn); } // 修改student数据 public static void updateStudentData() throws Exception { conn = getConnection(); pst = conn.prepareStatement("update student set phone=? where id=?"); pst.setString(1, "13725559771"); pst.setInt(2, 5); conn.setAutoCommit(false); int rows = pst.executeUpdate(); if (rows > 0) { conn.commit(); conn.setAutoCommit(true); System.out.println("update data successful"); } else { conn.rollback(); System.out.println("update data failureful"); } closeConnection(null, pst, conn); } // 删除student数据 public static void deleteStudentData() throws Exception { conn=getConnection(); pst=conn.prepareStatement("delete from student where id=?"); pst.setInt(1, 5); conn.setAutoCommit(false); int rows=pst.executeUpdate(); if(rows>0){ conn.commit(); conn.setAutoCommit(true); System.out.println("delete data successful"); }else{ conn.rollback(); System.out.println("delete data failureful"); } closeConnection(null, pst, conn); } // 关闭数据库连接 public static void closeConnection(ResultSet rst, PreparedStatement pst, Connection conn) throws Exception { try { if (rst != null) { rst.close(); } if (pst != null) { pst.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } // 主程序入口,调用相关方法 public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // insertStudentData(); // selectStudentData(); // updateStudentData(); // deleteStudentData(); } }

 

 

 

你可能感兴趣的:(jdbc事务处理)