Spring

스프링 - 오라클 데이터베이스 연동

마루설아 2025. 5. 18. 19:09

여기서는 Oracle의 무료 경량 버전 11g XE, Oracle SQL Developer 24.3.1 (JDK 17 포함)을 사용한다.

 

 

Oracle 11g Xe 설치 (출처 : 남가람북스)

https://drive.google.com/drive/u/0/folders/122FDfnySpfuaBEeoIQU0EOa4GRZo5N6D

 

오라클11gxe - Google Drive

이 브라우저 버전은 더 이상 지원되지 않습니다. 지원되는 브라우저로 업그레이드하세요. 닫기

drive.google.com

 

 

 

Oracle SQL Developer 설치

https://www.oracle.com/database/sqldeveloper/technologies/download/

 

Oracle SQL Developer Downloads

This archive. will work on a 32 or 64 bit Windows OS. The bit level of the JDK you install will determine if it runs as a 32 or 64 bit application. This download does not include the required Oracle Java JDK. You will need to install it if it's not already

www.oracle.com

 

 

SQL Developer가 설치되면 아래 정보를 입력해 접속한다.

 

 

사용자 생성과 권한 부여를 해준다.

CREATE USER maru IDENTIFIED BY 1234
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;

GRANT CONNECT, DBA TO maru;

 

 

* Tomcat과 오라클 웹환경의 Port 중복 (8080) 문제를 해결

오라클의 HTTP Port를 변경한다.

-- 8080인 경우 아래 Exec
SELECT DBMS_XDB.GETHTTPPORT() FROM DUAL;

-- 프로시저 실행 후 재확인
EXEC DBMS_XDB.SETHTTPPORT(9090);

 

 

pom.xml에 Oracle JDBC 의존성 추가

이후 프로젝트 우클릭 > Maven > Update Project

...    

    <dependency>
        <groupId>com.oracle.ojdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>19.3.0.0</version>
    </dependency>
    
...

 

 

 

Oracle 접속 테스트를 위한 Java 테스트 클래스 생성

package com.myspring.persistence;

import static org.junit.Assert.fail;

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

import org.junit.Test;

import lombok.extern.log4j.Log4j;

@Log4j
public class JDBCTests {
	
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testConnection() {
		try (Connection con = 
				DriverManager.getConnection(
						"jdbc:oracle:thin:@localhost:1521:XE",
						"maru",
						"1234")){
			log.info(con);
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
}

 

 

Run As > Junit Test

아래 로그 출력이 된다면 연결 성공