Callablestatement - ๊ฑฐ์ ์ํจ
: plsql - ํ๋ก์์ ๋ฅผ ํธ์ถํด์ ์ฌ์ฉํจ
ex) sql = "{call insertScore(?,?,?,?,?)}"; //callํ๋ก์์ ๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ
Callablestatement
Test3. ์ฌ์ฉ์ํํ ๊ฐ์ ๋ฐ๋ class์ DB์ ์ ์ฅํ๋ query๋ก ๋๋๊ธฐ
scoreDTO
: DTO [Data Transfer Object] : ๋ฐ์ดํฐ๋ฅผ DB์ ์ ๋ฌํด์ฃผ๋ ์ญํ
package com.score6;
public class ScoreDTO { // DTO [Data Transfer Object] : ๋ฐ์ดํฐ๋ฅผ DB์ ์ ๋ฌํด์ฃผ๋ ์ญํ
//์ฌ์ฉ์ํํ
5๊ฐ ๋ฐ์ดํฐ ๋ฐ์๋๋ฐ ์ด๋์๊ฐ ๋ฃ์ด์ผํ ์ง ๋ชจ๋ฅผ๋ ๋ฌด์กฐ๊ฑด DTO!!! => ๊ฐ์ฒด์์ฑํด์ผํจ
private String hak;
private String name;
private int kor;
private int eng;
private int mat;
//์ง์ด๋ฃ์ ๋ ์์ 5๊ฐ์ ๋ฐ์ดํฐ
//๊บผ๋ด์ฌ ๋ 8๊ฐ์ ๋ฐ์ดํฐ
private int tot;
private int ave;
private int rank;
public String getHak() {
return hak;
}
public void setHak(String hak) {
this.hak = hak;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMat() {
return mat;
}
public void setMat(int mat) {
this.mat = mat;
}
public int getTot() {
return tot;
}
public void setTot(int tot) {
this.tot = tot;
}
public int getAve() {
return ave;
}
public void setAve(int ave) {
this.ave = ave;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
String str;
str = String.format("%8s %8s %4d %4d %4d %4d %4d %4d",
hak,name,kor,eng,mat,tot,ave,rank);
return str;
}
}
scoreDAO
DAO [Data Access Object] : DB์ ์ฐ๊ฒฐ ํ INSERT/UPDATE/DELETE๋ฅผ ํตํด DB์ ACCESSํ๋ ๋ฒ
package com.score7;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.db.DBConn;
import oracle.jdbc.OracleTypes;
//CallableStatement (plsql - ํ๋ก์์ ๋ฅผ ํธ์ถํด์ ์ฌ์ฉํจ)
public class ScoreDAO {// JDBC
public int insertData(ScoreDTO dto) {
int result = 0;
Connection conn = DBConn.getConnection();
CallableStatement cstmt = null;
String sql;
try {
sql = "{call insertScore(?,?,?,?,?)}"; //callํ๋ก์์ ๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ
sql += "values (?,?,?,?,?)";
cstmt = conn.prepareCall(sql);
// getter๋ก ๋ค์ด์จ 5๊ฐ์ ๊ฐ์ด set์ผ๋ก ๋ฐ์์ pstmt์ ๋ค์ด๊ฐ๊ฒ ๋จ
cstmt.setString(1, dto.getHak());
cstmt.setString(2, dto.getName());
cstmt.setInt(3, dto.getKor());
cstmt.setInt(4, dto.getEng());
cstmt.setInt(5, dto.getMat());
result = cstmt.executeUpdate();
cstmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return result;
}
// ์์
public int updateData(ScoreDTO dto) {
int result = 0;
Connection conn = DBConn.getConnection(); //DBConn์ ์ด๋ฏธ static์ด๋ผ ๋ฉ๋ชจ๋ฆฌ์์ ์ฌ๋ผ๊ฐ์์ผ๋ฏ๋ก run์คํํ์๋ง์ ๊ทธ๊ฑธ ๊ฐ์ง๊ณ ์ด
CallableStatement cstmt = null;
String sql;
try {
sql = "{call updateScore(?,?,?,?)} ";
cstmt = conn.prepareCall(sql);
cstmt.setString(1, dto.getHak());
cstmt.setInt(2, dto.getKor());
cstmt.setInt(3, dto.getEng());
cstmt.setInt(4, dto.getMat());
result = cstmt.executeUpdate();
} catch (Exception e) {
System.out.println(e.toString());
}
return result;
}
//์ญ์
public int deleteData(String hak) {
int result = 0;
Connection conn = DBConn.getConnection();
CallableStatement cstmt = null;
String sql;
try {
sql = "delete score where hak = ? ";
cstmt = conn.prepareCall(sql);
cstmt.setString(1, hak);
result = cstmt.executeUpdate();
cstmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return result;
}
//์ ์ฒด ์ถ๋ ฅ ((๋๋ช
์ด์ธ์ด ๋์ฌ ์๋ ์์ผ๋๊น LIST๋ก ๋ฐ์)
public List<ScoreDTO> getList(){
List<ScoreDTO> lists = new ArrayList<ScoreDTO>();
Connection conn = DBConn.getConnection();
CallableStatement cstmt = null;
//select๋ฅผ ํ๊ฒ ๋์๋ select ํ ๊ฒฐ๊ณผ(table)๋ฅผ ๋ด๋ ๊ทธ๋ฆ์ธ ResultSet (interface) ๊ฐ ๋ฌด์กฐ๊ฑด ํ์ํจ
ResultSet rs = null;
String sql;
try {
//select๋ฌธ
sql = "{call selectAllScore(?)}"; //out์ฉ๋๋ก precedure๋ง๋ค๋ ๋๋ ค๋ฐ๋ ์ปค์๋ฅผ ๋ง๋ฆ
cstmt = conn.prepareCall(sql);
//out ํ๋ผ๋ฏธํฐ์ ์๋ฃํ ์ค์ (line 129)
cstmt.registerOutParameter(1, OracleTypes.CURSOR); //๋ฐํ๋ฐ๋ cursorํ์
์ ? ํ์
์ ์ ํ๊ฑฐ
//ํ๋ก์์ ์คํ
cstmt.executeQuery();
// out ํ๋ผ๋ฏธํฐ ๊ฐ ๋๋ ค๋ฐ๊ธฐ
rs = (ResultSet)cstmt.getObject(1);
while(rs.next()) { //next() : ๋ค์ ํ์นธ ๋ด๋ ค๋ดค์๋ ๋ฐ์ดํฐ๊ฐ ์๋?
//๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด
ScoreDTO dto = new ScoreDTO();
dto.setHak(rs.getString(1));//1๋ฒ๋ถํฐ ์์
dto.setName(rs.getString("name"));
dto.setKor(rs.getInt(3));
dto.setEng(rs.getInt("eng"));
dto.setMat(rs.getInt(5));
dto.setTot(rs.getInt("tot"));
dto.setAve(rs.getInt(7));
dto.setRank(rs.getInt("rank"));
lists.add(dto);
}
rs.close();
cstmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return lists;
}
// ์ด๋ฆ๊ฒ์ (๋๋ช
์ด์ธ์ด ๋์ฌ ์๋ ์์ผ๋๊น LIST๋ก ๋ฐ์)
public List<ScoreDTO> searchName(String name) {
List<ScoreDTO> lists = new ArrayList<ScoreDTO>();
Connection conn = DBConn.getConnection();
CallableStatement cstmt = null;
// select๋ฅผ ํ๊ฒ ๋์๋ select ํ ๊ฒฐ๊ณผ(table)๋ฅผ ๋ด๋ ๊ทธ๋ฆ์ธ ResultSet (interface) ๊ฐ ๋ฌด์กฐ๊ฑด ํ์ํจ
ResultSet rs = null;
String sql;
try {
//select๋ฌธ
sql = "{call selectNameScore(?,?)}"; //์ด๋ฆ๊ฒ์์ ๋งค๊ฐ๋ณ์ 2๊ฐ ๋์ค๋ cursor๊ฐ/๋๋ ค๋ฐ๋ ๊ฐ
cstmt = conn.prepareCall(sql);
//out ํ๋ผ๋ฏธํฐ์ ์๋ฃํ ์ค์ (line 129)
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
//in ํ๋ผ๋ฏธํฐ
cstmt.setString(2, name);
//ํ๋ก์์ ์คํ
cstmt.executeQuery();
rs = (ResultSet)cstmt.getObject(1); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ ๊ฐ ๋ฐ์์ฃผ๊ธฐ
while(rs.next()) { // ์ด๋ฆ๋ช๊ฐ์ธ์ง ๋ชจ๋ฅด๋๊น while / next() : ๋ค์ ํ์นธ ๋ด๋ ค๋ดค์๋ ๋ฐ์ดํฐ๊ฐ ์๋?
//๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด
ScoreDTO dto = new ScoreDTO();
dto.setHak(rs.getString(1));//1๋ฒ๋ถํฐ ์์
dto.setName(rs.getString("name"));
dto.setKor(rs.getInt(3));
dto.setEng(rs.getInt("eng"));
dto.setMat(rs.getInt(5));
dto.setTot(rs.getInt("tot"));
dto.setAve(rs.getInt(7));
dto.setRank(rs.getInt("rank"));
lists.add(dto);
}
rs.close();
cstmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return lists;
}
//ํ๋ฒ๊ฒ์ (๋๋ช
์ด์ธ์ด ๋์ฌ ์๋ ์์ผ๋๊น LIST๋ก ๋ฐ์)
public ScoreDTO searchHak(String hak) { //hak์ ๋๊ฐ์ ๋ฐ์ดํฐ ์์ - primary key => dto์ ๋ด์๊ฑฐ๊ณ dto๋ฅผ ๋ฐํํด์ฃผ๋ฉด ๋จ
ScoreDTO dto = null; //new ๊ฐ์ฒด์์ฑ - ์์ ์๋ ์์ผ๋๊น , ํ์ํ ๋ ๊ฐ์ฒด์์ฑํ๋ฉด ๋จ
Connection conn = DBConn.getConnection();
CallableStatement cstmt = null;
// select๋ฅผ ํ๊ฒ ๋์๋ select ํ ๊ฒฐ๊ณผ(table)๋ฅผ ๋ด๋ ๊ทธ๋ฆ์ธ ResultSet (interface) ๊ฐ ๋ฌด์กฐ๊ฑด ํ์ํจ
ResultSet rs = null;
String sql;
try {
//select๋ฌธ
sql = "{call selectHakScore(?,?)}";
cstmt = conn.prepareCall(sql);
//out ํ๋ผ๋ฏธํฐ์ ์๋ฃํ ์ค์ (line 129)
cstmt.registerOutParameter(1, OracleTypes.CURSOR);
//in ํ๋ผ๋ฏธํฐ
cstmt.setString(2, hak);
//ํ๋ก์์ ์คํ
cstmt.executeQuery();
rs = (ResultSet)cstmt.getObject(1); //์ฒซ๋ฒ์งธ ๋ฌผ์ํ ๊ฐ ๋ฐ์์ฃผ๊ธฐ
if(rs.next()) { // ์ด์ฐจํผ ํ๋ = if / ๋ค์ ํ์นธ ๋ด๋ ค๋ดค์๋ ๋ฐ์ดํฐ๊ฐ ์๋?
// ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด
dto = new ScoreDTO();
dto.setHak(rs.getString(1));// 1๋ฒ๋ถํฐ ์์
dto.setName(rs.getString("name"));
dto.setKor(rs.getInt(3));
dto.setEng(rs.getInt("eng"));
dto.setMat(rs.getInt(5));
dto.setTot(rs.getInt("tot"));
dto.setAve(rs.getInt(7));
}
rs.close();
cstmt.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return dto;
}
}
score
: ์ฌ์ฉ์ํํ
๋ฐ์ดํฐ 5๊ฐ ๋ฐ์๋ด๋ ํด๋์ค
package com.score6;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Score { //์ฌ์ฉ์ํํ
๋ฐ์ดํฐ 5๊ฐ ๋ฐ์๋ด๋ ํด๋์ค
ScoreDAO dao = new ScoreDAO();
Scanner sc = new Scanner(System.in);
//์
๋ ฅ
public void insert() {
ScoreDTO dto = new ScoreDTO(); //dto๋ ์คํํ ๋๋ง๋ค ์๋กญ๊ฒ ๋ง๋ค๊ฑฐ๋๊น ์์๋ค๊ฐ ๋ง๋ค์ด์ค์ผํจ
System.out.print("ํ๋ฒ?");
dto.setHak(sc.next());
System.out.print("์ด๋ฆ?");
dto.setName(sc.next());
System.out.print("๊ตญ์ด?");
dto.setKor(sc.nextInt());
System.out.print("์์ด?");
dto.setEng(sc.nextInt());
System.out.print("์ํ?");
dto.setMat(sc.nextInt());
int result = dao.insertData(dto); //5๊ฐ์ ๋ฐ์ดํฐ๋ฅผ dto๋ฅผ ๊ฐ์ง๊ณ dao์ ๊ฐ์ ์คํ ํํ 1/0์ผ๋ก ๋ฐํ
if(result != 0) {
System.out.println("์ถ๊ฐ ์ฑ๊ณต!");
}else {
System.out.println("์ถ๊ฐ ์คํจ!");
}
}
//์์
public void update() {
ScoreDTO dto = new ScoreDTO();
System.out.println("์์ ํ ํ๋ฒ?");
dto.setHak(sc.next());
System.out.print("๊ตญ์ด?");
dto.setKor(sc.nextInt());
System.out.print("์์ด?");
dto.setEng(sc.nextInt());
System.out.print("์ํ?");
dto.setMat(sc.nextInt());
int result = dao.updateData(dto);
if(result != 0) {
System.out.println("์์ ์ฑ๊ณต!");
}else {
System.out.println("์์ ์คํจ!");
}
}
//
public void delete() {
try {
System.out.print("์ญ์ ํ ํ๋ฒ?");
String hak = sc.next();
int result = dao.deleteData(hak);// ํ์ ๋๊ธฐ๋ฉด ์ญ์ ๊ฐ ๋จ
if(result != 0) {
System.out.println("์ญ์ ์ฑ๊ณต!");
}else {
System.out.println("์ญ์ ์คํจ!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
public void selectAll() {
List<ScoreDTO> lists = dao.getList();
Iterator<ScoreDTO> it = lists.iterator();
while (it.hasNext()) {
ScoreDTO dto = it.next();
System.out.println(dto.toString());
}
}
public void searchName() {
System.out.print("๊ฒ์ํ ์ด๋ฆ?");
String name = sc.next();
List<ScoreDTO> lists = dao.searchName(name); //name์ผ๋ก select๋ฅผ ํด์ lists๋ก ๋๋ ค์ค.
Iterator<ScoreDTO> it = lists.iterator();
while (it.hasNext()) {
ScoreDTO dto = it.next();
System.out.println(dto.toString());
}
}
public void searchHak() {
System.out.print("๊ฒ์ํ ํ๋ฒ?");
String hak = sc.next();
ScoreDTO dto = dao.searchHak(hak); //name์ผ๋ก select๋ฅผ ํด์ lists๋ก ๋๋ ค์ค.
if (dto != null) { //์์ ๋ญ๊ฐ์๋ค๋ฉด
System.out.println(dto.toString()); //๊ทธ๋ฅ ์ด์ฐจํผ ํ๋ ๋์ค๋๊น ๊ทธ๊ฑฐ ์ถ๋ ฅํด~
}
}
}
scoreMain
: ๋ฉ๋ด) 1. 2. 3.
package com.score6;
import java.util.Scanner;
public class ScoreMain {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Score ob = new Score();
int ch;
while(true) {
do {
System.out.print("1.์ถ๊ฐ 2.์์ 3.์ญ์ 4.์ ์ฒด์ถ๋ ฅ 5.์ด๋ฆ๊ฒ์ 6.ํ๋ฒ๊ฒ์ 7.์ข
๋ฃ");
ch = sc.nextInt();
}while(ch<1);
switch (ch) {
case 1:
ob.insert();
break;
case 2:
ob.update();
break;
case 3:
ob.delete();
break;
case 4:
ob.selectAll();
break;
case 5:
ob.searchName();
break;
case 6:
ob.searchHak();
break;
case 7:
System.exit(0);
break;
default:
break;
}
}
}
}
1.์ถ๊ฐ 2.์ข
๋ฃ1
ํ๋ฒ?111
์ด๋ฆ?suzi
๊ตญ์ด?50
์์ด?60
์ํ?70
์ถ๊ฐ ์ฑ๊ณต!
1.์ถ๊ฐ 2.์ข
๋ฃ1
ํ๋ฒ?222
์ด๋ฆ?inna
๊ตญ์ด?60
์์ด?70
์ํ?80
์ถ๊ฐ ์ฑ๊ณต!
1.์ถ๊ฐ 2.์ข
๋ฃ