본문 바로가기
JAVA/(MVC패턴)JDBCTest

자바(JDBC) mvc패턴01. JDBC Utill패키지 만들기

by 미눅스[멘토] 2023. 7. 8.
728x90

jdbc util 클래스

package kr.or.ddit.utill;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

// JDBC드라이버를 로딩하고 Connection객체를 생성하여 반환하는 메서드로 구성된 class 만들기
public class DBUtil {
	
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버 로딩 실패~~~");
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		try {
			return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","pc21","java");
			
			
		} catch (SQLException e) {
			System.out.println("오라클 연결 실패~~~");
			return null;
		}
	}
	
	
	
}

 

 

jdbc util2 클래스

package kr.or.ddit.utill;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

// JDBC드라이버를 로딩하고 Connection객체를 생성하여 반환하는 메서드로 구성된 class 만들기
public class DBUtil2 {
	private static Properties prop;	//properties객체 변수 선언
	
	
	
	
	static {
		prop = new Properties();	//Properties객체 생성
		File f = new File("res/kr/or.ddit/config/dbinfo.properties");
		FileInputStream fin = null;
		
		
		
		try {
			
			fin = new FileInputStream(f);
			prop.load(fin);
			
//			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName(prop.getProperty("driver"));
			
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버 로딩 실패~~~");
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			System.out.println("드라이버 로딩 실패~~~");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("드라이버 로딩 실패~~~");
			e.printStackTrace();
		}finally {
			if(fin != null) try {fin.close();}catch (IOException e) {
			}
		}
	}
	
	public static Connection getConnection() {
		try {
//			return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","pc21","java");
			
			return DriverManager.getConnection(
					prop.getProperty("url"),
					prop.getProperty("user"),
					prop.getProperty("pass")
					);
					
			
		} catch (SQLException e) {
			System.out.println("오라클 연결 실패~~~");
			return null;
		}
	}
	
	
	
}

 

 

jdbc util3 클래스

package kr.or.ddit.utill;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ResourceBundle;

// JDBC드라이버를 로딩하고 Connection객체를 생성하여 반환하는 메서드로 구성된 class 만들기
public class DBUtil3 {
	private static ResourceBundle bundle;	//ResourceBundle객체 변수 선언
	
	static {
		//객체 생성
		bundle = ResourceBundle.getBundle("kr.or.ddit.config.dbinfo");
		
		
		
		try {
//			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName(bundle.getString("driver"));
		} catch (ClassNotFoundException e) {
			System.out.println("드라이버 로딩 실패~~~");
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		try {
//			return DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","pc21","java");
			return DriverManager.getConnection(
					bundle.getString("url"),
					bundle.getString("user"),
					bundle.getString("pass")
					);
			
		} catch (SQLException e) {
			System.out.println("오라클 연결 실패~~~");
			return null;
		}
	}
	
	
	
}