Common
- ํ๋ก์ ํธ ๋ด์์ ๊ณตํต์ผ๋ก ์ฌ์ฉํ ๋ฉ์๋ ๊ตฌํ
package com.kh.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Common {
final static String ORACLE_URL = "jdbc:oracle:thin:@localhost:1521:xe";
final static String ORACLE_ID = "SCOTT";
final static String ORACLE_PWD = "TIGER";
final static String ORACLE_DRV = "oracle.jdbc.driver.OracleDriver";
public static Connection getConnection() {
Connection conn = null; // Connection์ ๋ฐ๋์ ์์ธ์ฒ๋ฆฌ ํด์ค์ผ ํจ
try {
Class.forName(ORACLE_DRV); // ์ค๋ผํด ์ฐ๊ฒฐํ๊ธฐ ์ํด ๋๋ผ์ด๋ธ ๋ก๋ฉ
conn = DriverManager.getConnection(ORACLE_URL, ORACLE_ID, ORACLE_PWD); // ์๋ฐ-์ค๋ผํด ์ฐ๊ฒฐ ์ป๊ธฐ
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn) {
try {
if(conn != null && !conn.isClosed()) {
conn.close();
System.out.println("Connection ํด์ ์ฑ๊ณต");
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void close(Statement stmt) {
try {
if(stmt != null && !stmt.isClosed()) {
stmt.close();
System.out.println("Connection ํด์ ์ฑ๊ณต");
}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void close(ResultSet rSet) {
try {
if(rSet != null && !rSet.isClosed()) {
rSet.close();
System.out.println("Connection ํด์ ์ฑ๊ณต");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
EmpDAO
- ๊ฐ ์ฟผ๋ฆฌ๋ฌธ ์คํ์ํฌ ๋ฉ์๋ ๊ตฌํ
package com.kh.jdbc.dao;
import com.kh.jdbc.util.Common;
import com.kh.jdbc.vo.EmpVO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmpDAO {
Connection conn = null; // ์๋ฐ์ ์ค๋ผํด์ ๋ํ ์ฐ๊ฒฐ ์ค์
Statement stmt = null; // sql๋ฌธ์ ์ํํ๊ธฐ ์ํ ๊ฐ์ฒด
ResultSet rs = null; // statement ๋์์ ๋ํ ๊ฒฐ๊ณผ๋ก ์ ๋ฌ๋๋ DB์ ๋ด์ฉ..!
Scanner sc = new Scanner(System.in);
public List<EmpVO> empSelect() {
List<EmpVO> list = new ArrayList<>(); // ๋ฐํํ ๋ฆฌ์คํธ๋ฅผ ์ํด list ๊ฐ์ฒด ์์ฑ
try {
conn = Common.getConnection();
stmt = conn.createStatement();
String sql = "SELECT * FROM EMP";
rs = stmt.executeQuery(sql); // SELECT ๋ฌธ๊ณผ ๊ฐ์ด ์ฌ๋ฌ ๊ฐ์ ๋ ์ฝ๋(ํ)๋ก ๊ฒฐ๊ณผ๊ฐ ๋ฐํ ๋ ๋ ์ฌ์ฉ
while (rs.next()) { // ์ฝ์ ํ์ด ์์ผ๋ฉด ์ฐธ
int no = rs.getInt("EMPNO");
String name = rs.getString("ENAME");
String job = rs.getString("JOB");
int mgr = rs.getInt("MGR");
Date date = rs.getDate("HIREDATE");
double sal = rs.getDouble("SAL");
double comm = rs.getDouble("COMM");
int deptNo = rs.getInt("DEPTNO");
EmpVO vo = new EmpVO(no, name, job, mgr, date, sal, comm, deptNo); // ํ๋์ ํ์ ๋ํ ์ ๋ณด ์ ์ฅ์ ์ํ ๊ฐ์ฒด ์์ฑ
list.add(vo);
}
// ์ฐ๊ฒฐ๊ณผ ์ญ์์ผ๋ก ํด์
Common.close(rs);
Common.close(stmt);
Common.close(conn);
} catch (Exception e) {
e.printStackTrace(); // ๋์ด์ค๋๊ฒ ์์ ๋๋ฅผ ๋๋นํด ์์ธ์ฒ๋ฆฌ ๋ฃ์ด์ค ์๋ ์์
}
return list;
}
public void empSelectPrint(List<EmpVO> list) {
for(EmpVO e : list) {
System.out.println("์ฌ์๋ฒํธ : " + e.getNo());
System.out.println("์ด๋ฆ : " + e.getName());
System.out.println("์ง์ฑ
: " + e.getJob());
System.out.println("๋งค๋์ : " + e.getMgr());
System.out.println("์
์ฌ์ผ : " + e.getDate());
System.out.println("๊ธ์ฌ : " + e.getSal());
System.out.println("๋ณด๋์ค : " + e.getComm());
System.out.println("๋ถ์๋ฒํธ : " + e.getDeptNo());
System.out.println("-----------------------------");
}
}
public void empInsert() {
System.out.println("์ฌ์ ์ ๋ณด๋ฅผ ์
๋ ฅํ์ธ์.");
System.out.print("์ฌ์๋ฒํธ(4์๋ฆฌ) : ");
int no = sc.nextInt();
System.out.print("์ด๋ฆ : ");
String name = sc.next();
System.out.print("์ง์ฑ
: ");
String job = sc.next();
System.out.print("๋งค๋์ (4์๋ฆฌ) : ");
int mgr = sc.nextInt();
System.out.print("์
์ฌ์ผ : ");
String date = sc.next();
System.out.print("๊ธ์ฌ : ");
double sal = sc.nextDouble();
System.out.print("๋ณด๋์ค : ");
double comm = sc.nextDouble();
System.out.print("๋ถ์๋ฒํธ : ");
int deptNo = sc.nextInt();
// INSERT INTO EMP VALUES(23,'ENAME', 'JOB',MGR,'HIREDATE',SAL,COMM,DEPTNO);
String sql = "INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES(" +
no + ", " + "'" + name + "'" + ", " + "'" + job + "'" + ", " + mgr + ", " + "'" + date + "'" +
", " + sal + ", " + comm + ", " + deptNo + ")";
try {
conn = Common.getConnection();
stmt = conn.createStatement();
int ret = stmt.executeUpdate(sql); // ์ํฅ๋ฐ์ ํ์ด ๋ช ๊ฐ๋
System.out.println("Return : " + ret);
} catch(Exception e) {
e.printStackTrace();
}
Common.close(stmt);
Common.close(conn);
}
}
EmpVO
package com.kh.jdbc.vo;
import java.sql.Date; // sql์ ์๋ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์๊ฑฐ๋ผ sql date import ํด์ผํจ.
// DB์ ์๋ ์ ๋ณด๋ฅผ ํผ์์ ๊ฐ์ฒด์ ๋ด๋ ์ฉ๋
public class EmpVO {
private int no;
private String name;
private String job;
private int mgr;
private Date date;
private double sal;
private double comm;
private int deptNo;
public EmpVO(int no, String name, String job, int mgr, Date date, double sal, double comm, int deptNo) {
this.no = no;
this.name = name;
this.job = job;
this.mgr = mgr;
this.date = date;
this.sal = sal;
this.comm = comm;
this.deptNo = deptNo;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
}
JDBCMain
package com.kh.jdbc;
import com.kh.jdbc.dao.EmpDAO;
import com.kh.jdbc.vo.EmpVO;
import java.util.List;
import java.util.Scanner;
public class JdbcMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EmpDAO dao = new EmpDAO();
while (true) {
System.out.println("[EMP TABLE COMMAND]");
System.out.println("๋ฉ๋ด๋ฅผ ์ ํํ์ธ์.");
System.out.print("[1]SELECT [2]INSERT [3]UPDATE [4]DELETE [5]EXIT : ");
int sel = sc.nextInt();
switch(sel) {
case 1 :
List<EmpVO> list = dao.empSelect();
dao.empSelectPrint(list);
break;
case 2 :
dao.empInsert();
break;
case 3 :
break;
case 4 :
break;
case 5 :
System.out.println("์ข
๋ฃํฉ๋๋ค.");
return;
}
}
}
}