yeony._.
๐Ÿข์ฝฉ๋ถ€๐Ÿข
yeony._.
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (99)
    • ๐Ÿ’ปProgramming (91)
      • โ˜•๏ธJava (19)
      • ๐Ÿ’ฝOracle (21)
      • ๐Ÿ‘ฉ‍๐ŸŽจHTML&CSS (12)
      • ๐ŸŽจ ์‹ค์Šต (4)
      • ๐Ÿƒ‍โ™€๏ธJavaScript (25)
      • JSP (8)
      • ๐ŸƒSpring (0)
      • ๐Ÿฅงpython (0)
      • API (0)
      • ๐Ÿ˜ˆGit (1)
    • ๐ŸคซPrivate (0)
      • -- (0)
      • + (0)
      • S (0)
      • P (0)
      • B (0)
    • ๐Ÿ˜ŽProject (4)
      • ๐Ÿ—“๏ธwbs ํ”„๋กœ์ ํŠธ (3)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • HTML #CSS #HTML์ด๋ฏธ์ง€ #HTML์ด๋ฏธ์ง€์—๊ธ€์”จ #HTML์ด๋ฏธ์ง€์—์ด๋ฏธ์ง€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
yeony._.

๐Ÿข์ฝฉ๋ถ€๐Ÿข

๐Ÿ’ปProgramming/๐Ÿ’ฝOracle

CallableStatement ํ™œ์šฉ(2) - ์ž๋ฐ” ์ฝ”๋“œ

2022. 8. 23. 23:03

insertScore ์„ ํ†ตํ•ด CallableStatement  ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

CallableStatement ๊ฐ์ฒด ์ƒ์„ฑ

CallableStatement cstmt = null;

 

ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•  ์ฟผ๋ฆฌ ์ž‘์„ฑ

์ž๋ฐ”์—์„œ ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” CALL๋กœ ํ˜ธ์ถœ 

{ CALL ์‹œํ€€์Šค ์ด๋ฆ„(์‹œํ€€์Šค์— ๋„ฃ์„ ๊ฐ’)}

sql = "{ CALL insertScore(?, ?, ?, ?, ?, ?)}";

 

 

์ปค๋„ฅ์…˜ ๋ฐฉ๋ฒ•

cstmt = conn.prepareCall(sql);

 

------------------------------------------------------------------------------------------------------------

readScore ์„ ํ†ตํ•ดCallableStatement  ์‚ฌ์šฉ ๋ฐฉ๋ฒ• ์•Œ์•„๋ณด๊ธฐ

์–ด๋ ต๋‹ค..๐Ÿค”๐Ÿค”๐Ÿค”

sql = "{ Call readScore(?, ?) }";

 

CREATE OR REPLACE PROCEDURE readScore 
(
    pResult OUT SYS_REFCURSOR,
    pHak IN VARCHAR2
)
IS
BEGIN
    OPEN pResult FOR
        SELECT hak, name, TO_CHAR(birth, 'YYYY-MM-DD') birth, kor, eng, mat,
            kor+eng+mat tot, (kor+eng+mat)/3 ave
        FROM score
        WHERE hak = pHak;
END;
/

readScore์€ ํ•™๋ฒˆ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ๋Š”๋‹ค. 

์ž๋ฐ”์—์„œ ํ•™๋ฒˆ์„ ์ž…๋ ฅํ•˜๋ฉด, ์ž…๋ ฅํ•œ ํ•™๋ฒˆ์„ ์˜ค๋ผํด๋กœ ๋„˜๊ธด๋‹ค.

์˜ค๋ผํด์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ ์ค‘์— ํ•ด๋‹น ํ•™๋ฒˆ์ด ์กด์žฌํ•œ๋‹ค๋ฉด ํ•ด๋‹น ํ•™๋ฒˆ์˜ ์ •๋ณด๋ฅผ ์ž๋ฐ”๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

cstmt.registerOutParameter(1, OracleTypes.CURSOR);

๊ฒฐ๊ณผ๋ฅผ ๋‹ด๋Š” ๋ณ€์ˆ˜์ธ result์˜ ๊ฒฝ์šฐ ์˜ค๋ผํด์˜ ์ปค์„œ๋กœ ์„ค์ •ํ•˜์—ฌ ์˜ค๋ผํด์—์„œ ๋ฐ›์•„์˜จ๋‹ค. 

cstmt.setString(2, hak);

ํ•™๋ฒˆ์˜ ๊ฒฝ์šฐ ์˜ค๋ผํด๋กœ ๋„˜๊ธธ ๋ณ€์ˆ˜์ด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ ์ž…๋ ฅํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ๋™์ผํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค. 

cstmt.executeUpdate();

ํ”„๋กœ์‹œ์ € ์‹คํ–‰

rs = (ResultSet) cstmt.getObject(1);

SYS_REFCURSOR๋Š” ResultSet๋กœ ๋ฐ˜ํ™˜ ๋ฐ›๋Š”๋‹ค.

 

ScoreDAOImpl ํด๋ž˜์Šค

package db.ch4.score;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import db.util.DBConn;
import oracle.jdbc.OracleTypes;

public class ScoreDAOImpl implements ScoreDAO {
	private Connection conn = DBConn.getConnection();

	@Override
	public int insertScore(ScoreDTO dto) throws SQLException {
		int result = 0;
		CallableStatement cstmt = null; // ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค
		String sql;

		try {
			// ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•  ์ฟผ๋ฆฌ ์ž‘์„ฑ - ์ž๋ฐ”์—์„œ ํ”„๋กœ์‹œ์ €๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” CALL๋กœ ํ˜ธ์ถœ
			sql = "{ CALL insertScore(?, ?, ?, ?, ?, ?)}";

			// CallbleSatement ๊ฐ์ฒด ์ƒ์„ฑ
			cstmt = conn.prepareCall(sql);

			cstmt.setString(1, dto.getHak());
			cstmt.setString(2, dto.getName());
			cstmt.setString(3, dto.getBirth());
			cstmt.setInt(4, dto.getKor());
			cstmt.setInt(5, dto.getEng());
			cstmt.setInt(6, dto.getMat());

			// ํ”„๋กœ์‹œ์ € ์‹คํ–‰
			cstmt.executeUpdate();
			// CallableStatement์˜ executeUpdate() ๋ฉ”์†Œ๋“œ ๋ฆฌํ„ด ๊ฐ’์€ ํ”„๋กœ์‹œ์ € ์‹คํ–‰ ์—ฌ๋ถ€์ด์ž
			// DML(INSERT, UPDATE, DELETE) ์‹คํ–‰ ๊ฐฏ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
			// SELECT ๋ฌธ์„ ์ œ์™ธํ•œ DML, DDL ์‹คํ–‰
			result = 1;

		} catch (SQLIntegrityConstraintViolationException e) {
			// ๊ธฐ๋ณธํ‚ค ์ œ์•ฝ ์œ„๋ฐ˜, NOT NULL ๋“ฑ์˜ ์ œ์•ฝ ์œ„๋ฐ˜ -- ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ ์œ„๋ฐ˜ ์‹œ ๋ฐœ์ƒ
			if (e.getErrorCode() == 1) {
				System.out.println("ํ•™๋ฒˆ ์ค‘๋ณต์œผ๋กœ ๋“ฑ๋ก์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค");

			} else if (e.getErrorCode() == 1400) {
				System.out.println("ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค ");

			} else {
				System.out.println(e.toString());
			}

			throw e;
		} catch (SQLDataException e) {
			// ๋‚ ์งœ ๋“ฑ์˜ ํ˜•์‹ ์ž˜๋ชป์œผ๋กœ ์ธํ•œ ์˜ˆ์™ธ ๋ฐœ์ƒ
			if (e.getErrorCode() == 1840 || e.getErrorCode() == 1861) {
				System.out.println("๋‚ ์งœ ์ž…๋ ฅ ํ˜•์‹ ์˜ค๋ฅ˜");
			} else {
				System.out.println(e.toString());
			}
			throw e;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

		return result;
	}

	@Override
	public int updateScore(ScoreDTO dto) throws SQLException {
		int result = 0;
		CallableStatement cstmt = null;
		String sql;

		try {

			sql = "{ CALL updateScore(? ,? ,?, ? ,?, ?)}";

			cstmt = conn.prepareCall(sql);

			cstmt.setString(1, dto.getName());
			cstmt.setString(2, dto.getBirth());
			cstmt.setInt(3, dto.getKor());
			cstmt.setInt(4, dto.getEng());
			cstmt.setInt(5, dto.getMat());
			cstmt.setString(6, dto.getHak());

			cstmt.executeUpdate();

			result = 1;

		} catch (SQLIntegrityConstraintViolationException e) {
			if (e.getErrorCode() == 1400) {
				System.out.println("ํ•„์ˆ˜ ์ž…๋ ฅ ์‚ฌํ•ญ์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค");
			} else {
				System.out.println();
			}
		} catch (SQLDataException e) {
			if (e.getErrorCode() == 1840 || e.getErrorCode() == 1861) {
				System.out.println("๋‚ ์งœ ํ˜•์‹ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค๋‹ค");
			} else {
				System.out.println(e.toString());
			}
			throw e;
		} catch (SQLException e) {
			if (e.getErrorCode() == 20100) {
				// 20100 : ํ”„๋กœ์‹œ์ €๋ฅผ ๋งŒ๋“ค ๋•Œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ์˜ˆ์™ธ ์ฝ”๋“œ
				System.out.println("๋“ฑ๋ก๋œ ์ž๋ฃŒ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค");
			}
			e.printStackTrace();
			throw e;
		} finally {
			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

		return result;
	}

	@Override
	public int deleteScore(String hak) throws SQLException {
		// TODO Auto-generated method stub
		int result = 0;

		CallableStatement cstmt = null;
		String sql;

		try {
			sql = "CALL deleteScore(?)";

			cstmt = conn.prepareCall(sql);

			cstmt.setString(1, hak);
			// hak ๋งŒ ์•Œ๋ฉด ์ง€์šธ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๊ตณ์ด dto ์‚ฌ์šฉ ์•ˆ ํ•ด๋„ ๋จ
			// ๋ฐ์ดํ„ฐ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค๋ฉด dto ์‚ฌ์šฉ

			cstmt.executeUpdate();
			result = 1;

		} catch (SQLException e) {
			if (e.getErrorCode() == 20100) {
				// ํ”„๋กœ์‹œ์ €์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด 20100์ด๋ผ๋Š” ์—๋Ÿฌ์ฝ”๋“œ๋ฅผ ๋˜์ง
				System.out.println("๋“ฑ๋ก๋œ ์ž๋ฃŒ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค");
			} else {
				e.printStackTrace();
			}
		} finally {
			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return result;
	}

	@Override
	public ScoreDTO readScore(String hak) {
		// TODO Auto-generated method stub
		ScoreDTO dto = null;
		CallableStatement cstmt = null;
		ResultSet rs = null;
		String sql;

		try {
			sql = "{ Call readScore(?, ?) }";
			// ? ๋Š” ํ”„๋กœ์‹œ์ € ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜๋งŒํผ(IN, OUT, INOUT ๋ชจ๋‘)

			cstmt = conn.prepareCall(sql);

			cstmt.registerOutParameter(1, OracleTypes.CURSOR);
			// OUT ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํƒ€์ž…์„ ์„ค์ •ํ•œ๋‹ค
			// ์˜ค๋ผํด์€ SYS_REFCURSOR์€ OracleTypes.CURSOR์œผ๋กœ ์„ค์ •

			cstmt.setString(2, hak);

			cstmt.executeQuery();

			rs = (ResultSet) cstmt.getObject(1);
			// OUT ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํ”„๋กœ์‹œ์ €๊ฐ€ ๋„˜๊ฒจ์ค€ ๊ฐ’์œผ๋กœ getter๋กœ ๋„˜๊ฒจ ๋ฐ›๋Š”๋‹ค

			if (rs.next()) {
				dto = new ScoreDTO();

				dto.setHak(rs.getString("hak"));
				dto.setName(rs.getString("name"));

				dto.setBirth(rs.getDate("birth").toString());
				// "๋…„-์›”-์ผ ์‹œ:๋ถ„:์ดˆ"๋กœ ์ถœ๋ ฅ๋จ
				dto.setKor(rs.getInt("kor"));
				dto.setEng(rs.getInt("eng"));
				dto.setMat(rs.getInt("mat"));

				dto.setTot(rs.getInt("tot"));
				dto.setAve(rs.getInt("ave"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}

			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return dto;
	}

	@Override
	public List<ScoreDTO> listScore() {
		List<ScoreDTO> list = new ArrayList<>();
		CallableStatement cstmt = null;
		ResultSet rs = null;
		String sql;

		try {
			// ์ „์ฒด ๋ฆฌ์ŠคํŠธ
			// SELECT ์ปฌ๋Ÿผ, ์ปฌ๋Ÿผ FROM ํ…Œ์ด๋ธ”

			sql = "{ CALL listScore(?) }";

			// TO_CHAR(birth, 'YYYY-MM-DD')

			cstmt = conn.prepareCall(sql);

			// ? ๊ฐ€ ์—†์œผ๋‹ˆ๊นŒ Setter ์—†์Œ
			cstmt.registerOutParameter(1, OracleTypes.CURSOR);

			cstmt.execute();

			rs = (ResultSet) cstmt.getObject(1);

			// score ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฝ์–ด List ๊ฐ์ฒด์— ์ €์žฅ
			while (rs.next()) {
				ScoreDTO dto = new ScoreDTO();
				dto.setHak(rs.getString("hak"));
				dto.setName(rs.getString("name"));
				dto.setBirth(rs.getString("birth"));

				dto.setKor(rs.getInt("kor"));
				dto.setEng(rs.getInt("eng"));
				dto.setMat(rs.getInt("mat"));

				dto.setTot(rs.getInt("tot"));
				dto.setAve(rs.getInt("ave"));
				dto.setRank(rs.getInt("rank"));

				list.add(dto);

			}

		} catch (SQLException e) {
			e.printStackTrace();

		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}

			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}

		return list;
	}

	@Override
	public List<ScoreDTO> listScore(String name) {
		// TODO Auto-generated method stub
		List<ScoreDTO> list = new ArrayList<>();
		CallableStatement cstmt = null;
		String sql;
		ResultSet rs = null;

		try {
			sql = "{Call searchNameScore(?, ?) }";
			// WHERE LIKE name '%' || ? || '%'";

			cstmt = conn.prepareCall(sql);

			cstmt.registerOutParameter(1, OracleTypes.CURSOR);
			cstmt.setString(2, name);

			cstmt.executeQuery();

			rs = (ResultSet) cstmt.getObject(1);

			while (rs.next()) {
				ScoreDTO dto = new ScoreDTO();
				dto.setHak(rs.getString("hak"));
				dto.setName(rs.getString("name"));
				dto.setBirth(rs.getString("birth"));

				dto.setKor(rs.getInt("kor"));
				dto.setEng(rs.getInt("eng"));
				dto.setMat(rs.getInt("mat"));

				dto.setTot(rs.getInt("tot"));
				dto.setAve(rs.getInt("ave"));

				list.add(dto);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return list;
	}

	@Override
	public Map<String, Integer> averageScore() {
		Map<String, Integer> map = new HashMap<>();

		CallableStatement cstmt = null;
		String sql;

		try {
			sql = "{CALL averageScore(?,?,?)}";

			cstmt = conn.prepareCall(sql);

			cstmt.registerOutParameter(1, OracleTypes.INTEGER);
			cstmt.registerOutParameter(2, OracleTypes.INTEGER);
			cstmt.registerOutParameter(3, OracleTypes.INTEGER);

			cstmt.execute();

			int kor = cstmt.getInt(1);
			int eng = cstmt.getInt(2);
			int mat = cstmt.getInt(3);

			map.put("kor", kor);
			map.put("eng", eng);
			map.put("mat", mat);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (cstmt != null) {
				try {
					cstmt.close();
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		}
		return map;
	}

}

 

ScoreUI ํด๋ž˜์Šค

package db.ch4.score;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

import db.util.DBConn;

public class ScoreUI {
	private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	private ScoreDAO dao = new ScoreDAOImpl();

	public void menu() {
		System.out.println("CallablePreparedStatement ์˜ˆ์ œ...");
		int ch;
		while (true) {

			try {
				System.out.print("1.๋“ฑ๋ก 2.์ˆ˜์ • 3.์‚ญ์ œ 4.ํ•™๋ฒˆ ๊ฒ€์ƒ‰ 5.์ด๋ฆ„๊ฒ€์ƒ‰ 6.๋ฆฌ์ŠคํŠธ 7.ํ‰๊ท  8. ์ข…๋ฃŒ =>");
				ch = Integer.parseInt(br.readLine());

				if (ch == 8) {
					DBConn.close();
					return;
				}

				switch (ch) {
				case 1:
					insert();
					break;
				case 2:
					update();
					break;
				case 3:
					delete();
					break;
				case 4:
					findByHak();
					break;
				case 5:
					findByName();
					break;
				case 6:
					listAll();
					break;
				case 7:
					average();
					break;

				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

	protected void insert() {
		System.out.println("[๋ฐ์ดํ„ฐ ๋“ฑ๋ก]");
		try {
			ScoreDTO dto = new ScoreDTO();

			System.out.print("ํ•™๋ฒˆ? ");
			dto.setHak(br.readLine());

			System.out.print("์ด๋ฆ„? ");
			dto.setName(br.readLine());

			System.out.print("์ƒ๋…„์›”์ผ? ");
			dto.setBirth(br.readLine());

			System.out.print("๊ตญ์–ด? ");
			dto.setKor(Integer.parseInt(br.readLine()));

			System.out.print("์˜์–ด? ");
			dto.setEng(Integer.parseInt(br.readLine()));

			System.out.print("์ˆ˜ํ•™? ");
			dto.setMat(Integer.parseInt(br.readLine()));

			dao.insertScore(dto);

			System.out.println("๋ฐ์ดํ„ฐ ๋“ฑ๋ก ์™„๋ฃŒ!");

		} catch (NumberFormatException e) {
			System.out.println("์ ์ˆ˜๋Š” ์ˆซ์ž๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค");
		} catch (Exception e) {
			System.out.println("๋ฐ์ดํ„ฐ ๋“ฑ๋ก์ด ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค");
		}
		System.out.println();
	}

	protected void update() {
		System.out.println("๋ฐ์ดํ„ฐ ์ˆ˜์ •");

		try {
			ScoreDTO dto = new ScoreDTO();

			System.out.print("์ˆ˜์ •ํ•  ํ•™๋ฒˆ?");
			dto.setHak(br.readLine());

			System.out.print("์ด๋ฆ„");
			dto.setName(br.readLine());

			System.out.print("์ƒ๋…„์›”์ผ?");
			dto.setBirth(br.readLine());

			System.out.print("๊ตญ์–ด?");
			dto.setKor(Integer.parseInt(br.readLine()));

			System.out.print("์˜์–ด?");
			dto.setEng(Integer.parseInt(br.readLine()));

			System.out.print("์ˆ˜ํ•™?");
			dto.setMat(Integer.parseInt(br.readLine()));

			dao.updateScore(dto);

			System.out.println("๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค");

		} catch (NumberFormatException e) {
			System.out.println("์ ์ˆ˜๋Š” ์ˆซ์ž๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค");
		} catch (Exception e) {
			System.out.println("์ž๋ฃŒ ์ˆ˜์ • ์‹คํŒจ");
		}

	}

	protected void delete() {
		System.out.println("๋ฐ์ดํ„ฐ ์‚ญ์ œ");

		String hak;
		try {
			System.out.println("์‚ญ์ œํ•  ํ•™๋ฒˆ?");
			hak = br.readLine();

			int result = dao.deleteScore(hak);

			System.out.println("๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค");

			// ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜์ง€ ์•Š์•˜์œผ๋ฉด 0 ๋ฆฌํ„ด
			// ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ–ˆ๋‹ค๋ฉด ์‚ญ์ œํ•œ ๊ฐœ์ˆ˜ ๋ฆฌํ„ด

		} catch (Exception e) {
			System.out.println("๋ฐ์ดํ„ฐ ์‚ญ์ œ ์‹คํŒจ");
		}
		System.out.println();

	}

	protected void findByHak() {
		System.out.println("ํ•™๋ฒˆ ๊ฒ€์ƒ‰");
		String hak;
		try {
			System.out.println("๊ฒ€์ƒ‰ํ•  ํ•™๋ฒˆ?");
			hak = br.readLine();

			ScoreDTO dto = dao.readScore(hak);

			if (dto == null) {
				System.out.println("๋“ฑ๋ก๋œ ์ž๋ฃŒ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค");
				return;
			}

			System.out.print(dto.getHak() + "\t");
			System.out.print(dto.getName() + "\t");
			System.out.print(dto.getBirth() + "\t");
			System.out.print(dto.getKor() + "\t");
			System.out.print(dto.getEng() + "\t");
			System.out.print(dto.getMat() + "\t");
			System.out.print(dto.getTot() + "\t");
			System.out.println(dto.getAve() + "\t");

		} catch (Exception e) {
			System.out.println("ํ•™๋ฒˆ ๊ฒ€์ƒ‰ ์‹คํŒจ");
		}
		System.out.println();
	}

	protected void findByName() {
		System.out.println("์ด๋ฆ„ ๊ฒ€์ƒ‰");
		String name;

		try {
			System.out.println("๊ฒ€์ƒ‰ํ•  ์ด๋ฆ„?");
			name = br.readLine();

			List<ScoreDTO> list = dao.listScore(name);
			if (list.size() == 0) {
				System.out.println("๋“ฑ๋ก๋œ ์ž๋ฃŒ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค");
				return;
			}

			for (ScoreDTO dto : list) {
				System.out.print(dto.getHak() + "\t");
				System.out.print(dto.getName() + "\t");
				System.out.print(dto.getBirth() + "\t");
				System.out.print(dto.getKor() + "\t");
				System.out.print(dto.getEng() + "\t");
				System.out.print(dto.getMat() + "\t");
				System.out.print(dto.getTot() + "\t");
				System.out.println(dto.getAve() + "\t");
			}
		} catch (Exception e) {
			System.out.println("์ด๋ฆ„ ๊ฒ€์ƒ‰ ์‹คํŒจ");
		}
		System.out.println();
	}

	protected void listAll() {
		System.out.println("์ „์ฒด ๋ฆฌ์ŠคํŠธ");
		System.out.println("ํ•™๋ฒˆ\t์ด๋ฆ„\t์ƒ๋…„์›”์ผ\t\t๊ตญ์–ด\t ์˜์–ด\t ์ˆ˜ํ•™\t์ด์ \tํ‰๊ท \t์„์ฐจ");
		System.out.println("----------------------------------------------------------------------------");

		List<ScoreDTO> list = dao.listScore();
		for (ScoreDTO dto : list) {
			System.out.print(dto.getHak() + "\t");
			System.out.print(dto.getName() + "\t");
			System.out.print(dto.getBirth() + "\t");
			System.out.print(dto.getKor() + "\t");
			System.out.print(dto.getEng() + "\t");
			System.out.print(dto.getMat() + "\t");
			System.out.print(dto.getTot() + "\t");
			System.out.print(dto.getAve() + "\t");
			System.out.println(dto.getRank());
		}
		System.out.println();

	}

	protected void average() {
		System.out.println("\n ๊ณผ๋ชฉ๋ณ„ ํ‰๊ท  ์ ์ˆ˜ ... ");

		Map<String, Integer> map = dao.averageScore();

		int kor = map.get("kor");
		int eng = map.get("eng");
		int mat = map.get("mat");
		System.out.println("๊ตญ์–ด " + kor);
		System.out.println("์ˆ˜ํ•™ " + mat);
		System.out.println("์˜์–ด" + eng);

	}

}

'๐Ÿ’ปProgramming > ๐Ÿ’ฝOracle' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ”์™€ ์˜ค๋ผํด ์—ฐ๋™์‹œํ‚ค๊ธฐ  (0) 2022.08.19
๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๋ฐ ์„ค๊ณ„  (0) 2022.08.19
[PL/SQL] ์ธ๋ฑ์Šค  (0) 2022.08.18
[PL/SQL] ์˜ˆ์™ธ(Exception) ์ฒ˜๋ฆฌ  (0) 2022.08.18
[PL/SQL] ์ปค์„œ(Cursor)  (0) 2022.08.18
    '๐Ÿ’ปProgramming/๐Ÿ’ฝOracle' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • ์ž๋ฐ”์™€ ์˜ค๋ผํด ์—ฐ๋™์‹œํ‚ค๊ธฐ
    • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง ๋ฐ ์„ค๊ณ„
    • [PL/SQL] ์ธ๋ฑ์Šค
    • [PL/SQL] ์˜ˆ์™ธ(Exception) ์ฒ˜๋ฆฌ
    yeony._.
    yeony._.
    ํž˜ ์„ธ์ง€๋Š” ์•Œ์•ฝ๐Ÿ’Š๐Ÿ’Š๐Ÿ’Š

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”