Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

꺄르륵

[Java] DBUtill - jdbc, oracle 연동 방법 - II 본문

프로그래밍/Java

[Java] DBUtill - jdbc, oracle 연동 방법 - II

Teddy. 2019. 10. 21. 17:31
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;
		}
	}



}