package com.pas.util; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import java.sql.*; public class SqlHelper { // 定義變量 private static Connection ct = null; // 大多數狀況下用preparedstatement替代statement private static PreparedStatement ps = null; private static ResultSet rs = null; // 鏈接數據庫的參數 private static String url = ""; private static String username = ""; private static String driver = ""; private static String passwd = ""; private static CallableStatement cs = null; public static CallableStatement getCs() { return cs; } private static Properties pp = null; private static InputStream fis = null; // 加載驅動,只須要一次,用靜態代碼塊 static { try { // 從dbinfo.properties pp = new Properties(); //類加載器默認主目錄SRC fis = SqlHelper.class.getClassLoader().getResourceAsStream( "dbInfo.properties"); // fis = new FileInputStream(); pp.load(fis); url = pp.getProperty("url"); username = pp.getProperty("username"); driver = pp.getProperty("driver"); passwd = pp.getProperty("passwd"); Class.forName(driver); } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } fis = null;// 垃圾回收站上收拾 } } // 獲得鏈接 public static Connection getConnection() { try { ct = DriverManager.getConnection(url, username, passwd); } catch (Exception e) { e.printStackTrace(); } return ct; } // *************callPro1存儲過程函數1************* public static CallableStatement callPro1(String sql, String[] parameters) { try { ct = getConnection(); cs = ct.prepareCall(sql); if (parameters != null) { for (int i = 0; i < parameters.length; i++) { cs.setObject(i + 1, parameters[i]); } } cs.execute(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { close(rs, cs, ct); } return cs; } // *******************callpro2存儲過程2************************ public static CallableStatement callPro2(String sql, String[] inparameters, Integer[] outparameters) { try { ct = getConnection(); cs = ct.prepareCall(sql); if (inparameters != null) { for (int i = 0; i < inparameters.length; i++) { cs.setObject(i + 1, inparameters[i]); } } // cs.registerOutparameter(2,oracle.jdbc.OracleTypes.CURSOR); if (outparameters != null) { for (int i = 0; i < outparameters.length; i++) { cs.registerOutParameter(inparameters.length + 1 + i, outparameters[i]); } } cs.execute(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { } return cs; } public static ResultSet executeQuery(String sql, String[] parameters) { try { ct = getConnection(); ps = ct.prepareStatement(sql); if (parameters != null) { for (int i = 0; i < parameters.length; i++) { ps.setString(i + 1, parameters[i]); } } rs = ps.executeQuery(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { //此處不關閉 返回後外部代碼關閉 } return rs; } public static Connection getCt() { return ct; } public static PreparedStatement getPs() { return ps; } public static ResultSet getRs() { return rs; } public static void executeUpdate2(String[] sql, String[][] parameters) { try { ct = getConnection(); ct.setAutoCommit(false); for (int i = 0; i < sql.length; i++) { if (null != parameters[i]) { ps = ct.prepareStatement(sql[i]); for (int j = 0; j < parameters[i].length; j++) { ps.setString(j + 1, parameters[i][j]); } ps.executeUpdate(); } } ct.commit(); } catch (Exception e) { e.printStackTrace(); try { ct.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } throw new RuntimeException(e.getMessage()); } finally { close(rs, ps, ct); } } // 先寫一個update、delete、insert // sql格式:update 表名 set 字段名 =?where 字段=? // parameter神應該是(」abc「,23) public static int executeUpdate(String sql, String[] parameters) { try { ct = getConnection(); ps = ct.prepareStatement(sql); if (parameters != null) { for (int i = 0; i < parameters.length; i++) { ps.setString(i + 1, parameters[i]); } } return ps.executeUpdate(); } catch (Exception e) { e.printStackTrace();// 開發階段 // 拋出異常 // 能夠處理,也能夠不處理 throw new RuntimeException(e.getMessage()); } finally { close(rs, ps, ct); } } public static void close(ResultSet rs, Statement ps, Connection ct) { // 關閉資源(先開後關) if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } ps = null; } if (null != ct) { try { ct.close(); } catch (SQLException e) { e.printStackTrace(); } ct = null; } } }
須要添加dbInfo.properties文件在src目錄下: java
url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL username=scott driver=oracle.jdbc.driver.OracleDriver passwd=654123