Oracle連接數據庫封裝類

  1. package com.jwgl.util;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9.   
  10.   
  11. /** 
  12.  * 封裝Oracle數據庫常用操作 
  13.  *  
  14.  * @author 劉鵬 
  15.  *  
  16.  */  
  17. public class DbUtil {  
  18.     /** 
  19.      * 取得Connection 
  20.      *  
  21.      * @return 
  22.      */  
  23.     public static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";  
  24.     public static final String DBURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  25.     public static final String DBUSER="kong";  
  26.     public static final String DBPASS="kong";  
  27.       
  28.       
  29.     public static Connection conn = null;  
  30.     public static Statement pstm = null;  
  31.     public static PreparedStatement pstmt = null;  
  32.     public static ResultSet rs = null;  
  33.       
  34.       
  35.     /** 
  36.      * 取得數據庫連接 
  37.      * @return 
  38.      */  
  39.     public static Connection getConnection(){  
  40.           
  41.   
  42.         try{  
  43.             Class.forName(DBDRIVER);  
  44.               
  45.         }catch(ClassNotFoundException e){  
  46.             e.printStackTrace();  
  47.         }  
  48.         try{  
  49.             conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);  
  50.         }catch(SQLException e){  
  51.             e.printStackTrace();  
  52.         }  
  53.         return conn;  
  54.     }  
  55.       
  56.       
  57.       
  58.       
  59.     /** 
  60.      * 數據庫操作Statement 
  61.      * @return 
  62.      */  
  63.     public static Statement getStatement(Connection con){  
  64.         //con = DbUtil.getConnection();  
  65.         try {  
  66.             if (con != null) {  
  67.                 pstm = con.createStatement();  
  68.             }  
  69.         } catch (SQLException e) {  
  70.             // TODO Auto-generated catch block  
  71.             e.printStackTrace();  
  72.         }  
  73.         return pstm;  
  74.     }  
  75.   
  76.       
  77.       
  78.     /** 
  79.      * 數據庫操作PreparedStatement 
  80.      * @return 
  81.      */  
  82.     public static PreparedStatement getPreparedStatement(Connection con,String sql) {  
  83.         //con = DbUtil.getConnection();  
  84.         if (con != null) {  
  85.             try {  
  86.                 pstmt = conn.prepareStatement(sql);  
  87.             } catch (SQLException e) {  
  88.                   
  89.                 e.printStackTrace();  
  90.             }  
  91.         }  
  92.         return pstmt;  
  93.     }  
  94.       
  95. /*    此方法不需要寫,會出現丟失In或者Out參數 
  96.     public static ResultSet getResultSet(Connection conn,String sql){ 
  97.         pstmt = DbUtil.getPreparedStatement(conn, sql); 
  98.         try { 
  99.             rs = pstmt.executeQuery(); 
  100.         } catch (SQLException e) { 
  101.              
  102.             e.printStackTrace(); 
  103.         } 
  104.         return rs; 
  105.     } 
  106. */    
  107.       
  108.     /** 
  109.      * 關閉數據庫連接 
  110.      * @param conn 
  111.      */  
  112.     public static void close(Connection conn){  
  113.         if(conn!=null){  
  114.             try{  
  115.                 conn.close();  
  116.             }catch(SQLException e){  
  117.                 e.printStackTrace();  
  118.             }  
  119.         }  
  120.     }  
  121.       
  122.       
  123.     /** 
  124.      * 關閉數據庫操作Statement 
  125.      * @param stmt 
  126.      */  
  127.     public static void close(Statement stmt){  
  128.         if(stmt!=null){  
  129.             try{  
  130.                 stmt.close();  
  131.             }catch(SQLException e){  
  132.                 e.printStackTrace();  
  133.                 }  
  134.         }  
  135.     }  
  136.       
  137.       
  138.     /** 
  139.      * 關閉數據庫操作PreparedStatement 
  140.      * @param pstmt 
  141.      */  
  142.     public static void close(PreparedStatement pstmt){  
  143.         if(pstmt!=null){  
  144.             try{  
  145.                 pstmt.close();  
  146.             }catch(SQLException e){  
  147.                 e.printStackTrace();  
  148.             }  
  149.         }  
  150.     }  
  151.       
  152.       
  153.     /** 
  154.      * 關閉數據庫查詢 
  155.      * @param rs 
  156.      */  
  157.     public static void close(ResultSet rs){  
  158.         if(rs!=null){  
  159.             try{  
  160.                 rs.close();  
  161.             }catch(SQLException e){  
  162.                 e.printStackTrace();  
  163.             }  
  164.         }  
  165.     }  
  166.       
  167.       
  168.     /** 
  169.      * 主方法用於測試 
  170.      * @param args 
  171.      */  
  172.     public static void main(String[] args) {  
  173.           
  174.         System.out.println(DbUtil.getConnection());  
  175.     }  
  176.   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章