java串連oracle資料庫———DBUtil類(韓順平視頻裡的例子)

來源:互聯網
上載者:User

DBUtil類(註:看韓老師視頻時,有時會用到DBUtil類,剛開始不知道它是什麼。後來,漸漸覺得應該是一個串連資料庫用到的工具類,這是我自己從上次SqlHelper類裡抽取出來的DBUtil類。只是供大家參考,不是韓老師視頻裡的DBUtil類源碼

package com.xk.util;


import java.io.IOException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil
{
    //定義變數
    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 Properties  pp = null;
//    private static FileInputStream fis = null;
    private static InputStream fis = null;
    //載入驅動,只需要一次,用靜態代碼塊
    static
    {
        try
        {
            //從dbinfo.properties
            pp = new Properties();
//            fis = new FileInputStream("dbinfo.properties");
            
            fis = DBUtil.class.getClassLoader().getResourceAsStream("dbinfo.properties"); 
            pp.load(fis);
            url = pp.getProperty("url");
            username = pp.getProperty("username");
            driver = pp.getProperty("driver");
            passwd = pp.getProperty("passwd");
            
            //1.載入驅動
            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;
        }
    
    //關閉資源
    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檔案

url = jdbc:oracle:thin:@localhost:1521:orcl
username = root
driver = oracle.jdbc.driver.OracleDriver
passwd = root


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.