⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 transactiontest.java

📁 这是一本描述JDBC数据库的书籍
💻 JAVA
字号:
/* * TransactionTest.java * * Created on June 11, 2005, 12:42 AM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */package ch03;import java.sql.*;import common.*;/** * * @author kevin */public class TransactionTest {        public static void main(String[] args){        Connection connCommit = null;        Connection connCommitRe = null;        Statement stmt  = null;        Statement stmtRe = null;        ResultSet rs = null;        String sqlString = "insert into simple_tbl (id, name) values (100, \'insert data\')";        try{            ConnectionFactory factory = ConnectionFactory.getConnectionFactory();            connCommit = factory.getConnection();            connCommit.setTransactionIsolation(connCommit.TRANSACTION_READ_COMMITTED);            connCommit.setAutoCommit(false);            stmt = connCommit.createStatement();            connCommitRe = factory.getConnection();            connCommitRe.setTransactionIsolation(connCommitRe.TRANSACTION_SERIALIZABLE);            connCommitRe.setAutoCommit(false);            stmtRe = connCommit.createStatement();                                    rs = stmt.executeQuery("select id, name from simple_tbl");            display(rs);            rs = stmtRe.executeQuery("select id, name from simple_tbl");            display(rs);                        System.out.println("insert data");            stmt.executeUpdate(sqlString);            rs = stmt.executeQuery("select id, name from simple_tbl");            display(rs);            rs = stmtRe.executeQuery("select id, name from simple_tbl");            display(rs);                              System.out.println("delete from tab");            stmt.executeUpdate("delete from simple_tbl where id=1");            rs = stmt.executeQuery("select id, name from simple_tbl");            display(rs);            rs = stmtRe.executeQuery("select id, name from simple_tbl");            display(rs);                                    connCommit.rollback();            connCommitRe.rollback();        }catch(SQLException e){            ExportSQLInfo.showSQLException(e);        }catch(Exception e){            e.printStackTrace();        }        finally{            DBUtils.close(rs,stmt,connCommit);            DBUtils.close(stmtRe, connCommitRe);        }    }        public static void display(ResultSet rs){        try{            while(rs.next()){                long id = rs.getLong("id");                String name = rs.getString("name");                System.out.println("ID: " + id + " NAME: " + name);            }        }catch(SQLException e){            ExportSQLInfo.showSQLException(e);        }finally{            DBUtils.close(rs);        }    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -