[JDBC] Day2 - Callablestatement

์œค์ˆ˜์ธยท2023๋…„ 12์›” 27์ผ
0

๐Ÿ“’๊ตญ๋น„ํ•™์› [DB]

๋ชฉ๋ก ๋ณด๊ธฐ
13/14
post-thumbnail

Callablestatement - ๊ฑฐ์˜ ์•ˆํ•จ

: plsql - ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‚ฌ์šฉํ•จ
ex) sql = "{call insertScore(?,?,?,?,?)}"; //callํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•

1.


Callablestatement

๐Ÿ’ป ์ž…๋ ฅ

1. procedure๋ฅผ ํ†ตํ•ด DB accessํ•˜๋Š” ๋ฒ•- ํ•™๋ฒˆ/๊ณผ๋ชฉ์ ์ˆ˜/๋žญํ‚น ์ถœ๋ ฅ ์˜ˆ์ œ

Test3. ์‚ฌ์šฉ์žํ•œํ…Œ ๊ฐ’์„ ๋ฐ›๋Š” class์™€ DB์— ์ €์žฅํ•˜๋Š” query๋กœ ๋‚˜๋ˆ„๊ธฐ

โœ… cmd

  • Score ํŒจํ‚ค์ง€์™€ ๊ด€๋ จ๋œ ํ”„๋กœ์‹œ์ €(Procedure) ์ƒ์„ฑํ•˜๊ธฐ


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.์ข…๋ฃŒ
profile
์–ด์ œ๋ณด๋‹ค ์กฐ๊ธˆ ๋” ์„ฑ์žฅํ•˜๊ธฐ!

0๊ฐœ์˜ ๋Œ“๊ธ€