control Y

[오라클/자바] 이클립스(eclipse) 데이터 베이스 연동 본문

KH정보교육원/SQL

[오라클/자바] 이클립스(eclipse) 데이터 베이스 연동

ControlY 2023. 6. 27. 12:30

1. PROJECT > 우클릭 > Java Build Path > Add External JARs 경로 설정

 

2. 연결

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

public class ConnectDatabase {
	public static Connection makeConnection(String id, String password) {
		String url ="jdbc:oracle:thin:@127.0.0.1:1521/xepdb1";
		Connection con = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("드라이버 적재 성공");
			
			con = DriverManager.getConnection(url, id, password);
			System.out.println("데이터베이스 연결 성공");
		}catch(ClassNotFoundException e) {
			System.out.println("드라이버를 찾을 수 없습니다.");
			e.printStackTrace();
		}catch(SQLException e) {
			System.out.println("연결에 실패하였습니다");
			e.printStackTrace();
		}
		return con;
	}
	public static void main(String arg[]) throws SQLException{
		Connection con = makeConnection("javauser", "java1234");
		con.close();
	}
}

 

반응형