JDBC—Oracle

Oracle JDBC工具类:

 1 package com.foster.utils;

 2 

 3 import java.sql.Connection;

 4 import java.sql.DriverManager;

 5 import java.sql.ResultSet;

 6 import java.sql.SQLException;

 7 import java.sql.Statement;

 8 

 9 public final class JDBCUtils {

10     private JDBCUtils(){

11         

12     }

13     

14     static{

15         try {

16             Class.forName("oracle.jdbc.driver.OracleDriver");

17         } catch (ClassNotFoundException e) {

18             throw new ExceptionInInitializerError(e);

19         }

20     }

21     

22     public static Connection getConnection(String host, String SID,String user,String password) throws SQLException{

23         Connection conn=null;

24         if((host!=null) && (!"".equals(host))

25         && (SID!=null) && (!"".equals(SID))

26         && (user!=null) && (!"".equals(user))

27         && (password!=null) && (!"".equals(password))){

28             String url="jdbc:oracle:thin:@//"+host+":1521/"+SID;

29             conn=DriverManager.getConnection(url, user, password);

30         }        

31         return conn;

32     }

33     

34     public static void free(ResultSet rs,Statement st,Connection conn){

35         try{

36             if(rs!=null)

37                 rs.close();

38         }catch(SQLException e){

39             e.printStackTrace();

40         }finally{

41             try{

42                 if(st!=null)

43                     st.close();

44             }catch(SQLException e){

45                 e.printStackTrace();

46             }finally{

47                 if(conn!=null)

48                     try {

49                         conn.close();

50                     } catch (SQLException e) {

51                         e.printStackTrace();

52                     }

53             }

54         }

55     }

56 }

 

你可能感兴趣的:(oracle)