日期:2014-05-16 浏览次数:20631 次
package com.abin.db.connection; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public final class DBClassConnection { //Oracle 的数据库连接参数 private static String driver="oracle.jdbc.driver.OracleDriver"; private static String url="jdbc:oracle:thin:@localhost:1521:orcl"; private static String username="zhang"; private static String password="zhang"; public DBClassConnection ()throws ClassNotFoundException{ } //Oracle连接 public static Connection getOracle(){ Connection conn=null; try{ if(null == conn || conn.isClosed()){ Class.forName(driver).newInstance(); conn=DriverManager.getConnection(url,username,password); } }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } return conn; } //oracle插入,修改,删除语句(PreparedStatement)executeUpdate public static int psexecuteUpdate(String sql){ int count=0; Connection conn=getOracle(); try{ PreparedStatement ps=null; if(null==ps||ps.isClosed()){ ps=conn.prepareStatement(sql); count=ps.executeUpdate(); } }catch(Exception e){ throw new RuntimeException(e); } return count; } //oracle插入,修改,删除语句(PreparedStatement)execute public static boolean psexecute(String sql){ boolean flag=false; try{ Connection conn=getOracle(); PreparedStatement ps=null; if(null==ps||ps.isClosed()){ ps=conn.prepareStatement(sql); flag=ps.execute(); } }catch(Exception e){ throw new RuntimeException(e); } return flag; } //oracle插入,修改,删除语句(PreparedStatement)execute public static boolean stexecute(String sql){ boolean flag=false; try{ Connection conn=getOracle(); Statement stmt=null; if(null==stmt||stmt.isClosed()){ stmt=conn.createStatement(); flag=stmt.execute(sql); } }catch(Exception e){ throw new RuntimeException(e); } return flag; } //oracle插入,修改,删除语句(Statement) public static int stexecuteUpdate(String sql){ int count=0; Connection conn=getOracle(); try{ Statement stmt=null; if(null==stmt||stmt.isClosed()){ stmt=conn.createStatement(); stmt.executeUpdate(sql); } }catch(Exception e){ throw new RuntimeException(e); } return count; } //oracle 的 Statement结果集 ResultSet public static ResultSet stexecuteQuery(String sql){ ResultSet rs=null; try{ Connection conn=getOracle(); if(null==rs||rs.isClosed()){ Statement stmt=conn.createStatement(); rs=stmt.executeQuery(sql); } }catch(Exception e){ throw new RuntimeException(e); } return rs; } //oracle 的 PreparedStatement结果集 ResultSet public static ResultSet psexecuteQuery(){ ResultSet rs=null; PreparedStatement ps=null; try{ if(null==rs||rs.isClosed()){ rs=ps.executeQuery(); } }catch(Exception e){ throw new RuntimeException(e); } return rs; } //关闭连接PreparedStatement public static void close(Connection conn,PreparedStatement ps,ResultSet rs){ try{ if(null!=conn){ conn.close(); } }catch(Exception e){ throw new RuntimeException(e); } try{ if(null!=ps){ ps.close(); } }catch(Exception e){ throw new RuntimeException(e); } try{ if(null!=rs){ rs.close(); } }catch(Exception e){ throw new RuntimeException(e); } } //关闭连接Statement public static void close(Connection conn,Statement stmt,ResultSet rs){ try{ if(null!=conn){ conn.close(); } }catch(Exception e){ throw new RuntimeException(e); } try{ if(null!=stmt){ stmt.close(); } }catch(Exception e){ throw new RuntimeException(e); } try{ if(null!=rs){ rs.close(); } }catch(Exception e){ throw new RuntimeException(e); } } }
package com.abin.db.connection; import java