public class DBCon {// 資料庫驅動對象public static final String DRIVER = "oracle.jdbc.driver.OracleDriver";// 資料庫連接地址(資料庫名)public static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";// 登陸名public static final String USER = "scott";// 登陸密碼public static final String PWD = "123456";// 建立資料庫連接對象private Connection con = null;// 建立資料庫先行編譯對象private PreparedStatement ps = null;// 建立結果集private ResultSet rs = null;// 建立資料來源對象public static DataSource source = null;public Connection getCon() {try{Class.forName(DRIVER);con = DriverManager.getConnection(URL, USER, PWD);System.out.println("資料庫連接成功。");} catch (Exception e) {System.err.println("資料庫連接失敗。");e.printStackTrace();}return con;}public void closeAll() {if (rs != null)try {rs.close();} catch (SQLException e) {e.printStackTrace();}if (ps != null)try {ps.close();} catch (SQLException e) {e.printStackTrace();}if (con != null)try {con.close();} catch (SQLException e) {e.printStackTrace();}}public int update(String sql,String... pras){ int resu=0; con=getCon(); try { ps=con.prepareStatement(sql); for(int i=0;i<pras.length;i++){ ps.setString(i+1,pras[i]); } resu=ps.executeUpdate(); } catch (SQLException e) { } finally{ closeAll(); } return resu; } public ResultSet query(String sql,String... pras){ con=getCon(); try { ps=con.prepareStatement(sql); if(pras!=null) for(int i=0;i<pras.length;i++){ ps.setString(i+1, pras[i]); } rs=ps.executeQuery(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public static void main(String[] args) throws SQLException {DBCon dbCon = new DBCon();//dbCon.getCon();ResultSet resultSet = dbCon.query("select * from emp", null);while (resultSet.next()) {System.out.printf("%-10d",resultSet.getInt(1)); //輸出10列,靠左對齊(-號表示靠左對齊)//System.out.print(resultSet.getInt(1) + "\t\t");System.out.printf("%-10s",resultSet.getString(2)); //System.out.print(resultSet.getString(2) + "\t\t");System.out.printf("%-20s",resultSet.getString(3)); //System.out.print(resultSet.getString(3) + "\t\t\t\t");System.out.printf("%-10d",resultSet.getInt(4)); //System.out.print(resultSet.getInt(4) + "\t\t");System.out.print(resultSet.getDate(5) + "\t\t");System.out.print(resultSet.getDouble(6) + "\t\t");System.out.print(resultSet.getDouble(7) + "\t\t");System.out.println(resultSet.getInt(8));}dbCon.closeAll();}}
Jar包下載 http://yunpan.cn/QC5IeekKJvixN 提取碼 243b
Java操作Oracle實現交易回復
DBCon dbCon = new DBCon();Connection connection = dbCon.getCon();String sql = "";Statement statement = null;try {connection.setAutoCommit(false);//設定事物不自動認可sql = "update emp set sal = sal - 100 where ename = 'SMITH'";statement = connection.createStatement();statement.executeUpdate(sql);int i = 7/0; //拋異常。sql = "update emp set sal = sal - 100 where ename = 'JAMES'";statement.executeUpdate(sql);connection.commit();} catch (Exception e) {//注意這裡如果是SQLException,上面7/0拋出的異常就不會被這裡捕獲到,下面的rollback自然就不會執行。陷阱try {connection.rollback();//交易回復} catch (SQLException e1) {e1.printStackTrace();}e.printStackTrace();}finally{dbCon.closeAll();if (statement != null) {try {statement.close();} catch (SQLException e) {e.printStackTrace();}}}
預設情況下事物自動認可,此時上例中拋出異常後,前面的sql語句可以執行成功,後面的不會被執行。進行事物管理之後,上例中拋出異常後,事物會在下面的catch塊中復原。保證兩個sql語句都不執行。
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1532489