package utill;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/*
* 방법1) 외부에 만들어진 properties파일을 읽어와 Properties객체로 처리하기
* 요즘은 잘 사용하지 않는 방식이다.
*
*/
public class DBUtill2 {
static Properties prop; // Properties 객체 변수 선언
static{
prop = new Properties(); // 객체 생성
// 읽어올 파일 정보를 갖는 File 객체를 생성한다
File f = new File("resource/db.properties");
try {
// db.properties 파일의 내용을 읽어와 Properties객체 변수에 저장한다.
FileInputStream fin = new FileInputStream(f);
prop.load(fin);
// Properties 객체의 내용을 가져와 드라이버 로딩명령에 세팅한다.
Class.forName(prop.getProperty("driver"));
} catch (ClassNotFoundException e) {
System.out.println("diver loading fail");
e.printStackTrace();
} catch (IOException e) {
System.out.println("file not found or I/O error");
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
System.out.println("Oracle 접속 성공");
return DriverManager.getConnection(
prop.getProperty("url"),
prop.getProperty("user"),
prop.getProperty("password"));
} catch (SQLException e) {
System.out.println(" ########### Oracle Connected FAIL ###########");
return null;
}
}
}