맥북에서 오라클과 이클립스 연동하기
1.이클립스(Eclipse) 준비하기
JDK와 이클립스를 설치하고 환경변수 설정과 이클립스 환경 설정을 합니다.
맥에서 자바 환경변수 설정 & 이클립스 오류 해결맥에서 자바 환경변수 설정하기
1.터미널에서 JDK 버전 확인하기 2.vi에디터 열기 3.환경변수 설정하기 4.자바 버전 확인하기
맥에서 이클립스 실행이 되지 않을 때 해결하는 방법
이클립스에서 코딩용 글꼴 사용하기...
2.오라클(Oracle) 준비하기
Oracle 11g와 SQL Developer를 설치합니다.
[Mac]오라클 설치하기_docker맥에서 사용할 수 있게 먼저 Docker Hub를 다운로드한다. 다운로드가 완료되면 Docker를 실행하고 로그인한다. Docker Desktop is starting이라는 문구가Docker Desktop is running이라는 문구로 바뀌면 성공적으로 실행된 것이다....
3.Tomcat 준비하기
Tomcat을 설치에 사용할 Homebrew를 먼저 다운로드 해줍니다.
[Mac]Homebrew 설치하기공식사이트에서 아래 코드를 붙여넣기 하세요.
Homebrew 공식 사이트 링크 : https://brew.sh/index_ko
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
패스워드를 입력하세요.
패스워드가 화면에 표시되지 않으니 오타에 주의하세요....
1.brew 업데이트를 실행한다.
dahyechoi@Dahyeui-MacBookPro ~ %brew update
터미널보기
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
https://github.com/Homebrew/brew#donations
Updated 1 tap (homebrew/core).
No changes to formulae.
https://github.com/Homebrew/brew#donations
Updated 1 tap (homebrew/core).
No changes to formulae.
2.Homebrew에서 tomact을 검색해준다.
dahyechoi@Dahyeui-MacBookPro ~ %brew search tomcat
터미널보기
==> Formulae
tomcat tomcat-native tomcat@7 tomcat@8 tomcat@9
tomcat tomcat-native tomcat@7 tomcat@8 tomcat@9
3.tomcat을 설치해준다.
dahyechoi@Dahyeui-MacBookPro ~ % brew install tomcat
터미널보기
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/16.0.1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:55c120ab6b
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/tomcat/manifests/10.0.8
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/tomcat/blobs/sha256:d879b4a70b6
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Installing dependencies for tomcat: openjdk
==> Installing tomcat dependency: openjdk
==> Pouring openjdk–16.0.1.big_sur.bottle.tar.gz
? /usr/local/Cellar/openjdk/16.0.1: 617 files, 330MB
==> Installing tomcat
==> Pouring tomcat–10.0.8.all.bottle.tar.gz
==> Caveats
To have launchd start tomcat now and restart at login:
brew services start tomcat
Or, if you don’t want/need a background service you can just run:
catalina run
==> Summary
? /usr/local/Cellar/tomcat/10.0.8: 635 files, 15.2MB
==> Caveats
==> tomcat
To have launchd start tomcat now and restart at login:
brew services start tomcat
Or, if you don’t want/need a background service you can just run:
catalina run
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:55c120ab6b
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/tomcat/manifests/10.0.8
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/tomcat/blobs/sha256:d879b4a70b6
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Installing dependencies for tomcat: openjdk
==> Installing tomcat dependency: openjdk
==> Pouring openjdk–16.0.1.big_sur.bottle.tar.gz
? /usr/local/Cellar/openjdk/16.0.1: 617 files, 330MB
==> Installing tomcat
==> Pouring tomcat–10.0.8.all.bottle.tar.gz
==> Caveats
To have launchd start tomcat now and restart at login:
brew services start tomcat
Or, if you don’t want/need a background service you can just run:
catalina run
==> Summary
? /usr/local/Cellar/tomcat/10.0.8: 635 files, 15.2MB
==> Caveats
==> tomcat
To have launchd start tomcat now and restart at login:
brew services start tomcat
Or, if you don’t want/need a background service you can just run:
catalina run
4.tomcat 실행하기
다운이 완료되면 자신의 경로로 /usr/local/Cellar/tomcat/10.0.8/bin까지 접속해 실행합니다.
위 경로까지 가서 bin파일을 클릭하고 ctrl+shift+c를 누르면 경로가 복사됩니다.
dahyechoi@Dahyeui-MacBookPro ~ % cd /usr/local/Cellar/tomcat/10.0.8/bin
dahyechoi@Dahyeui-MacBookPro bin % ./catalina start
터미널보기
Using CATALINA_BASE: /usr/local/Cellar/tomcat/10.0.8/libexec
Using CATALINA_HOME: /usr/local/Cellar/tomcat/10.0.8/libexec
Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/10.0.8/libexec/temp
Using JRE_HOME: /usr/local/opt/openjdk
Using CLASSPATH: /usr/local/Cellar/tomcat/10.0.8/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/10.0.8/libexec/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
dahyechoi@Dahyeui-MacBookPro bin %
Using CATALINA_HOME: /usr/local/Cellar/tomcat/10.0.8/libexec
Using CATALINA_TMPDIR: /usr/local/Cellar/tomcat/10.0.8/libexec/temp
Using JRE_HOME: /usr/local/opt/openjdk
Using CLASSPATH: /usr/local/Cellar/tomcat/10.0.8/libexec/bin/bootstrap.jar:/usr/local/Cellar/tomcat/10.0.8/libexec/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
dahyechoi@Dahyeui-MacBookPro bin %
4.이클립스와 오라클 연결하기
Oracle 11g : ojdbc6.jar
Oracle 10g : ojdbc5.jar
- 오라클 버전에 맞는 JDBC 드라이버 프로그램을 준비해주세요.
- 이클립스 프로젝트 파일 아래에 library 폴더를 하나 만들어서 다운받은 jar파일을 끌어다 놓습니다.
- project에서 마우스 오른쪽 버튼을 클릭해서 properties창을 열어줍니다.
- 오른쪽 탭에서 Java Build Path 설정에 들어옵니다.
- 상단에 있는 library 탭 선택합니다.
- Add JARs… 버튼 클릭해서 Jar Selection 창을 띄웁니다.
- 프로젝트 경로 안에 있는 odbc6.jar를 선택한 후에 apply and close를 클릭합니다.
오라클 DB를 이용해서 자바에서 코딩해보기
예제1. 직원번호, 이름
package ex01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB01 {
private static String driverName = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
private static String dbuid = "hr";
private static String dbpwd = "1234";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,dbuid,dbpwd);
Statement stmt = conn.createStatement();
String sql = "SELECT * FROM EMPLOYEES";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.print(rs.getInt("Employee_ID")+ " ");
System.out.print(rs.getString("FIRST_NAME") +" ");
System.out.println(rs.getString("LAST_NAME"));
}
rs.close();
stmt.close();
conn.close();
}
}
예제2. 직원번호, 이름, 월급, 연봉, 부서번호
package ex01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB02 {
private static String driver = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
privatestaticString dbuid = "hr";
privatestaticString dbpwd = "1234";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbuid,dbpwd);
Statement stmt = conn.createStatement();
String sql = "";
sql += "select employee_id 번호,";
sql += " first_name || ' ' || last_name 이름,";
sql += " salary 월급, ";
sql += " (salary*12 + salary*commission_pct) 연봉,";
sql += " department_id 부서번호";
sql += " from employees";
sql += " where department_id = 60";
System.out.println("sql:"+sql);
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
int num = rs.getInt(1);
String name = rs.getString(2);
double sal = rs.getDouble(3);
double ysal = rs.getDouble(4);
int deptid = rs.getInt(5);
String fmt = "%d %s %.2f %.2f %d";
String msg = String.format(fmt, num, name, sal, ysal, deptid);
System.out.println(msg);
}
rs.close();
stmt.close();
conn.close();
}
}
예제3. 부서이름, 직원이름
package ex01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB03 {
private static String driver = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
privatestaticString dbuid = "hr";
privatestaticString dbpwd = "1234";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbuid,dbpwd);
Statement stmt = conn.createStatement();
Stringsql = "";
sql += "select d.department_name dname,";
sql += " e.first_name || ' ' || e.last_name ename";
sql += " from departments d, employees e";
sql += " where d.department_id = e.department_id(+)";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
String dname = rs.getString("dname");
String ename = rs.getString("ename");
if(ename.equals(" ")) ename = "직원없음";
System.out.println(dname+","+ ename);
}
rs.close();
stmt.close();
conn.close();
}
}
예제4. 직원번호, 직원이름, 전화번호, 부서번호
package ex01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class TestDB04 {
private static String driver = "oracle.jdbc.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:xe";
privatestaticString dbuid = "hr";
privatestaticString dbpwd = "1234";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Scannerin = new Scanner(System.in);
System.out.println("부서번호를 입력하세요(ex)50,60,90)");
String line = in.nextLine();
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,dbuid,dbpwd);
Statement stmt = conn.createStatement();
Stringsql = "";
sql += "select d.department_name dname,";
sql += " e.first_name || ' ' || e.last_name ename";
sql += " from departments d, employees e";
sql += " where d.department_id = e.department_id(+)";
sql += " and d.department_id in(" + line + ")";
sql += "order by d.department_id asc";
ResultSet rs = stmt.executeQuery(sql);
System.out.println("부서명 직원이름");
System.out.println("-----------------------------------");
while(rs.next()) {
String dname = rs.getString("dname");
String ename = rs.getString("ename");
if(ename.equals(" ")) ename = "직원없음";
System.out.println(dname+","+ ename);
}
rs.close();
stmt.close();
conn.close();
}
}
자바에서 SQL문 적을 때 주의할 점
- from, where문 앞에 한칸 공백
- 세미콜론(;) 사용금지
- 한글로 별명(얼라이언스) 사용하지 말것