Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter01 : connextion / ConnectionMain 본문

JAVA/chapter25_db_connection

chapter01 : connextion / ConnectionMain

GAWON 2023. 5. 31. 09:25
package org.joonzis.ex;

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

/*
 * Oracle jdbc를 프로젝트에 등록
 * 1.프로젝트 우클릭
 * 2.Build Path - Configure Build Path
 * 3.Libraries 탭
 * 4.Add External JARs
 * 5.ojdbc6.jar 파일을 찾아서 선택
 */
public class Ex01_connextion {
public static void main(String[] args) {
	
	// 1. Connection 인스턴스
	Connection conn = null;
	  try {
	         // 2. Oracle jdbc 드라이브 로드      Class.forName("드라이버 종류");
	         Class.forName("oracle.jdbc.driver.OracleDriver");   // 외우자
	        // Class.forName("com.mysql.jdbc.Driver");            // MySQL
	        // Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");   // Ms-SQL
	         
	         // 3. 접속 정보 생성
	         String user = "scott";
	         String password = "tiger";
	         String url = "jdbc:oracle:thin:@localhost:1521:xe";   // 외우자
	         /*
	          * Oracle   :   jdbc:oracle://localhost:1521:SID
	          * MySQL   :   jdbc:mysql://localhost:3306:DB명
	          * Ms-SQL   :   jdbc:Microsoft:sqlserver://localhost:1433;databasename=DB명
	          */
	         
	         // 4. DriverManager : 자바를 오라클 jdbc에 연결 시켜주는 클래스
	         conn = DriverManager.getConnection(url, user, password);
	         
	         // 5. 연결 확인 메세지
	         System.out.println("DB 연결 성공");
	         
	         // 6.접속 종료
	         conn.close();
	      } catch (ClassNotFoundException e) {
	         System.out.println("jdbc 드라이버 오류");
	         
	      } catch (SQLException e) {
	    	  System.out.println("DB 연결 오류");
		}
	  		  
    }
}
package org.joonzis.ex2;

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


public class ConnectionMain {
	public static void main(String[] args) {
		Connection conn = null;

		try {
			conn = DBConnection.getConnection();
			System.out.println("DB 연결 성공");
			conn.close();
		} catch (ClassNotFoundException e) {
			System.out.println("jdbc 드라이버 오류");

		} catch (SQLException e) {
			System.out.println("DB 연결 오류");
		}
	}
}

'JAVA > chapter25_db_connection' 카테고리의 다른 글

chapter02 : DBConnection  (0) 2023.05.31