使用硬連接Oracle數據庫

使用硬連接Oracle數據庫

部分代碼

import java.util.*;
import java.sql.*;
import java.io.*;

public class DBConBean {
  private String jdbcdriver = new String();
  private String url = new String();
  private String user = new String();
  private String password = new String();
  private Connection conn = null;

  public DBConBean(){
    getConnection();
  }

  private void getConnection(){
//從properties文件讀取預設的數據庫相關信息
    Properties prop = new Properties();
    try{
      InputStream is = getClass().getResourceAsStream("/database-conf.properties");
      prop.load(is);
      if(is!=null)is.close();
    }catch (IOException e){
      System.out.print("[DBConnection]打開文件出錯");
    }
    jdbcdriver = prop.getProperty("jdbcdriver");
    url = prop.getProperty("url");
    user = prop.getProperty("user");
    password = prop.getProperty("password");
    try{
      Class.forName(jdbcdriver);
    }catch(ClassNotFoundException e){
    }
//產生連接
    try{
      conn = DriverManager.getConnection(url,user,password);
    }catch(SQLException e){
    }
  }

  public Connection getConn(){
//返回連接的接口
    return conn;
  }

  public void close() throws SQLException{
    if(conn!=null) conn.close();

  }
}

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