Oracle Cloud 19c, Java Project 연동하기 [도레 코딩]

환경 : Oracle Cloud 19c, Java 11, SQLDeveloper

 

먼저, SQLDeveloper로 Oracle Cloud에 연결이 완료된 상태이여야한다.

 

JDBC 설정

다음과 같이

Modulpath -> Add Exteranal JARs... -> "자신 PC의 OJDC(버전).jar를 선택해준다.

 

Java Code

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


public class Conn {
	final static String DB_URL= "jdbc:oracle:thin:@orcl_high?TNS_ADMIN=C:/Users/lancelot/instantclient_19_13/network/admin";
    //final static String DB_URL= "jdbc:oracle:thin:@전자지갑 이름?TNS_ADMIN=instantclient 경로";
	final static String DB_USER = "계정";
	final static String DB_PASSWORD = "비밀번호";
	public static void main(String[] args) throws SQLException {
		Statement st = null; //DB와 소통하는 통로
		ResultSet rs = null; //결과 받아서 처리할때
		
		 try {
	            Class.forName("oracle.jdbc.driver.OracleDriver");
	            System.out.println("driver load 성공!");
	            Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
	            System.out.println("DB 연결 성공!");
	            
                //SQL문
	            String sql = "select * from POS_TABLE";
	            st = conn.createStatement();
	            rs = st.executeQuery(sql);
	            
	            while(rs.next()) {

	            	String menu = rs.getString("MENU");
	            	System.out.println(menu);
	            }
	        } catch (ClassNotFoundException e) {
	            System.out.println("driver load 실패!");
	            e.printStackTrace();
	        }
	}
}

각각 URL, USER, PASSWORD 정보를 입력

=> 성공