PreparedStatement를 활용한 성적 처리 프로그램 구현
ScoreDAO를 제외하고 변하는 부분은 없다
// <ScoreDAO.java>
package com.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.util.DBConn;
public class ScoreDAO
{
private Connection conn;
public Connection connection() throws ClassNotFoundException, SQLException
{
conn = DBConn.getConnection();
return conn;
}
// 성적 입력 메소드
public int add(ScoreDTO dto) throws SQLException
{
int result = 0;
String sql = "INSERT INTO TBL_SCORE(SID, NAME, KOR, ENG, MAT)"
+ " VALUES(SCORESEQ.NEXTVAL, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getName());
pstmt.setInt(2, dto.getKor());
pstmt.setInt(3, dto.getEng());
pstmt.setInt(4, dto.getMat());
result = pstmt.executeUpdate();
pstmt.close();
return result;
}
// 전체 리스트 출력 메소드
public ArrayList<ScoreDTO> lists() throws SQLException
{
ArrayList<ScoreDTO> result = new ArrayList<ScoreDTO>();
String sql = "SELECT SID, NAME, KOR, ENG, MAT"
+ ", (KOR + ENG + MAT) AS TOT"
+ ", (KOR + ENG + MAT)/3 AS AVG"
+ ", RANK() OVER(ORDER BY (KOR+ENG+MAT) DESC) AS RANK"
+ " FROM TBL_SCORE"
+ " ORDER BY SID ASC";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ScoreDTO dto = new ScoreDTO();
dto.setSid(rs.getString("SID"));
dto.setName(rs.getString("NAME"));
dto.setKor(rs.getInt("KOR"));
dto.setEng(rs.getInt("ENG"));
dto.setMat(rs.getInt("MAT"));
dto.setTot(rs.getInt("TOT"));
dto.setAvg(rs.getDouble("AVG"));
dto.setRank(rs.getInt("RANK"));
result.add(dto);
}
rs.close();
pstmt.close();
return result;
}
// 이름 검색 담당 메소드
public ArrayList<ScoreDTO> lists(String name) throws SQLException
{
ArrayList<ScoreDTO> result = new ArrayList<ScoreDTO>();
String sql = "SELECT SID, NAME, KOR, ENG, MAT, TOT, AVG, RANK"
+ " FROM (SELECT SID, NAME, KOR, ENG, MAT, (KOR + ENG + MAT) AS TOT"
+ ", (KOR + ENG + MAT)/3 AS AVG"
+ ", RANK() OVER(ORDER BY (KOR+ENG+MAT) DESC) AS RANK"
+ " FROM TBL_SCORE) WHERE NAME = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ScoreDTO dto = new ScoreDTO();
dto.setSid(rs.getString("SID"));
dto.setName(rs.getString("NAME"));
dto.setKor(rs.getInt("KOR"));
dto.setEng(rs.getInt("ENG"));
dto.setMat(rs.getInt("MAT"));
dto.setTot(rs.getInt("TOT"));
dto.setAvg(rs.getDouble("AVG"));
dto.setRank(rs.getInt("RANK"));
result.add(dto);
}
rs.close();
pstmt.close();
return result;
}
// 번호 검색 메소드(수정 및 삭제를 하기 위해 식별하기 위해서..)
public ArrayList<ScoreDTO> lists(int sid) throws SQLException
{
ArrayList<ScoreDTO> result = new ArrayList<ScoreDTO>();
String sql = "SELECT SID, NAME, KOR, ENG, MAT, TOT, AVG, RANK"
+ " FROM (SELECT SID, NAME, KOR, ENG, MAT"
+ ", (KOR + ENG + MAT) AS TOT"
+ ", (KOR + ENG + MAT)/3 AS AVG"
+ ", RANK() OVER(ORDER BY (KOR+ENG+MAT) DESC) AS RANK"
+ " FROM TBL_SCORE)"
+ " WHERE SID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, sid);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
ScoreDTO dto = new ScoreDTO();
dto.setSid(rs.getString("SID"));
dto.setName(rs.getString("NAME"));
dto.setKor(rs.getInt("KOR"));
dto.setEng(rs.getInt("ENG"));
dto.setMat(rs.getInt("MAT"));
dto.setTot(rs.getInt("TOT"));
dto.setAvg(rs.getDouble("AVG"));
dto.setRank(rs.getInt("RANK"));
result.add(dto);
}
rs.close();
pstmt.close();
return result;
}
// 인원 수 확인 메소드
public int count() throws SQLException
{
int result = 0;
String sql = "SELECT COUNT(*) AS COUNT FROM TBL_SCORE";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
result = rs.getInt("COUNT");
}
rs.close();
pstmt.close();
return result;
}
// 데이터 수정 담당 메소드
public int modify(ScoreDTO dto) throws SQLException
{
int result = 0;
String sql = "UPDATE TBL_SCORE SET NAME= ?, KOR = ?, ENG = ?, MAT = ? WHERE SID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getName());
pstmt.setInt(2, dto.getKor());
pstmt.setInt(3, dto.getEng());
pstmt.setInt(4, dto.getMat());
pstmt.setInt(5, Integer.parseInt(dto.getSid()));
result = pstmt.executeUpdate();
return result;
}
// 데이터 삭제 담당 메소드
public int remove(int sid) throws SQLException
{
int result = 0;
String sql = "DELETE FROM TBL_SCORE WHERE SID = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, sid);
result = pstmt.executeUpdate();
pstmt.close();
return result;
}
public void close()
{
DBConn.close();
}
}
Author And Source
이 문제에 관하여(PreparedStatement를 활용한 성적 처리 프로그램 구현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@cseon230/PreparedStatement를-활용해-성적-처리-프로그램-구현저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)