오라클 OJDBC6 설정은 아래페이지에서...
https://memory-develo.tistory.com/82
오라클 OJDBC6 객체 설명은 아래페이지에서..
https://memory-develo.tistory.com/83
오라클 사용되는 값
Statement
Statement 객체 이용한 조회, 추가, 수정, 삭제
회원정보 전체조회
JDBCModel.class
package com.model.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCModel {
// Statement 객체 사용
// 1. EMP 테이블 전체 회원정보 조회
public void empSelectAll() {
// 객체사용후 close하기위해 지역변수로 선언
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 오라클 드라이버를 사용하겠다는 의미
Class.forName("oracle.jdbc.driver.OracleDriver");
// 자신의 주소값에 scott 라는 아이디와 tiger 의 비밀번호로 접속함
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
stmt = conn.createStatement();
// EMP 테이블 전체조회하기위한 쿼리문
String query = "SELECT * FROM EMP";
//select문이기때문에 executeQuery으로사용
rs = stmt.executeQuery(query);
//값 뽑아오기위한 반복문
/*
* EMPNO NUMBER
ENAME VARCHAR2(10 BYTE)
JOB VARCHAR2(9 BYTE)
MGR NUMBER
HIREDATE DATE
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
* */
while(rs.next()) {
int empNo = rs.getInt("EMPNO");
String empName = rs.getString("ENAME");
String job = rs.getString("JOB");
int mgr = rs.getInt("MGR");
// Date import 대상은 util 패키지가아닌 sql 패키지 해야함
Date hireDate = rs.getDate("HIREDATE");
int sal = rs.getInt("SAL");
int comm = rs.getInt("COMM");
int deptNo = rs.getInt("DEPTNO");
// 출력
System.out.println(empNo + ", " + empName + ", " + job + ", " + mgr + ", " + hireDate + ", " + sal + ", " + comm
+ ", " + deptNo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 항상 사용후 무조건 닫아주자!
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
public class TestMain {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 회원정보 전체조회
model.empSelectAll();
}
}
출력
회원정보 추가
9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 으로추가 하겠습니다.
Employee.class
package com.model.vo;
import java.sql.Date;
public class Employee {
private int empNo;
private String empName;
private String job;
private int mgr;
private Date hireDate;
private int sal;
private int comm;
private int deptNo;
public Employee() {}
public Employee(int empNo, String job, int sal, int comm) {
super();
this.empNo = empNo;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public Employee(int empNo, String empName, String job, int mgr, int sal, int comm, int deptNo) {
super();
this.empNo = empNo;
this.empName = empName;
this.job = job;
this.mgr = mgr;
this.sal = sal;
this.comm = comm;
this.deptNo = deptNo;
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
@Override
public String toString() {
return "Employee [empNo=" + empNo + ", empName=" + empName + ", job=" + job + ", mgr=" + mgr + ", hireDate="
+ hireDate + ", sal=" + sal + ", comm=" + comm + ", deptNo=" + deptNo + "]";
}
}
JDBCModel.class
package com.model.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.model.vo.Employee;
public class JDBCModel {
// Statement 객체 사용
...
// 2. EMP 테이블 회원 추가
public void empMemberAdd(Employee emp) {
Connection conn = null;
Statement stmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
stmt = conn.createStatement();
// 매개변수로 들어온 emp 참조변수를 이용하여 회원정보 추가
// 그리고 아래와같이 입력할때 쿼리의 문자열 계열을 '' << 안에작성해야함 주의
String query = "INSERT INTO EMP " // 여기서 EMP_ 띄어쓰기안하면 EMLVALUES 이렇게 붙여진상태로 저장됨 .. 띄어쓰기필수
+ "VALUES(" + emp.getEmpNo() + ", '" + emp.getEmpName() + "', '" + emp.getJob() + "', "
+ emp.getMgr() + ", " + "SYSDATE" + ", " + emp.getSal() + ", " + emp.getComm()
+ ", " + emp.getDeptNo() + ")";
// executeUpdate의 반환 값은 0이다
// 만약 추가에 성공하면 1이라는 값반환 실패하면 0이라는값 반환
// 그리고 DML구문은 항상 COMMIT 작업을 해줘야 반영이된다 무조건필수 !
result = stmt.executeUpdate(query);
System.out.println(result + "개의 행을 추가하였습니다.");
if(result > 0) {
// 성공했을시
conn.commit();
}else {
// 실패 했을시
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
import com.model.vo.Employee;
public class TestMain {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 1. 회원정보 전체조회
// model.empSelectAll();
// 2. 회원정보 추가
// 9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 회원정보로 추가
Employee emp = new Employee(9000, "ERIC", "MANAGER", 8000, 3000, 90, 30);
model.empMemberAdd(emp);
}
}
출력
전체 정보 출력
회원정보 1명만 조회하기
회원번호 9000번 조회하기
JDBCModel.class
// 회원정보 1명 조회하기
public void empSelectOne(int emp_No) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
stmt = conn.createStatement();
String query = "SELECT * FROM EMP "
+ "WHERE EMPNO = " + emp_No;
rs = stmt.executeQuery(query);
while(rs.next()) {
int empNo = rs.getInt("EMPNO");
String empName = rs.getString("ENAME");
String job = rs.getString("JOB");
int mgr = rs.getInt("MGR");
Date hireDate = rs.getDate("HIREDATE");
int sal = rs.getInt("SAL");
int comm = rs.getInt("COMM");
int deptNo = rs.getInt("DEPTNO");
// 출력
System.out.println(empNo + ", " + empName + ", " + job + ", " + mgr + ", " + hireDate + ", " + sal + ", " + comm
+ ", " + deptNo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
import com.model.vo.Employee;
public class TestMain {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 1. 회원정보 전체조회
// model.empSelectAll();
// 2. 회원정보 추가
// 9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 회원정보로 추가
// Employee emp = new Employee(9000, "ERIC", "MANAGER", 8000, 3000, 90, 30);
// model.empMemberAdd(emp);
// 3. 회원정보 1명 조회
model.empSelectOne(9000);
}
}
출력
회원정보 1명 수정하기
회원번호 9000번인 직원 수정하기
JDBCModel.class
// 4. 회원정보 업데이트하기
public void empUpdate(Employee emp) {
Connection conn = null;
Statement stmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
stmt = conn.createStatement();
String query = "UPDATE EMP "
+ "SET JOB = '" + emp.getJob() + "', "
+ " SAL = " + emp.getSal() + ", "
+ " COMM = " + emp.getComm() + " "
+ "WHERE EMPNO = " + emp.getEmpNo();
result = stmt.executeUpdate(query);
System.out.println(result + "개의 행을 수정하였습니다.");
if(result > 0) {
conn.commit();
}else {
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
import com.model.vo.Employee;
public class TestMain {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 1. 회원정보 전체조회
// model.empSelectAll();
// 2. 회원정보 추가
// 9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 회원정보로 추가
// Employee emp = new Employee(9000, "ERIC", "MANAGER", 8000, 3000, 90, 30);
// model.empMemberAdd(emp);
// 3. 회원정보 1명 조회
// model.empSelectOne(9000);
// 4. 회원정보 1명 수정
Employee empModify = new Employee(9000, "ANALYST", 2000, 10);
model.empUpdate(empModify);
}
}
출력
전체 출력
회원정보 1명 삭제하기
회원번호 9000번인 회원 삭제하기
JDBCModel.class
// 5. 회원정보 1명삭제하기
public void empDelete(int empNo) {
Connection conn = null;
Statement stmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
stmt = conn.createStatement();
String query = "DELETE FROM EMP "
+ "WHERE EMPNO = " + empNo;
result = stmt.executeUpdate(query);
System.out.println(result + "개의 행이 삭제되었습니다.");
if(result > 0) {
conn.commit();
}else {
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
import com.model.vo.Employee;
public class TestMain {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 1. 회원정보 전체조회
// model.empSelectAll();
// 2. 회원정보 추가
// 9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 회원정보로 추가
// Employee emp = new Employee(9000, "ERIC", "MANAGER", 8000, 3000, 90, 30);
// model.empMemberAdd(emp);
// 3. 회원정보 1명 조회
// model.empSelectOne(9000);
// 4. 회원정보 1명 수정
// Employee empModify = new Employee(9000, "ANALYST", 2000, 10);
// model.empUpdate(empModify);
// 5. 회원정보 1명 삭제
model.empDelete(9000);
}
}
출력
전체 출력
PreparedStatement
결과값을 똑같기때문에 소스만 올림
Employee.class
package com.model.vo;
import java.sql.Date;
public class Employee {
private int empNo;
private String empName;
private String job;
private int mgr;
private Date hireDate;
private int sal;
private int comm;
private int deptNo;
public Employee() {}
public Employee(int empNo, String job, int sal, int comm) {
super();
this.empNo = empNo;
this.job = job;
this.sal = sal;
this.comm = comm;
}
public Employee(int empNo, String empName, String job, int mgr, int sal, int comm, int deptNo) {
super();
this.empNo = empNo;
this.empName = empName;
this.job = job;
this.mgr = mgr;
this.sal = sal;
this.comm = comm;
this.deptNo = deptNo;
}
public int getEmpNo() {
return empNo;
}
public void setEmpNo(int empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
@Override
public String toString() {
return "Employee [empNo=" + empNo + ", empName=" + empName + ", job=" + job + ", mgr=" + mgr + ", hireDate="
+ hireDate + ", sal=" + sal + ", comm=" + comm + ", deptNo=" + deptNo + "]";
}
}
JDBCModel2.class
package com.model.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.model.vo.Employee;
public class JDBCModel2 {
// PreparedStatement 객체 사용
// 1. EMP 테이블 전체 회원정보 조회
public void empSelectAll() {
// 객체사용후 close하기위해 지역변수로 선언
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 오라클 드라이버를 사용하겠다는 의미
Class.forName("oracle.jdbc.driver.OracleDriver");
// 자신의 주소값에 scott 라는 아이디와 tiger 의 비밀번호로 접속함
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
// EMP 테이블 전체조회하기위한 쿼리문
String query = "SELECT * FROM EMP";
pstmt = conn.prepareStatement(query);
//select문이기때문에 executeQuery으로사용
rs = pstmt.executeQuery(query);
//값 뽑아오기위한 반복문
/*
* EMPNO NUMBER
ENAME VARCHAR2(10 BYTE)
JOB VARCHAR2(9 BYTE)
MGR NUMBER
HIREDATE DATE
SAL NUMBER
COMM NUMBER
DEPTNO NUMBER
* */
while(rs.next()) {
int empNo = rs.getInt("EMPNO");
String empName = rs.getString("ENAME");
String job = rs.getString("JOB");
int mgr = rs.getInt("MGR");
// Date import 대상은 util 패키지가아닌 sql 패키지 해야함
Date hireDate = rs.getDate("HIREDATE");
int sal = rs.getInt("SAL");
int comm = rs.getInt("COMM");
int deptNo = rs.getInt("DEPTNO");
// 출력
System.out.println(empNo + ", " + empName + ", " + job + ", " + mgr + ", " + hireDate + ", " + sal + ", " + comm
+ ", " + deptNo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 항상 사용후 무조건 닫아주자!
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 2. EMP 테이블 회원 추가
public void empMemberAdd(Employee emp) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
// 매개변수로 들어온 emp 참조변수를 이용하여 회원정보 추가
// 그리고 아래와같이 입력할때 쿼리의 문자열 계열을 '' << 안에작성해야함 주의
// String query = "INSERT INTO EMP " // 여기서 EMP_ 띄어쓰기안하면 EMLVALUES 이렇게 붙여진상태로 저장됨 .. 띄어쓰기필수
// + "VALUES(" + emp.getEmpNo() + ", '" + emp.getEmpName() + "', '" + emp.getJob() + "', "
// + emp.getMgr() + ", " + "SYSDATE" + ", " + emp.getSal() + ", " + emp.getComm()
// + ", " + emp.getDeptNo() + ")";
String query = "INSERT INTO EMP"
+ "VALUES(?, ?, ?, ?, SYSDATE, ?, ?, ?)";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, emp.getEmpNo());
pstmt.setString(2, emp.getEmpName());
pstmt.setString(3, emp.getJob());
pstmt.setInt(4, emp.getMgr());
pstmt.setInt(5, emp.getSal());
pstmt.setInt(6, emp.getComm());
pstmt.setInt(7, emp.getDeptNo());
result = pstmt.executeUpdate();
System.out.println(result + "개의 행을 추가하였습니다.");
if(result > 0) {
// 성공했을시
conn.commit();
}else {
// 실패 했을시
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 3. 회원정보 1명 조회하기
public void empSelectOne(int emp_No) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "scott", "tiger");
String query = "SELECT * FROM EMP "
+ "WHERE EMPNO = ?";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, emp_No);
rs = pstmt.executeQuery();
while(rs.next()) {
int empNo = rs.getInt("EMPNO");
String empName = rs.getString("ENAME");
String job = rs.getString("JOB");
int mgr = rs.getInt("MGR");
Date hireDate = rs.getDate("HIREDATE");
int sal = rs.getInt("SAL");
int comm = rs.getInt("COMM");
int deptNo = rs.getInt("DEPTNO");
// 출력
System.out.println(empNo + ", " + empName + ", " + job + ", " + mgr + ", " + hireDate + ", " + sal + ", " + comm
+ ", " + deptNo);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 4. 회원정보 업데이트하기
public void empUpdate(Employee emp) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
String query = "UPDATE EMP "
+ "SET JOB = ?, SAL =?, COMM = ? "
+ "WHERE EMPNO = ?";
pstmt = conn.prepareStatement(query);
pstmt.setString(1, emp.getJob());
pstmt.setInt(2, emp.getSal());
pstmt.setInt(3, emp.getComm());
pstmt.setInt(4, emp.getEmpNo());
result = pstmt.executeUpdate();
System.out.println(result + "개의 행을 수정하였습니다.");
if(result > 0) {
conn.commit();
}else {
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 5. 회원정보 1명삭제하기
public void empDelete(int empNo) {
Connection conn = null;
PreparedStatement pstmt = null;
int result = 0;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "scott", "tiger");
String query = "DELETE FROM EMP "
+ "WHERE EMPNO = ?";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1, empNo);
result = pstmt.executeUpdate(query);
System.out.println(result + "개의 행이 삭제되었습니다.");
if(result > 0) {
conn.commit();
}else {
conn.rollback();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestMain.class
package com.controller;
import com.model.dao.JDBCModel;
import com.model.vo.Employee;
public class TestMain2 {
public static void main(String[] args) {
JDBCModel model = new JDBCModel();
// 1. 회원정보 전체조회
model.empSelectAll();
// 2. 회원정보 추가
// 9000, "ERIC", "MANAGER", 8000, 오늘날짜, 3000, 90, 30 회원정보로 추가
// Employee emp = new Employee(9000, "ERIC", "MANAGER", 8000, 3000, 90, 30);
// model.empMemberAdd(emp);
// 3. 회원정보 1명 조회
// model.empSelectOne(9000);
// 4. 회원정보 1명 수정
// Employee empModify = new Employee(9000, "ANALYST", 2000, 10);
// model.empUpdate(empModify);
// 5. 회원정보 1명 삭제
// model.empDelete(9000);
}
}
'프로그래밍 > JAVA' 카테고리의 다른 글
JAVA - Oracle JDBC(회원관리프로그램) 예제 (1) | 2021.07.14 |
---|---|
JAVA - Oracle JDBC(도서관리프로그램) 예제 (0) | 2021.07.13 |
JAVA - Oracle JDBC (0) | 2021.07.12 |
JAVA - Oracle JDBC 다운, 적용 (0) | 2021.07.12 |
JAVA - GUI(Graphic User Interface) (0) | 2021.06.14 |