CallableStatement
PreparedStatement ํ์ ์ธํฐํ์ด์ค. SQL์ ์ฅ ํ๋ฆฌ์์ ๋ฅผ ์คํํ๋๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด. ํ๋ก์์ ํธ์ถ ๊ตฌ๋ฌธ์๋ ๊ฒฐ๊ณผ ๋งค๊ฐ๋ณ์๋ฅผ ๊ฐ์ง๋ ํํ์ ๊ฒฐ๊ณผ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋๊ฐ์ง ํํ๊ฐ ์๋ค. ๋ ํํ ๋ชจ๋ IN ๋งค๊ฐ๋ณ์(์ ๋ ฅ), OUT ๋งค๊ฐ๋ณ์(์ถ๋ ฅ) ๋๋ INOUT ๋งค๊ฐ๋ณ์(์ ์ถ๋ ฅ)๋ฅผ ์ํด ์ฌ์ฉ๋์ด์ง๋ ๋งค๊ฐ๋ณ์์ ๋ณ์ ๋ฒํธ๋ฅผ ๊ฐ์ง๋ฉฐ ?๋ ๋งค๊ฐ๋ณ์์ ์ ์ฅ์์น๋ก ๋ํ๋ธ๋ค.
ScoreDAOImpl
ํ๋ฆฌ์์ ํธ์ถ ํ์ ๋ฐ์ดํฐ ์ ๋ ฅ ๋ฉ์๋
//๋ฐ์ดํฐ ์
๋ ฅ
@Override
public int insertScore(ScoreDTO dto) throws SQLException {
int result = 0;
CallableStatement cstmt = null;
String sql;
try {
// 1) ํ๋ก์์ ๋ฅผ ์คํํ๋ ์ฟผ๋ฆฌ ์์ฑ
// ์๋ฐ์์ ํ๋ก์์ ๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ : CALL๋ก ํ๋ก์์ ์คํ
sql = "{CALL INSERT SCORE(?,?,?,?,?,?) }";
// 2) ์ฟผ๋ฆฌ๋ฅผ ์ธ์๋ก CallableStatement ๊ฐ์ฒด๋ฅผ ์์ฑ
cstmt = conn.prepareCall(sql);
// 3) setter ๋ฅผ ์ด์ฉํ์ฌ ? ์ ๊ฐ ํ ๋น (์์ ์ง์ผ์ ๋ฃ์ด์ผ ํจ)
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());
// 4) ์ฟผ๋ฆฌ๋ฅผ ์คํํ๋ค
cstmt.executeUpdate(); // ๋ฐํ๊ฐ์ ํ๋ก์์ ์คํ ์ ๋ฌด
result=1;
} catch (SQLException e) {
throw e; // ์์ธ ๋์ง๊ธฐ
} finally {
// 5) ๊ฐ์ฒด close
DBUtil.close(cstmt);
}
return result;
}
ScoreUI
๋ฐ์ดํฐ ๋ฑ๋ก UI, ์์ธ ์ฒ๋ฆฌ
protected void insert() {
System.out.println("\n์๋ฃ ๋ฑ๋ก...");
ScoreDTO dto = new ScoreDTO();
try {
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 (SQLIntegrityConstraintViolationException e) {
//NOT null ๋ฑ์ ์ ์ฝ์กฐ๊ฑด์ ์๋ฐํ ๋ ๋ฐ์ํ๋ ์์ธ
//๋ฌด๊ฒฐ์ฑ ์ ์ฝ ์กฐ๊ฑด
if(e.getErrorCode()==1) {
System.out.println("์๋ฌ-๋ฑ๋ก๋ ํ๋ฒ์
๋๋ค.");
}else if(e.getErrorCode()==1400) { // INSERT NOT NULL ์ ์ฝ ์๋ฐ
System.out.println("์๋ฌ - ํ์ ์
๋ ฅ ์ฌํญ์ ์
๋ ฅํ์ง ์์์ต๋๋ค.");
}else {
System.out.println(e.toString());
}
} catch (SQLDataException e) {
if(e.getErrorCode()==1840 || e.getErrorCode()==1861) {
System.out.println("์๋ฌ-๋ ์ง ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.");
}else{
System.out.println(e.toString());
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
ScoreDAOImpl
ํ๋ก์์ ํธ์ถ ํ์์ผ๋ก ๋ฐ์ดํฐ ์ญ์ ๋ฉ์๋
//์ญ์
@Override
public int deleteScore(String hak) throws SQLException {
int result = 0;
CallableStatement cstmt =null;
String sql;
//
try {
sql="{Call deleteScore(?)}";
cstmt=conn.prepareCall(sql);
cstmt.executeUpdate(sql);
cstmt.setString(1, hak);
result = 1;
} catch (SQLException e) {
throw e;
}finally {
DBUtil.close(cstmt);
}
return result;
}
ScoreUI
๋ฐ์ดํฐ ์ญ์ UI
//๋ฐ์ดํฐ ์ญ์
protected void delete() {
System.out.println("\n์๋ฃ ์ญ์ ...");
String hak;
try {
System.out.println("์ญ์ ํ ํ๋ฒ?");
hak = br.readLine();
dao.deleteScore(hak);
System.out.println("๋ฐ์ดํฐ๊ฐ ์ญ์ ๋์์ต๋๋ค.");
} catch (SQLException e) {
if (e.getErrorCode() == 20100) {
System.out.println(e.getMessage());
// 20100 : ํ๋ก์์ ์์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด ๋ฐ์์ํจ ์๋ฌ ์ฝ๋
} else {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println();
}
'๐ป > JDBC' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(33) [JDBC] 4์ผ์ฐจ : JDBC(Java Data Connectivity) (0) | 2024.04.03 |
---|---|
(31) [JDBC] 2์ผ์ฐจ : JDBC(Java Data Connectivity) (0) | 2024.04.03 |
(30) [JDBC] 1์ผ์ฐจ : JDBC(Java Data Connectivity) (0) | 2024.04.02 |