public class DBConnectionManager {
public static Connection connectDB() {
//DB ์ฐ๊ฒฐ ๊ด๋ จ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ก๋ฉ
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String db_url = "jdbc:oracle:thin:@localhost:1521:orcl";
String db_id = "scott";
String db_pw = "tiger";
conn = DriverManager.getConnection(db_url, db_id, db_pw);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void closeDB(Connection conn, PreparedStatement psmt, ResultSet rs) {
try {
if(conn != null) {
conn.close();
}
if(psmt != null) {
psmt.close();
}
if(rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //DB Connection ์ฐ๊ฒฐ ์ข
๋ฃ
}
}
public class StudentDTO {
//ํ๋๋ณ์
public int studno;
public String name;
public String id;
public int grade;
public String jumin;
public String birthday; //๋ ์ง
public String tel;
public int height;
public int weight;
public int deptno1;
public int deptno2;
public int profno;
//์์ฑ์
public StudentDTO() {
}
public StudentDTO(int studno, String name, String id, int grade, String jumin, String birthday, String tel, int height,
int weight, int deptno1, int deptno2, int profno) {
super();
this.studno = studno;
this.name = name;
this.id = id;
this.grade = grade;
this.jumin = jumin;
this.birthday = birthday;
this.tel = tel;
this.height = height;
this.weight = weight;
this.deptno1 = deptno1;
this.deptno2 = deptno2;
this.profno = profno;
}
//๋ฉ์๋
public int getStudno() {
return studno;
}
public void setStudno(int studno) {
this.studno = studno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public String getJumin() {
return jumin;
}
public void setJumin(String jumin) {
this.jumin = jumin;
}
public String getbirthday() {
return birthday;
}
public void setbirthday(String birthday) {
this.birthday = birthday;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getDeptno1() {
return deptno1;
}
public void setDeptno1(int deptno1) {
this.deptno1 = deptno1;
}
public int getDeptno2() {
return deptno2;
}
public void setDeptno2(int deptno2) {
this.deptno2 = deptno2;
}
public int getProfno() {
return profno;
}
public void setProfno(int profno) {
this.profno = profno;
}
}
๐จ
UPDATE, DELETE๋ INSERT์ ๋๊ฐ๋ค!! sql ๋ถ๋ถ๋ง ๋ค๋ฅด๊ฒ ์ฐ๊ณ ์ถ์๋ฐ๋ก ์ฐ๋๊ฑฐ๊ณ psmt.setInt(1, grade); ์ด๋ฐ ๋ถ๋ถ๋ ๋ฌ๋ผ์ง๋๊ฑฐ๊ณ !
๐จ
SELECT ๋ rs = psmt.executeQuery();
UPDATE, DELETE, INSERT๋ result = psmt.executeUpdate();
๐จ
SELECT : find
INSERT : save
UPDATE : modify
DELETE : remove ๋ก ๋ณ์๋ช
์ ์ง๋๋ค~!
public class StudentDAO {
//ํ๋๋ณ์
Connection conn;
PreparedStatement psmt;
ResultSet rs;
// 1. ๊ธฐ๋ณธ student ์ ์ฒด ์กฐํ ๋ฉ์๋
(SELECT)
public List<StudentDTO> findStudentList()
//2. ํ๋
(grade)์ ๋ฐ์์ ํด๋น ํ๋
student ๋ฐ์ดํฐ๋ฅผ ์กฐํํ๋ ๋ฉ์๋
(SELECT)
public List<StudentDTO> findStudentList(int grade)
//3. StudentDTO ๊ฐ์ฒด๋ฅผ ์ ๋ฌ๋ฐ์์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅ(Insert)ํ๋ ๋ฉ์๋
(INSERT)
public int saveStudent (StudentDTO studentDTO)
}
public List<StudentDTO> findStudentList() {
//DBConnectionManager ๋ง๋ค์ด์ค connection ์ ํ์ฉ
conn = DBConnectionManager.connectDB();
String sql = " SELECT studno, name, id, grade, jumin, TO_CHAR(birthday, 'YYYY-MM-DD') birthday, tel, height, weight, deptno1, deptno2, profno "
+ "FROM student " ;
List<StudentDTO> studentList = null;
try {
psmt = conn.prepareStatement(sql);
//Connection ํ์ฉํด์ sql ๋ช
๋ น์ ์คํํ๋ ๊ฐ์ฒด
rs = psmt.executeQuery(); //์ค๋น๋ sql ์ฟผ๋ฆฌ๋ฌธ ์คํ!
studentList = new ArrayList<StudentDTO>(); // ๋ฆฌ์คํธ์์ฑ
while(rs.next()) {
int studno = rs.getInt("studno");
String name = rs.getString("name");
String id = rs.getString("id");
int grade = rs.getInt("grade");
String jumin = rs.getString("jumin");
String birthday = rs.getString("birthday"); //DB์์ TO_CHAR๋ก String์ผ๋ก ๋ง๋ค์ด์ค๋ค์ ์ฌ์ฉ!
String tel = rs.getString("tel");
int height = rs.getInt("height");
int weight = rs.getInt("weight");
int deptno1 = rs.getInt("deptno1");
int deptno2 = rs.getInt("deptno2");
int profno = rs.getInt("profno");
StudentDTO stu = new StudentDTO(studno, name, id, grade, jumin, birthday, tel, height, weight, deptno1, deptno2, profno);
//ํ์ค ๋ฐ์ดํฐ ๊ฐ์ง๊ณ ์ด
studentList.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.closeDB(conn, psmt, rs);
}
return studentList;
}
public List<StudentDTO> findStudentList(int grade) {
//๊ฒฐ๊ณผ๊ฐ ํ๊ฐ์ธ์ง ์ฌ๋ฌ๊ฐ(List)์ธ์ง
//DBConnectionManager ๋ง๋ค์ด์ค connection ์ ํ์ฉ
conn = DBConnectionManager.connectDB();
String sql = " SELECT studno, name, id, grade, jumin, TO_CHAR(birthday, 'YYYY-MM-DD') birthday, tel, height, weight, deptno1, deptno2, profno "
+ " FROM student "
+ " WHERE grade = ? " ;
List<StudentDTO> studentList2 = null;
try {
psmt = conn.prepareStatement(sql);
//Connection ํ์ฉํด์ sql ๋ช
๋ น์ ์คํํ๋ ๊ฐ์ฒด
psmt.setInt(1, grade);
rs = psmt.executeQuery(); //์ค๋น๋ sql ์ฟผ๋ฆฌ๋ฌธ ์คํ!
studentList2 = new ArrayList<StudentDTO>();
while(rs.next()) {
int studno = rs.getInt("studno");
String name = rs.getString("name");
String id = rs.getString("id");
int grade2 = rs.getInt("grade");
String jumin = rs.getString("jumin");
String birthday = rs.getString("birthday"); //DB์์ TO_CHAR๋ก String์ผ๋ก ๋ง๋ค์ด์ค๋ค์ ์ฌ์ฉ!
String tel = rs.getString("tel");
int height = rs.getInt("height");
int weight = rs.getInt("weight");
int deptno1 = rs.getInt("deptno1");
int deptno2 = rs.getInt("deptno2");
int profno = rs.getInt("profno");
StudentDTO stu = new StudentDTO(studno, name, id, grade2, jumin, birthday, tel, height, weight, deptno1, deptno2, profno);
//ํ์ค ๋ฐ์ดํฐ ๊ฐ์ง๊ณ ์ด
studentList2.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.closeDB(conn, psmt, rs);
}
return studentList2;
}
public int saveStudent (StudentDTO studentDTO) {
//DBConnectionManager ๋ง๋ค์ด์ค connection ์ ํ์ฉ
conn = DBConnectionManager.connectDB();
String sql = " INSERT INTO student "
+ " VALUES ( ?, ?, ?, ?, ?, TO_DATE(?, 'YYYY-MM-DD'), ?, ?, ?, ?, ? ,? ) ";
int result = 0;
try {
psmt = conn.prepareStatement(sql);
//Connection ํ์ฉํด์ sql ๋ช
๋ น์ ์คํํ๋ ๊ฐ์ฒด
psmt.setInt(1, studentDTO.getStudno());
psmt.setString(2, studentDTO.getName());
psmt.setString(3, studentDTO.getId());
psmt.setInt(4, studentDTO.getGrade());
psmt.setString(5, studentDTO.getJumin());
psmt.setString(6, studentDTO.getbirthday()); //String //TO_DATE('2023-05-01', 'YYYY-MM-DD')
psmt.setString(7, studentDTO.getTel());
psmt.setInt(8, studentDTO.getHeight());
psmt.setInt(9, studentDTO.getWeight());
psmt.setInt(10, studentDTO.getDeptno1());
psmt.setInt(11, studentDTO.getDeptno2());
psmt.setInt(12, studentDTO.getProfno());
result = psmt.executeUpdate(); //์ค๋น๋ sql ์ฟผ๋ฆฌ๋ฌธ ์คํ!
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.closeDB(conn, psmt, rs);
}
return result;
}
public int modifyPersonInfo(PersonInfoDTO personInfo) { //ํฌ์ฅ๋ฐ์ค(DTO)๋ถ๋ฌ์ค๋ฉด ๊ทธ์์ ์๋ id name ์ฌ์ฉ๊ฐ๋ฅ!
//ํด๋น ์์ด๋์ ๋ง๋ ์ฌ๋์ ์ด๋ฆ์ ์์ !
conn = DBConnectionManager.connectDB();
String sql = " UPDATE t_person_info "
+ " SET name = ? "
+ " WHERE id = ? ";
int result = 0;
try {
psmt = conn.prepareStatement(sql);
//Connection ํ์ฉํด์ sql ๋ช
๋ น์ ์คํํ๋ ๊ฐ์ฒด
psmt.setString(1, personInfo.getName()); //ํฌ์ฅ๋ฐ์ค(DTO) ์์ ์๋ id
psmt.setInt(2, personInfo.getId()); //ํฌ์ฅ๋ฐ์ค(DTO) ์์ ์๋ name
result = psmt.executeUpdate(); // 1, 0
//rs = psmt.executeQuery(); //์ค๋น๋ sql ์ฟผ๋ฆฌ๋ฌธ ์คํ! -> SELECT๋ฌธ์ผ๋!
/*
SELECT ์ฟผ๋ฆฌ : psmt.excuteQuery(); -> ๊ฒฐ๊ณผ๋ก ResultSet
INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ : psmt.excuteUpdate();
-> ๊ฒฐ๊ณผ : ์ ์ฉ๋ ํ์ ์ซ์
*/
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.closeDB(conn, psmt, rs);
}
return result;
}
public int removePersonInfoById(int id) {
//ํด๋น ์์ด๋์ ๋ง๋ ์ฌ๋์ ์ด๋ฆ์ ์์ !
conn = DBConnectionManager.connectDB();
String sql = " DELETE FROM t_person_info "
+ " WHERE id = ? ";
int result = 0;
try {
psmt = conn.prepareStatement(sql);
//Connection ํ์ฉํด์ sql ๋ช
๋ น์ ์คํํ๋ ๊ฐ์ฒด
psmt.setInt(1, id);
result = psmt.executeUpdate(); // 1, 0
//rs = psmt.executeQuery(); //์ค๋น๋ sql ์ฟผ๋ฆฌ๋ฌธ ์คํ! -> SELECT๋ฌธ์ผ๋!
/*
SELECT ์ฟผ๋ฆฌ : psmt.excuteQuery(); -> ๊ฒฐ๊ณผ๋ก ResultSet
INSERT, UPDATE, DELETE ์ฟผ๋ฆฌ : psmt.excuteUpdate();
-> ๊ฒฐ๊ณผ : ์ ์ฉ๋ ํ์ ์ซ์
*/
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnectionManager.closeDB(conn, psmt, rs);
}
return result;
}