Java 8 ์ ์ด์ฉํด ์จ๋ผ์ธ ๊ต์ก ํ๋ซํผ์ ์ฃผ์ ๋ก ์ ์ , ์ดํด๋ฆฝ์ค๋ฅผ ํ์ฉํ์ฌ JDBC์ฐ๊ฒฐ, Oracle 8 ๋ฒ์ SQL ์ฟผ๋ฆฌ ๋ก์ง ์์ฑ์ ์๋ฃํ์์ต๋๋ค. ๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ, ์์ฑ์ ์ค๋ฒ๋ผ์ด๋ฉ, ์์ธ์ฒ๋ฆฌ, ์ปค๋ฅ์ ํ์ฉ์ ํตํ์ฌ ์๋ฐ ๋ฐฑ์๋ ๊ธฐ๋ฅ์ ๊ตฌํํ์์ต๋๋ค.
๋น๋๋ฉด ์๋น์ค ์๋์ ๋ง์ถฐ์ ์จ๋ผ์ธ ๊ต์ก ํ๋ซํผ ์ ์์ ์๊ฐํ๊ฒ ๋์์ต๋๋ค.
์ง๊ธ๊น์ง์ ๊ฐ์๋ฅผ ๋ฐํ์ผ๋ก ์๋น์ค ํด๋์ค๋ฅผ ํ์ฅํ DAO ๊ตฌํ๊ณผ DB ํ ์ด๋ธ ์ค๊ณ ๊ณผ์ ์ ํ๋ก์ ํธ๋ก ๊ตฌํํด๋ณด๊ณ ์ ์ ์ํด๋ณด๊ฒ ๋์์ต๋๋ค.
-
๋ณ๊ฒฝ ์
- ์กํฐ ๊ธฐ์ค ์ ์ค์ผ์ด์ค ๋ค์ด์ด๊ทธ๋จ ์์ฑ โ ๊ธฐ๋ฅ์ ์ง๊ด์ ์ผ๋ก ํ์ธํ๊ธฐ ์ด๋ ต๋ค๋ ์ + ํด๋์ค ์์ฃผ ํ์ธ์ด ์ด๋ ต๋ค๋ ์ โ ์๋กญ๊ฒ ์ฌ์์ฑ
-
๋ณ๊ฒฝ ํ
ํด๋์ค ๊ธฐ๋ฅ๋ณ๋ก ๋ค์ด์ด๊ทธ๋จ ์ฌ์์ฑ + ๊ธฐ์กด CRUD ๊ธฐ๋ฅ๊ณผ ์ถ๊ฐ ๊ธฐ๋ฅ์ ๋ถ๋ฆฌ
-
๋ณ๊ฒฝ ์
-
๋ณ๊ฒฝ ํ
-
์ด๊ธฐ DB ๋ชจ๋ธ๋ง ๋๋ ์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ํ์ฉ๋๋ ์ปฌ๋ผ ๋ช ์ ๊ณ ๋ คํ์ง ๋ชปํจ. like ์ฐ์ฐ์๋ check๋ฅผ ์ธ์ X
-
์ ๋ํฌํ ์ปฌ๋ผ์ ์ ์ธํ ๋๋จธ์ง๋ค์ ์ง๊ด์ฑ์ ์ํด ์ปฌ๋ผ๋ช ์ ์์ ํ์๊ณ ๋ฐ์ดํฐ ํ์ ์ ์ฌ์ด์ฆ๋ฅผ ์๋ง๊ฒ ์ง์ ํด์ฃผ์๋ค.
ํ์๊ณผ ์ ์๋์ ํ ์ด๋ธ์ ๊ตฌ๋ถํ์์ผ๋ ์์ ๊ด๊ณ๋ฅผ ํตํด์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ธฐ ์ํด ์์ ๊ฐ์ด ํด๋์ค ๊ด๊ณ๋ฅผ ์ค์ ํ๋ค.
-
์์ธ ํ๋ฉด
1) ๊ฐ์์ ์ฐฝ
๊ฐ์์๊ฐ ๋ณธ์ธ์ ๊ฐ์ ID์ ๊ฐ์๋ช , ๊ณผ๋ชฉ, ๊ฐ๊ฒฉ์ ์์ฑํด ๋ฑ๋ก ๋ฐ ๋ณ๊ฒฝํ๋ ํ๋ฉด
๋ณธ์ธ์ด ๋ด๋นํ๊ณ ์๋ ๊ณผ๋ชฉ ์กฐํ
2) ํ์์ฐฝ - ์๊ฐ์ค์ธ ๊ฐ์ ๋ชฉ๋ก
์๊ฐ์์ด ์๊ฐ ์ค์ธ ๊ฐ์ ๋ชฉ๋ก, ๊ฐ์์๊ฐ, ์ฒ ํํ๋ ํ๋ฉด
์ ์๋ ์ด๋ฆ, ๊ฐ์๋ช , ์ข์์ ์ ํ์
CRUD
- ๊ฐ์์๊ฐ ๋ณธ์ธ์ ๊ฐ์ ID์ ๊ฐ์๋ช , ๊ณผ๋ชฉ, ๊ฐ๊ฒฉ์ ์์ฑํด ๋ฑ๋ก ๋ฐ ๋ณ๊ฒฝ
- ๋ณธ์ธ์ด ๋ด๋นํ๊ณ ์๋ ๊ณผ๋ชฉ ์กฐํ
- ํ์์ด ์๊ฐ ์ค์ธ ๊ณผ๋ชฉ ์ ๋ณด ์กฐํ
- ๊ณผ๋ชฉ๋ณ ์ ์๋ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
- ์ ์๋ id๋ก ๊ฐ์ ๋ชฉ๋ก ์กฐํ
- ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ
- ์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์์ ๊ฐ์ ์ญ์
- ์ ์๋ ๋ฑ๋ก, ์กฐํ, ๋ณ๊ฒฝ
์ถ๊ฐ ๊ธฐ๋ฅ
- ์๊ฐ์์ ํน์ ๊ฐ์ ์ข์์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
- ํน์ ์ ์๋ ๊ฐ์์ ์๊ฐ์ 3๊ฐ ์ด์ ์๊ฐ ์ค์ด๋ฉด ๊ฐ์ ํ ์ธ
- ๊ฐ์ id๋ก ์ข์์ ํ์
-
๊ตฌ์กฐ
-
com.edu.vo
- User
- Student
- Teacher
-
com.edu.exception
- IdNotFoundException
- DuplicateException
-
com.edu.dao
- EduDAO
- com.edu.dao.impl
- EduDAOImpl
-
com.edu.test
- EduTest
-
config
- ServcerInfo(interface)
-
-
์ํ์ค ์ฟผ๋ฆฌ๋ฌธ
package com.edu.dao;
import java.util.ArrayList;
import com.edu.vo.User;
public interface EduDAO {
//๊ณตํต ์ปค๋ฅ์
์ฐ๊ฒฐ๋ถ
public Connection getConnection() throws SQLException;
// ์กด์ฌ์ฌ๋ถ ํ์ธ
public int studentIdExist(int id, Connection conn);
public int teacherIdExist(int id, Connection conn);
public int lectureIdExist(int id);
//close ๋ฉ์๋
public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception;
public void closeAll(PreparedStatement ps, Connection conn) throws Exception;
//**๊ณตํต ๊ธฐ๋ฅ** (๊ฐ๋น)
public ArrayList<User> FindAllTeacher(); //์ ์๋ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
public ArrayList<User> FindTeacherBySubject(String subject); //๊ณผ๋ชฉ๋ณ ์ ์๋ ์ ์ฒด ๋ชฉ๋ก ์กฐํ
public ArrayList<Lecture> FindLectureByTeacher(int teacherId); //์ ์๋ id๋ก ๊ฐ์ ๋ชฉ๋ก ์กฐํ
public Lecture FindLectureById(int lectureId); // ๊ฐ์๋ช
id๋ก ๊ฐ์ ์กฐํ
public ArrayList<Lecture> FindBestLectures(); // ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ
//๊ณตํต ๊ธฐ๋ฅ ์ ๋ถ : throw IdNotFoundException -> IdExist ๋ฉ์๋ ํธ์ถ ํ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ
//**๊ด๋ฆฌ์** (์์ฑ)
public void addTeacher(Teacher teacher);
// ์ ์๋ ๋ฑ๋ก -> ์ด๋ฉ์ผ์ด ๋์ผํ๋ฉด throw AlreadyExistException
public void updateTeacher(Teacher teacher); // ์ ์๋ ๋ณ๊ฒฝ
public void deleteTeacher(int teacherId); // ์ ์๋ ์ญ์
//์ ๋๊ฐ ๋ฉ์๋ ์ ๋ถ : throw IdNotFoundException -> IdExist ๋ฉ์๋ ํธ์ถ ํ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ
//**๊ฐ์์** (์กํฌ)
public void addLecture(Lecture lecture);
public void updateLecture(Lecture lecture);
//throw IdNotFoundException
//**์๊ฐ์**
//(์กํฌ)
public ArrayList<Lecture> getAllMyLecture(int studentId); // id๋ก ์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์กฐํ
// throw IdNotFoundException
public void applyLecture(int studentId, Lecture lecture);
//์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์ ๊ฐ์ ์ถ๊ฐ -> getAllMyLecture ๋ฉ์๋ ํธ์ถํด์ ๊ฐ์ ๋ชฉ๋ก ์ถ๋ ฅ
// throw AlreadyExistException
public void cancleLecture(int studentId, Lecture lecture); // ์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์์ ๊ฐ์ ์ญ์
// throw IdNotFoundException
//(์์ฑ)
public boolean likePossible(int studentId, int lectureId); // ์๊ฐ์๊ฐ ๊ฐ์ ์ข์์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
// throw IdNotFoundException( ์๊ฐ์ id๋ ๊ฐ์ id ๋๋ค ํ์ธ)
public void likeLecture(int lectureId); // ๊ฐ์ id๋ก ์ข์์ ํ์
// likePossible ํธ์ถํด์ true ์ด๋ฉด likeLecture ํธ์ถ
// likePossible ํธ์ถํด์ false์ด๋ฉด ๋ฉ์๋ ๋๊ฐ๊ธฐ
public boolean discountPossible(int studentId, int teacherId);
// ์ ์๋ ๊ฐ์๋ฅผ ์๊ฐ์๊ฐ 3๊ฐ ์ด์ ์๊ฐ ์ค์ด๋ฉด ๊ฐ์ ํ ์ธ
// throw IdNotFoundException
}
-
student
ํ ์ด๋ธ ์์ฑCREATE TABLE student ( student_id number(38), student_name VARCHAR2(50), address VARCHAR2(255), phone VARCHAR2(30), student_email VARCHAR2(40) );
๊ธฐ๋ณธํค ์ ์ฝ์กฐ๊ฑด
ALTER TABLE student ADD CONSTRAINT student_student_id_pk PRIMARY KEY(student_id); DESC student ON DELETE CASCADE;
-
lecture
ํ ์ด๋ธ ์์ฑCREATE TABLE lecture( lecture_id number(38), lecture_name VARCHAR2(50), subject VARCHAR2(255), likes number(38), price number(38) );
๊ธฐ๋ณธํค ์ ์ฝ์กฐ๊ฑด
ALTER TABLE lecture ADD CONSTRAINT lecture_lecture_id_pk PRIMARY KEY(lecture_id); DESC lecture ON DELETE CASCADE;
-
์ ์ฉ๊ฐ๋ฅํ ๊ธฐ๋ฅ
์ฌ๋ฌ๊ฐ์ง ์์ฑํด๋ณด๋ฉด ๊ณผ๋ชฉ๋ณ 1-3์ ๊ฐ์ ๋ชฉ๋ก ์ถ์ถ, ๊ฐ์ ์ ์ฒด ์ค 1-5์ ๊ฐ์ ๋ชฉ๋ก ์ถ์ถ, ์ ์๋๋ณ ์ธ๊ธฐ ๊ฐ์ฌ 1์-3์ ๋ชฉ๋ก ์ถ์ถ
ROW_NUMBER()
,RANK()
- ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ
FindBestLectures
: ROW_NUMBER() ํจ์
: ๊ฐ์ ์ ์ฒด ์ค 1-5์ ๊ฐ์ ๋ชฉ๋ก ์ถ์ถ (์ผ๋จ 3๊ฐ๋ง)
SELECT lecture_id, name, subject, likes, price FROM (SELECT lecture_id, name, subject, likes, price, ROW_NUMBER() OVER(ORDER BY likes DESC) AS ranking FROM lecture l) WHERE ranking <= 3
- ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ
-
์ ์ฉ๊ฐ๋ฅํ ๊ธฐ๋ฅ
์ข์์ ์๊ฐ ๊ฐ์ฅ ๋ง์ ๊ฐ์ โ โ์ธ๊ธฐ๊ฐ์โ , ๊ทธ๋ ์ง ์์ ๊ฐ์ โ โ๋ง์กฑ๋๊ฐ ๋จ์ด์ง๋ ๊ฐ์โ
- ์๊ฐ์์ ๊ฐ์ ๊ฐ๊ฒฉ ์ดํฉ โ ํ์ ๋ฑ๊ธ ๊ตฌ๋ณ :
getAllMyLecture
- ์๊ฐ์์ ๊ฐ์ ๊ฐ๊ฒฉ ์ดํฉ โ ํ์ ๋ฑ๊ธ ๊ตฌ๋ณ :
- ์ ์ฉ๊ฐ๋ฅํ ๊ธฐ๋ฅ
discountPossible
๊ฐ์ ๋ฑ๋ก, ๋ณ๊ฒฝ, ์ญ์ , ์กฐํ
์ ์๋ ๋ฑ๋ก, ๋ณ๊ฒฝ
์กด์ฌ ์ ๋ฌด ํ์ธ ๋ฉ์๋๋
์กฐํ ๊ธฐ๋ฅ ๋ฉ์๋์ ์์ด์ ์ค๋ฒ๋ก๋ฉ ์ ์ฉ.
vo ํด๋์ค ๋ด์์ ์์ฑ์ ์ค๋ฒ๋ก๋ฉ์ ์ ์ฉ.
-
์ ์ฉ๊ฐ๋ฅํ ๊ธฐ๋ฅ
๊ฐ์ ์กฐํ
discountPossible
์์ด๋ ๋ณ ๊ฐ์ ์กฐํ
SELECT lecture_id, name,subject,likes,to_char(price,'999,999,999') FROM lecture WHERE lecture_id=?;
(1) DAO์ DAOImpl
-
DAO ์์ค ์ฝ๋
package com.edu.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.edu.vo.Lecture; import com.edu.vo.Student; import com.edu.vo.Teacher; import com.edu.vo.User; public interface EduDAO { //๊ณตํต ์ปค๋ฅ์ ์ฐ๊ฒฐ๋ถ public Connection getConnection() throws SQLException; // ์กด์ฌ์ฌ๋ถ ํ์ธ //private boolean studentIdExist(int id); //private boolean teacherIdExist(int id); //private boolean lectureIdExist(int id); //close ๋ฉ์๋ public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception; public void closeAll(PreparedStatement ps, Connection conn) throws Exception; //**๊ณตํต ๊ธฐ๋ฅ** (๊ฐ๋น) public ArrayList<User> findAllTeacher() throws Exception; //์ ์๋ ์ ์ฒด ๋ชฉ๋ก ์กฐํ public ArrayList<User> findTeacherBySubject(String subject) throws Exception; //๊ณผ๋ชฉ๋ณ ์ ์๋ ์ ์ฒด ๋ชฉ๋ก ์กฐํ public ArrayList<Lecture> findLectureByTeacher(int teacherId) throws Exception; //์ ์๋ id๋ก ๊ฐ์ ๋ชฉ๋ก ์กฐํ public Lecture findLectureById(int lectureId) throws Exception; // ๊ฐ์๋ช id๋ก ๊ฐ์ ์กฐํ public ArrayList<Lecture> findBestLectures() throws Exception; // ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ //๊ณตํต ๊ธฐ๋ฅ ์ ๋ถ : throw IdNotFoundException -> IdExist ๋ฉ์๋ ํธ์ถ ํ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ //**๊ด๋ฆฌ์** (์์ฑ) public void addTeacher(Teacher teacher) throws Exception; // ์ ์๋ ๋ฑ๋ก -> ์ด๋ฉ์ผ์ด ๋์ผํ๋ฉด throw AlreadyExistException public void updateTeacher(Teacher teacher) throws Exception; // ์ ์๋ ๋ณ๊ฒฝ public void deleteTeacher(int teacherId) throws Exception; // ์ ์๋ ์ญ์ //์ ๋๊ฐ ๋ฉ์๋ ์ ๋ถ : throw IdNotFoundException -> IdExist ๋ฉ์๋ ํธ์ถ ํ ์กด์ฌํ์ง ์์ผ๋ฉด ์์ธ //**๊ฐ์์** (์กํฌ) public void addLecture(Lecture lecture) throws Exception; public void updateLecture(Lecture lecture) throws Exception; //throw IdNotFoundException //**์๊ฐ์** //(์กํฌ) public ArrayList<Student> getAllStudents() throws Exception; public String getStudentLectures(int studentId) throws Exception; public ArrayList<Lecture> getAllMyLecture(int studentId) throws Exception; // id๋ก ์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์กฐํ // throw IdNotFoundException public void applyLecture(int studentId, Lecture lecture) throws Exception; //์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์ ๊ฐ์ ์ถ๊ฐ -> getAllMyLecture ๋ฉ์๋ ํธ์ถํด์ ๊ฐ์ ๋ชฉ๋ก ์ถ๋ ฅ // throw AlreadyExistException public void cancleLecture(int studentId, Lecture lecture) throws Exception; // ์๊ฐ์ ๊ฐ์ ๋ชฉ๋ก์์ ๊ฐ์ ์ญ์ // throw IdNotFoundException //(์์ฑ) public boolean likePossible(int studentId, int lectureId) throws Exception; // ์๊ฐ์๊ฐ ๊ฐ์ ์ข์์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ // throw IdNotFoundException( ์๊ฐ์ id๋ ๊ฐ์ id ๋๋ค ํ์ธ) public void likeLecture(int studentId, int lectureId) throws Exception; // ๊ฐ์ id๋ก ์ข์์ ํ์ // likePossible ํธ์ถํด์ true ์ด๋ฉด likeLecture ํธ์ถ // likePossible ํธ์ถํด์ false์ด๋ฉด ๋ฉ์๋ ๋๊ฐ๊ธฐ //public boolean discountPossible(int studentId, int teacherId); // ์ ์๋ ๊ฐ์๋ฅผ ์๊ฐ์๊ฐ 3๊ฐ ์ด์ ์๊ฐ ์ค์ด๋ฉด ๊ฐ์ ํ ์ธ // throw IdNotFoundException public int discountLecture(String subject, int discount) throws Exception; //ํน์ subject์ ๊ฐ๊ฒฉ ์ด ํฉ์ ํ ์ธ์จ์ ์ ์ฉํ์ฌ ํจํค์ง ๊ฐ๊ฒฉ์ ์๋ ค์ค๋ค. //SELECT SUM(price) FROM lecture WHERE subject='์ํ'; }
-
DAOImpl ์์ค์ฝ๋
package com.edu.dao.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import com.edu.dao.EduDAO; import com.edu.exception.AlreadyExistException; import com.edu.exception.IdNotFoundException; import com.edu.vo.Lecture; import com.edu.vo.Teacher; import com.edu.vo.User; import config.ServerInfo; public class EduDAOImpl implements EduDAO { private static EduDAOImpl dao = new EduDAOImpl(); private EduDAOImpl() { System.out.println("EduDAOImpl Creating...Using Singletone"); } public static EduDAOImpl getInstance() { return dao; } @Override public Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection(ServerInfo.URL, ServerInfo.USER, ServerInfo.PASSWORD); System.out.println("DB Connect..."); return conn; } private boolean teacherIdExist(int id, Connection conn) throws SQLException { String query = "SELECT teacher_id FROM teacher WHERE teacher_id=?"; PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); return rs.next(); } private boolean lectureIdExist(int id, Connection conn) throws SQLException { String query = "SELECT lecture_id FROM lecture WHERE lecture_id=?"; PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); return rs.next(); } //๊ฐ๋น ์ฝ๋ private boolean lectureSubjectExist(String subject,Connection conn)throws SQLException{ PreparedStatement ps = null; ResultSet rs = null; String query = "SELECT subject FROM lecture WHERE subject=?"; ps = conn.prepareStatement(query); ps.setString(1, subject); rs = ps.executeQuery(); return rs.next(); } //์กํฌ ์ฝ๋ private boolean studentIdExist(int id, Connection conn) throws SQLException { String query = "SELECT STUDENT_ID FROM STUDENT WHERE STUDENT_ID = ?"; PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); boolean flag= rs.next(); System.out.println("์๊ฐ์ ์์ด๋ ์กฐํ๊ฒฐ๊ณผ : "+ flag); return flag; } private boolean learningIdExist(int lectureId, int studentId, Connection conn) throws SQLException { String query = "SELECT DISTINCT student_id FROM learning WHERE student_id = ? AND lecture_id = ?"; PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lectureId); ResultSet rs = ps.executeQuery(); boolean flag= rs.next(); System.out.println("๊ต์ก ํ ์ด๋ธ ์์ด๋ ์กฐํ๊ฒฐ๊ณผ : "+ flag); return flag; } private boolean learningIdExist(int studentId, Connection conn) throws SQLException { String query = "SELECT DISTINCT student_id FROM learning WHERE student_id = ?"; PreparedStatement ps = conn.prepareStatement(query); ps.setInt(1, studentId); ResultSet rs = ps.executeQuery(); boolean flag= rs.next(); System.out.println("๊ต์ก ํ ์ด๋ธ ์์ด๋ ์กฐํ๊ฒฐ๊ณผ : "+ flag); return flag; } @Override public void closeAll(ResultSet rs, PreparedStatement ps, Connection conn) throws Exception { if(rs!=null) rs.close(); closeAll(ps, conn); } @Override public void closeAll(PreparedStatement ps, Connection conn) throws Exception { if(ps!=null) ps.close(); if(conn!=null) conn.close(); } /////////////////////////// ๊ณตํต ๊ธฐ๋ฅ ////////////////////////////// @Override public ArrayList<User> findAllTeacher() throws Exception{ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<User> list = new ArrayList<User>(); try { conn = getConnection(); String query ="SELECT teacher_id, name, email,subject FROM teacher"; ps = conn.prepareStatement(query); rs = ps.executeQuery(); while(rs.next()){ list.add(new Teacher(rs.getInt("teacher_id"), rs.getString("name"), rs.getString("email"), rs.getString("subject") )); } }finally { closeAll(rs, ps, conn); } return list; } @Override public ArrayList<User> findTeacherBySubject(String subject) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<User> list = new ArrayList<User>(); try { conn = getConnection(); if(lectureSubjectExist(subject,conn)) { String query ="SELECT teacher_id, name, email,subject FROM teacher WHERE subject=?"; ps = conn.prepareStatement(query); ps.setString(1, subject); rs = ps.executeQuery(); while(rs.next()){ list.add(new Teacher(rs.getInt("teacher_id"), rs.getString("name"), rs.getString("email"), rs.getString("subject") )); } }else { throw new IdNotFoundException("ํด๋น ๊ณผ๋ชฉ์ ๋ด๋นํ๋ ์ ์๋์ด ์์ต๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; } @Override public ArrayList<Lecture> findLectureByTeacher(int teacherId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<Lecture>(); try { conn = getConnection(); if(teacherIdExist(teacherId,conn)) { String query ="SELECT lecture_id,name,subject,likes,to_char(price,'9,999,999') FROM lecture WHERE lecture_id in (SELECT lecture_id FROM education where teacher_id=?)"; ps = conn.prepareStatement(query); ps.setInt(1, teacherId); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture(rs.getInt("lecture_id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("to_char(price,'9,999,999')") )); } }else { throw new IdNotFoundException("๋ฑ๋ก๋ ์ ์๋์ด ์๋๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; } @Override public Lecture findLectureById(int lectureId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Lecture lecture = new Lecture(); try { conn = getConnection(); if(lectureIdExist(lectureId,conn)) { String query ="SELECT lecture_id, name,subject,likes,to_char(price,'9,999,999') FROM lecture WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, lectureId); rs = ps.executeQuery(); if(rs.next()){ lecture.setId(lectureId); lecture.setName(rs.getString("name")); lecture.setSubject(rs.getString("subject")); lecture.setLikes( rs.getInt("likes")); lecture.setPrice(rs.getString("to_char(price,'9,999,999')")); } }else { throw new IdNotFoundException("์กด์ฌํ์ง ์๋ ๊ฐ์์ ๋๋ค."); } }finally { closeAll(rs, ps, conn); } return lecture; } @Override public ArrayList<Lecture> findBestLectures() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<Lecture>(); try { conn = getConnection(); String query ="SELECT lecture_id, name, subject, likes, to_char(price,'9,999,999')" + "FROM (SELECT lecture_id, name, subject, likes, price, " + "ROW_NUMBER() OVER(ORDER BY likes DESC) AS ranking FROM lecture l) " + "WHERE ranking <= 3"; ps = conn.prepareStatement(query); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture(rs.getInt("lecture_id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("to_char(price,'9,999,999')") )); } }finally { closeAll(rs, ps, conn); } return list; } /////////////////////////// ๊ด๋ฆฌ์ ๊ธฐ๋ฅ (์์ฑ) /////////////////////// @Override public void addTeacher(Teacher teacher) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(!teacherIdExist(teacher.getId(), conn)) { //๋ฑ๋กํ๋ ค๋ ๊ฐ์์๊ฐ ์์ง ์์ผ๋ฉด String query = "INSERT INTO teacher(teacher_id, name, email, subject) VALUES(seq_teacher.nextVal,?,?,?)"; ps= conn.prepareStatement(query); ps.setString(1, teacher.getName()); ps.setString(2, teacher.getEmail()); ps.setString(3, teacher.getSubject()); System.out.println(ps.executeUpdate()+" ๋ช INSERT ์ฑ๊ณต...addTeacher().."); }else { throw new AlreadyExistException(); } }finally { closeAll(ps, conn); } } @Override public void updateTeacher(Teacher teacher) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE teacher SET name=?, email=?, subject=? WHERE teacher_id=?"; ps = conn.prepareStatement(query); ps.setString(1, teacher.getName()); ps.setString(2, teacher.getEmail()); ps.setString(3, teacher.getSubject()); ps.setInt(4, teacher.getId()); int row = ps.executeUpdate(); if(row==1)System.out.println(row+" ๋ช UPDATE OK...updateTeacher()..."); else throw new IdNotFoundException("์์ ํ ๊ฐ์์๊ฐ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } } @Override public void deleteTeacher(int teacherId) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(teacherIdExist(teacherId, conn)) { //ํด๋น ๊ฐ์์ ์กด์ฌํ๋ฉด ์ญ์ String query = "DELETE teacher WHERE teacher_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, teacherId); System.out.println(ps.executeUpdate()+" ๋ช DELETE OK...deleteTeacher().."); }else { throw new IdNotFoundException("์ญ์ ํ ๊ฐ์์๊ฐ ์์ต๋๋ค "); } }finally { closeAll(ps, conn); } } /////////////////////////// ๊ด๋ฆฌ์ ๊ธฐ๋ฅ (์์ฑ) /////////////////////// @Override public void addLecture(Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "INSERT INTO lecture(lecture_id, name, subject, likes, price) VALUES (seq_lecture.nextVal, ?, ?, ?, ?)"; ps = conn.prepareStatement(query); ps.setString(1, lecture.getName()); ps.setString(2, lecture.getSubject()); ps.setInt(3, lecture.getLikes()); ps.setString(4, lecture.getPrice()); System.out.println(ps.executeUpdate()+ "๊ฐ ๊ฐ์ ๋ฑ๋ก๋์์ต๋๋ค."); }finally { closeAll(ps, conn); } } @Override public void updateLecture(Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(lectureIdExist(lecture.getId(), conn)) { String query = "UPDATE lecture SET name = ?, subject = ?, price = ? WHERE lecture_id = ?"; ps = conn.prepareStatement(query); ps.setString(1, lecture.getName()); ps.setString(2, lecture.getSubject()); ps.setString(3, lecture.getPrice()); ps.setInt(4, lecture.getId()); System.out.println(ps.executeUpdate()+ "๊ฐ ๊ฐ์๊ฐ ์์ ๋์์ต๋๋ค."); }else { throw new IdNotFoundException("์กด์ฌํ๋ lecture id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } } @Override public ArrayList<Lecture> getAllMyLecture(int studentId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<>(); try { conn = getConnection(); if(studentIdExist(studentId, conn)){ if(learningIdExist(studentId, conn)) { conn = getConnection(); String query = "SELECT lecture.lecture_id AS id, lecture.name AS name, lecture.subject AS subject, lecture.likes AS likes, lecture.price AS price" + " FROM learning, lecture" + " WHERE learning.lecture_id = lecture.lecture_id AND learning.student_id = ?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture( rs.getInt("id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("price"))); } } }else{ throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; } @Override public void applyLecture(int studentId, Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; System.out.println("ํ์์์ด๋ "+studentId); try { conn = getConnection(); if(studentIdExist(studentId, conn)) { if(!learningIdExist(lecture.getId(), studentId, conn)) { conn = getConnection(); String query = "INSERT INTO learning(student_id, lecture_id, flag) VALUES(?, ?, ?)"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lecture.getId()); ps.setInt(3, 0); System.out.println(ps.executeUpdate()+"๊ฐ ๊ฐ์๊ฐ ์ง์๋์์ต๋๋ค."); }else { System.out.println("์ด๋ฏธ ์๊ฐ์ ์ฒญํ ์ด๋ ฅ์ด ์กด์ฌํฉ๋๋ค."); } }else { throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } } @Override public void cancleLecture(int studentId, Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(learningIdExist(lecture.getId(),studentId, conn)) { conn = getConnection(); String query = "DELETE learning WHERE student_id = ? AND lecture_id = ?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lecture.getId()); System.out.println(ps.executeUpdate()+"๊ฐ ๊ฐ์๊ฐ ์ทจ์๋์์ต๋๋ค."); getAllMyLecture(studentId); }else { throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } } /////////////////////////// ๊ด๋ฆฌ์ ๊ธฐ๋ฅ (์์ฑ) /////////////////////// @Override public boolean likePossible(int studentId, int lectureId) throws Exception { //๊ฐ์-ํ์ -> learning //๋ง์ฝ learning์ flag ๊ฐ์ด 0์ด๋ฉด -> ์ข์์ ๋๋ฅด๊ธฐ ๊ฐ๋ฅ, true ๋ฆฌํด //๋ง์ฝ learning์ flag ๊ฐ์ด 1์ด๋ฉด -> ์ข์์ ๋๋ฅด๊ธฐ ๋ถ๊ฐ๋ฅ, false ๋ฆฌํด boolean flag = false; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(); String query = "SELECT flag FROM learning WHERE student_id=? and lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lectureId); rs = ps.executeQuery(); if(rs.next()) { if(rs.getInt("flag")==0) flag = true; } } finally { closeAll(rs, ps, conn); } return flag; } // ์๊ฐ์๊ฐ ๊ฐ์ ์ข์์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ // throw IdNotFoundException( ์๊ฐ์ id๋ ๊ฐ์ id ๋๋ค ํ์ธ) private void setFlag(int studentId, int lectureId) throws Exception { //learning ํ ์ด๋ธ์ flag 0->1 Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE learning SET flag=? WHERE student_id=? and lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, 1); ps.setInt(2, studentId); ps.setInt(3, lectureId); int row = ps.executeUpdate(); if(row==1)System.out.println("flag ๋ณ๊ฒฝ ์ฑ๊ณต"); else throw new IdNotFoundException("flag ๋ณ๊ฒฝํ ์ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } } //ํน์ ๊ฐ์์ ์ธ๊ธฐ๋ ๊ฐ์ ธ์ค๊ธฐ public int getLikes(int lectureId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; int likes = 0; try { conn = getConnection(); if(lectureIdExist(lectureId, conn)) { //๊ฐ์ ์กด์ฌํ๋ฉด ์ธ๊ธฐ๋ ๊ฐ์ ธ์ค๊ธฐ String query = "SELECT likes FROM lecture WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, lectureId); rs = ps.executeQuery(); if(rs.next()) { likes = rs.getInt("likes"); } }else { throw new IdNotFoundException("์ธ๊ธฐ๋๋ฅผ ๊ฐ์ ธ์ฌ ๊ฐ์๊ฐ ์์ต๋๋ค "); } } finally { closeAll(rs, ps, conn); } return likes; } @Override public void likeLecture(int studentId, int lectureId) throws Exception { if(!likePossible(studentId, lectureId)) return; //์ข์์ ๋๋ฅด๊ธฐ ๋ถ๊ฐ๋ฅํ๋ฉด ๋ int nowLikes = getLikes(lectureId); //ํ์ฌ ์ธ๊ธฐ๋+1 -> db์ ์ ๋ฐ์ดํธ Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE lecture SET likes=? WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, nowLikes+1); ps.setInt(2, lectureId); int row = ps.executeUpdate(); if(row==1)System.out.println("์ธ๊ธฐ๋ ์ฌ๋ฆฌ๊ธฐ ์ฑ๊ณต"); else throw new IdNotFoundException("์ธ๊ธฐ๋ ์ฌ๋ฆด ๊ฐ์๊ฐ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } //flag 0->1๋ก setFlag(studentId, lectureId); } @Override public int discountLecture(String subject, int discount) throws Exception { //ํน์ subject์ ๊ฐ๊ฒฉ ์ด ํฉ์ ํ ์ธ์จ์ ์ ์ฉํ์ฌ ํจํค์ง ๊ฐ๊ฒฉ์ ์๋ ค์ค๋ค. //SELECT SUM(price) FROM lecture WHERE subject='์ํ'; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; int total = 0; try { conn = getConnection(); String query = "SELECT SUM(price) FROM lecture WHERE subject=?"; ps = conn.prepareStatement(query); ps.setString(1, subject); rs = ps.executeQuery(); if(rs.next()) { total = rs.getInt("SUM(price)"); } } finally { closeAll(rs, ps, conn); } return total/discount; } /////////////////////////// ๊ด๋ฆฌ์ ๊ธฐ๋ฅ (์์ฑ) /////////////////////// }
(2) ์ ์๋ ์ ์ฒด ์กฐํ
-
์์ค ์ฝ๋
@Override public ArrayList<User> findAllTeacher() throws Exception{ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<User> list = new ArrayList<User>(); try { conn = getConnection(); String query ="SELECT teacher_id, name, email,subject FROM teacher"; ps = conn.prepareStatement(query); rs = ps.executeQuery(); while(rs.next()){ list.add(new Teacher(rs.getInt("teacher_id"), rs.getString("name"), rs.getString("email"), rs.getString("subject") )); } }finally { closeAll(rs, ps, conn); } return list; }
-
๊ฒฐ๊ณผ
(3) ๊ณผ๋ชฉ๋ณ ์ ์๋ ์กฐํ
-
์์ค์ฝ๋
@Override public ArrayList<User> findTeacherBySubject(String subject) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<User> list = new ArrayList<User>(); try { conn = getConnection(); if(lectureSubjectExist(subject,conn)) { String query ="SELECT teacher_id, name, email,subject FROM teacher WHERE subject=?"; ps = conn.prepareStatement(query); ps.setString(1, subject); rs = ps.executeQuery(); while(rs.next()){ list.add(new Teacher(rs.getInt("teacher_id"), rs.getString("name"), rs.getString("email"), rs.getString("subject") )); } }else { throw new IdNotFoundException("ํด๋น ๊ณผ๋ชฉ์ ๋ด๋นํ๋ ์ ์๋์ด ์์ต๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; }
-
๊ฒฐ๊ณผ
(4) ์ ์๋๋ณ ๊ฐ์ ์กฐํ
-
์์ค์ฝ๋
@Override public ArrayList<Lecture> findLectureByTeacher(int teacherId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<Lecture>(); try { conn = getConnection(); if(teacherIdExist(teacherId,conn)) { String query ="SELECT lecture_id,name,subject,likes,to_char(price,'9,999,999') FROM lecture WHERE lecture_id in (SELECT lecture_id FROM education where teacher_id=?)"; ps = conn.prepareStatement(query); ps.setInt(1, teacherId); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture(rs.getInt("lecture_id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("to_char(price,'9,999,999')") )); } }else { throw new IdNotFoundException("๋ฑ๋ก๋ ์ ์๋์ด ์๋๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; }
-
๊ฒฐ๊ณผ
(5) ๊ฐ์ id๋ก ๊ฐ์ ์กฐํ
-
์์ค์ฝ๋
@Override public Lecture findLectureById(int lectureId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; Lecture lecture = new Lecture(); try { conn = getConnection(); if(lectureIdExist(lectureId,conn)) { String query ="SELECT lecture_id, name,subject,likes,to_char(price,'9,999,999') FROM lecture WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, lectureId); rs = ps.executeQuery(); if(rs.next()){ lecture.setId(lectureId); lecture.setName(rs.getString("name")); lecture.setSubject(rs.getString("subject")); lecture.setLikes( rs.getInt("likes")); lecture.setPrice(rs.getString("to_char(price,'9,999,999')")); } }else { throw new IdNotFoundException("์กด์ฌํ์ง ์๋ ๊ฐ์์ ๋๋ค."); } }finally { closeAll(rs, ps, conn); } return lecture; }
-
๊ฒฐ๊ณผ
(6) ์ธ๊ธฐ ๊ฐ์ ๋ชฉ๋ก ์กฐํ
-
์์ค์ฝ๋
@Override public ArrayList<Lecture> findBestLectures() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<Lecture>(); try { conn = getConnection(); String query ="SELECT lecture_id, name, subject, likes, to_char(price,'9,999,999')" + "FROM (SELECT lecture_id, name, subject, likes, price, " + "ROW_NUMBER() OVER(ORDER BY likes DESC) AS ranking FROM lecture l) " + "WHERE ranking <= 3"; ps = conn.prepareStatement(query); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture(rs.getInt("lecture_id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("to_char(price,'9,999,999')") )); } }finally { closeAll(rs, ps, conn); } return list; }
-
๊ฒฐ๊ณผ
(7) ๊ฐ์ ์ ๋ณด ์ถ๊ฐ
-
์์ค์ฝ๋
@Override public void addLecture(Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "INSERT INTO lecture(lecture_id, name, subject, likes, price) VALUES (seq_lecture.nextVal, ?, ?, ?, ?)"; ps = conn.prepareStatement(query); ps.setString(1, lecture.getName()); ps.setString(2, lecture.getSubject()); ps.setInt(3, lecture.getLikes()); ps.setString(4, lecture.getPrice()); System.out.println(ps.executeUpdate()+ "๊ฐ ๊ฐ์ ๋ฑ๋ก๋์์ต๋๋ค."); }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(8) ๊ฐ์ ์ ๋ณด ์์
-
์์ค์ฝ๋
@Override public void updateLecture(Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(lectureIdExist(lecture.getId(), conn)) { String query = "UPDATE lecture SET name = ?, subject = ?, price = ? WHERE lecture_id = ?"; ps = conn.prepareStatement(query); ps.setString(1, lecture.getName()); ps.setString(2, lecture.getSubject()); ps.setString(3, lecture.getPrice()); ps.setInt(4, lecture.getId()); System.out.println(ps.executeUpdate()+ "๊ฐ ๊ฐ์๊ฐ ์์ ๋์์ต๋๋ค."); }else { throw new IdNotFoundException("์กด์ฌํ๋ lecture id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(9) ๊ฐ์ ์๊ฐ ์ด์ก๊ณผ ๊ฐ์๋ชฉ๋ก ์กฐํ
-
์์ค์ฝ๋
@Override public ArrayList<Student> getAllStudents() throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Student> list = new ArrayList<Student>(); try { conn = getConnection(); String query = "SELECT student_id, name, address, phone, email" + " FROM student"; ps = conn.prepareStatement(query); rs = ps.executeQuery(); while(rs.next()) { list.add(new Student(rs.getInt("student_id"), rs.getString("name"), rs.getString("address"), rs.getString("phone"), rs.getString("email") )); } }finally { closeAll(rs, ps, conn); } return list; } @Override public String getStudentLectures(int studentId) throws Exception{ Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; String sumPrice = ""; try { conn = getConnection(); if(studentIdExist(studentId, conn)){ if(learningIdExist(studentId, conn)) { conn = getConnection(); String query = "SELECT TO_CHAR(SUM(price), 'FM999,999,999,999') AS price, id" + " FROM" + " (SELECT lecture.name AS name, lecture.subject AS subject, lecture.likes AS likes, lecture.price AS price, learning.student_id AS id" + " FROM learning, lecture" + " WHERE learning.lecture_id = lecture.lecture_id AND learning.student_id = ?)" + " GROUP BY id"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); rs = ps.executeQuery(); if(rs.next()) { sumPrice = rs.getString("price"); } } }else{ throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(rs, ps, conn); } if(sumPrice.equals("")) { sumPrice = "0"; } return sumPrice; } @Override public ArrayList<Lecture> getAllMyLecture(int studentId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; ArrayList<Lecture> list = new ArrayList<>(); try { conn = getConnection(); if(studentIdExist(studentId, conn)){ if(learningIdExist(studentId, conn)) { conn = getConnection(); String query = "SELECT lecture.lecture_id AS id, lecture.name AS name, lecture.subject AS subject, lecture.likes AS likes, lecture.price AS price" + " FROM learning, lecture" + " WHERE learning.lecture_id = lecture.lecture_id AND learning.student_id = ?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); rs = ps.executeQuery(); while(rs.next()) { list.add(new Lecture( rs.getInt("id"), rs.getString("name"), rs.getString("subject"), rs.getInt("likes"), rs.getString("price"))); } } }else{ throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(rs, ps, conn); } return list; }
-
๊ฒฐ๊ณผ
(10) ์๊ฐ ์ ์ฒญ
-
์์ค์ฝ๋
@Override public void applyLecture(int studentId, Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; System.out.println("ํ์์์ด๋ "+studentId); try { conn = getConnection(); if(studentIdExist(studentId, conn)) { if(!learningIdExist(lecture.getId(), studentId, conn)) { conn = getConnection(); String query = "INSERT INTO learning(student_id, lecture_id, flag) VALUES(?, ?, ?)"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lecture.getId()); ps.setInt(3, 0); System.out.println(ps.executeUpdate()+"๊ฐ ๊ฐ์๊ฐ ์ง์๋์์ต๋๋ค."); }else { System.out.println("์ด๋ฏธ ์๊ฐ์ ์ฒญํ ์ด๋ ฅ์ด ์กด์ฌํฉ๋๋ค."); } }else { throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(11) ์๊ฐ ์ฒ ํ
-
์์ค์ฝ๋
@Override public void cancleLecture(int studentId, Lecture lecture) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(learningIdExist(lecture.getId(),studentId, conn)) { conn = getConnection(); String query = "DELETE learning WHERE student_id = ? AND lecture_id = ?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lecture.getId()); System.out.println(ps.executeUpdate()+"๊ฐ ๊ฐ์๊ฐ ์ทจ์๋์์ต๋๋ค."); getAllMyLecture(studentId); }else { throw new IdNotFoundException("์กด์ฌํ๋ student id๊ฐ ์์ต๋๋ค."); } }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(12) ๊ฐ์์ ์ถ๊ฐ
-
์์ค์ฝ๋
@Override public void addTeacher(Teacher teacher) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(!teacherIdExist(teacher.getId(), conn)) { //๋ฑ๋กํ๋ ค๋ ๊ฐ์์๊ฐ ์์ง ์์ผ๋ฉด String query = "INSERT INTO teacher(teacher_id, name, email, subject) VALUES(seq_teacher.nextVal,?,?,?)"; ps= conn.prepareStatement(query); ps.setString(1, teacher.getName()); ps.setString(2, teacher.getEmail()); ps.setString(3, teacher.getSubject()); System.out.println(ps.executeUpdate()+" ๋ช INSERT ์ฑ๊ณต...addTeacher().."); }else { throw new AlreadyExistException(); } }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(13) ๊ฐ์์ ์ ๋ณด ์์
-
์์ค์ฝ๋
@Override public void updateTeacher(Teacher teacher) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE teacher SET name=?, email=?, subject=? WHERE teacher_id=?"; ps = conn.prepareStatement(query); ps.setString(1, teacher.getName()); ps.setString(2, teacher.getEmail()); ps.setString(3, teacher.getSubject()); ps.setInt(4, teacher.getId()); int row = ps.executeUpdate(); if(row==1)System.out.println(row+" ๋ช UPDATE OK...updateTeacher()..."); else throw new IdNotFoundException("์์ ํ ๊ฐ์์๊ฐ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(14) ๊ฐ์์ ์ ๋ณด ์ญ์
-
์์ค์ฝ๋
@Override public void deleteTeacher(int teacherId) throws Exception { Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); if(teacherIdExist(teacherId, conn)) { //ํด๋น ๊ฐ์์ ์กด์ฌํ๋ฉด ์ญ์ String query = "DELETE teacher WHERE teacher_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, teacherId); System.out.println(ps.executeUpdate()+" ๋ช DELETE OK...deleteTeacher().."); }else { throw new IdNotFoundException("์ญ์ ํ ๊ฐ์์๊ฐ ์์ต๋๋ค "); } }finally { closeAll(ps, conn); } }
-
๊ฒฐ๊ณผ
(15) ์ถ์ฒ์ ์ฆ๊ฐ
-
์์ค์ฝ๋
@Override public boolean likePossible(int studentId, int lectureId) throws Exception { //๊ฐ์-ํ์ -> learning //๋ง์ฝ learning์ flag ๊ฐ์ด 0์ด๋ฉด -> ์ข์์ ๋๋ฅด๊ธฐ ๊ฐ๋ฅ, true ๋ฆฌํด //๋ง์ฝ learning์ flag ๊ฐ์ด 1์ด๋ฉด -> ์ข์์ ๋๋ฅด๊ธฐ ๋ถ๊ฐ๋ฅ, false ๋ฆฌํด boolean flag = false; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(); String query = "SELECT flag FROM learning WHERE student_id=? and lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, studentId); ps.setInt(2, lectureId); rs = ps.executeQuery(); if(rs.next()) { if(rs.getInt("flag")==0) flag = true; } } finally { closeAll(rs, ps, conn); } return flag; } // ์๊ฐ์๊ฐ ๊ฐ์ ์ข์์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ // throw IdNotFoundException( ์๊ฐ์ id๋ ๊ฐ์ id ๋๋ค ํ์ธ) private void setFlag(int studentId, int lectureId) throws Exception { //learning ํ ์ด๋ธ์ flag 0->1 Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE learning SET flag=? WHERE student_id=? and lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, 1); ps.setInt(2, studentId); ps.setInt(3, lectureId); int row = ps.executeUpdate(); if(row==1)System.out.println("flag ๋ณ๊ฒฝ ์ฑ๊ณต"); else throw new IdNotFoundException("flag ๋ณ๊ฒฝํ ์ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } } //ํน์ ๊ฐ์์ ์ธ๊ธฐ๋ ๊ฐ์ ธ์ค๊ธฐ public int getLikes(int lectureId) throws Exception { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; int likes = 0; try { conn = getConnection(); if(lectureIdExist(lectureId, conn)) { //๊ฐ์ ์กด์ฌํ๋ฉด ์ธ๊ธฐ๋ ๊ฐ์ ธ์ค๊ธฐ String query = "SELECT likes FROM lecture WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, lectureId); rs = ps.executeQuery(); if(rs.next()) { likes = rs.getInt("likes"); } }else { throw new IdNotFoundException("์ธ๊ธฐ๋๋ฅผ ๊ฐ์ ธ์ฌ ๊ฐ์๊ฐ ์์ต๋๋ค "); } } finally { closeAll(rs, ps, conn); } return likes; } @Override public void likeLecture(int studentId, int lectureId) throws Exception { if(!likePossible(studentId, lectureId)) return; //์ข์์ ๋๋ฅด๊ธฐ ๋ถ๊ฐ๋ฅํ๋ฉด ๋ int nowLikes = getLikes(lectureId); //ํ์ฌ ์ธ๊ธฐ๋+1 -> db์ ์ ๋ฐ์ดํธ Connection conn = null; PreparedStatement ps = null; try { conn = getConnection(); String query = "UPDATE lecture SET likes=? WHERE lecture_id=?"; ps = conn.prepareStatement(query); ps.setInt(1, nowLikes+1); ps.setInt(2, lectureId); int row = ps.executeUpdate(); if(row==1)System.out.println("์ธ๊ธฐ๋ ์ฌ๋ฆฌ๊ธฐ ์ฑ๊ณต"); else throw new IdNotFoundException("์ธ๊ธฐ๋ ์ฌ๋ฆด ๊ฐ์๊ฐ ์์ต๋๋ค"); }finally { closeAll(ps, conn); } //flag 0->1๋ก setFlag(studentId, lectureId); }
-
๊ฒฐ๊ณผ
(16) ํน์ ๊ฐ์ ๊ณผ๋ชฉ ํ ์ธ๊ฐ
-
์์ค์ฝ๋
@Override public int discountLecture(String subject, int discount) throws Exception { //ํน์ subject์ ๊ฐ๊ฒฉ ์ด ํฉ์ ํ ์ธ์จ์ ์ ์ฉํ์ฌ ํจํค์ง ๊ฐ๊ฒฉ์ ์๋ ค์ค๋ค. //SELECT SUM(price) FROM lecture WHERE subject='์ํ'; Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; int total = 0; try { conn = getConnection(); String query = "SELECT SUM(price) FROM lecture WHERE subject=?"; ps = conn.prepareStatement(query); ps.setString(1, subject); rs = ps.executeQuery(); if(rs.next()) { total = rs.getInt("SUM(price)"); } } finally { closeAll(rs, ps, conn); } return total/discount; }
-
๊ฒฐ๊ณผ
-
๊น๊ฐ๋น
-
์ถํ ๋๋ฒจ๋กญ ํ๊ณ ์ถ์ ๋ด์ฉ
: ๋ณ์ ์ธ์ ์๊ฐ ํ๊ธฐ๋ฅผ ๋จ๊ธธ ์ ์๋ ๊ธฐ๋ฅ๊ณผ Q&A ๊ธฐ๋ฅ์ ์ถ๊ฐํด๋ณด๊ณ ์ถ๋ค.
-
ํ๋ก์ ํธ์์ ์ข์๋ ์
: ํต์ฌ ๊ธฐ๋ฅ์ ์ ๋ณํ๊ธฐ ์ํด ์ฌ์ฉ์์ ์ ์ฅ์์ ์์คํ ์ ๋ฐ๋ผ๋ณด๊ณ ๊ธฐ๋ฅ๋ค์ ์ถ์ถํ ์ ์ด ์ข์๋ค.
๋๋ถ์ด ๊ณ ์ฌํ๋ ๊ณผ์ ์์์ ์จ๋ผ์ธ ๊ต์ก ํ๋ซํผ์ ์ฒด๊ณ๋ฅผ ์ดํดํ ์ ์์ด ์ข์๊ณ ,
ํ ์ด๋ธ๋ค์ ์ผ๊ด์ฑ์ ์งํค๊ณ ํด๋์ค๋ ์ฌ ์ฌ์ฉ์ฑ์ ๋์ด๊ธฐ ์ํด ์์ ๊ด๊ณ์ธ ํด๋์ค๋ค์ ํ ํ ์ด๋ธ๋ก ๊ตฌ์ฑํ์ง ์์๋ ๋๋ ๊ฒ์ ์๋กญ๊ฒ ๋ฐฐ์ธ ์ ์์ด ์ข์๋ค.
-
-
๋งน์์ฑ
-
์์ฌ์ ๋ ์
:
-
์ถํ ๋๋ฒจ๋กญ ํ๊ณ ์ถ์ ๋ด์ฉ
:
-
ํ๋ก์ ํธ์์ ์ข์๋ ์
:
-
-
์ต์กํฌ
-
์์ฌ์ ๋ ์
: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ด๊ณ ์ค์ ์์ ์กฐ๊ธ์ด๋ผ๋ ๋ณต์กํด์ง๋ฉด ์ณ๊ฒ ์ค๊ณํ๊ณ ์๋ ๊ฒ์ธ์ง ํท๊ฐ๋ฆฌ๋ ์ํฉ์ด ์์์ต๋๋ค. ์ด ๋ถ๋ถ์์ ๋ ๋ฅ์ํ๊ฒ ์ค๊ณ๋ถ๋ถ์ ์์ฑํ ์ ์์๋ค๋ฉด ๊ธฐ๋ฅ์ ๋ค์ํํ ์ ์์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ค๊ณ์์ ๋ง์ ์์ด๋์ด๋ฅผ ์ฐฝ์ถํ๊ธฐ ์ด๋ ค์ ์ง๋ง ์ข์ ๊ฒฝํ์ด์์ต๋๋ค. ์ ์ค์ผ์ด์ค ๋ค์ด์ด๊ทธ๋จ ๊ธฐ๋ฅ์ ์กํฐ ์ค์ฌ์ผ๋ก ์์ฑํ๋ ๊ฒ์ด ์ฒ์์ ํฐ ์ค๋ฅ์์ง๋ง ์ด๋ฒ ๊ฒฝํ์ ๊ณ๊ธฐ๋ก ์ ํํ๊ฒ ์ค๊ณ ๋ฅ๋ ฅ์ ํฅ์์์ผฐ์ต๋๋ค.
-
์ถํ ๋๋ฒจ๋กญ ํ๊ณ ์ถ์ ๋ด์ฉ
: ๋๊ธ ์์ฑ/์ญ์ /์์ /๋๋๊ธ๊ณผ ์ธ๊ณต์ง๋ฅ์ ํ์ฉํ ํค์๋ ๋ถ์์ ํตํด ํ์๋ค์ ์ฃผ์ ๊ด์ฌ ๊ฐ์ ๋ชฉ๋ก์ ์ ๊ทธ๋ ์ด๋ํ๋ ๋ด์ฉ๋ ๋ฃ๊ณ ์ถ์ต๋๋ค. ๊ฐ์ ์๊ฐ์ ํ์์ ์ธ ๊ฐ์ ์ค๋ช ๋๊ณผ ๊ฐ์ ์๊ฐ ํ๊ธฐ ๋ชฉ๋ก ๋ฆฌ์คํธํ๋ก ์ค์ ์๋น์ค์ ๊ฐ๊น๊ฒ ๊ตฌํํ๊ณ ์ถ์ต๋๋ค.
-
ํ๋ก์ ํธ์์ ์ข์๋ ์
: ์ด๋ฒ ํ๋ก์ ํธ๋ฅผ ํตํ์ฌ VO, DAO ์ธํฐํ์ด์ค, DAO ๊ตฌํ ํด๋์ค์ ํ ์คํธ ํด๋์ค ๊ตฌํ ๋ฅ๋ ฅ์ ํฅ์์์ผฐ์ต๋๋ค. DAO ๊ตฌํ์ฒด ๋ฉ์๋๋ ํด๋น ์๋น์ค์ ๋ํด ์ผ๋์ผ๋ก ํธ์ถ๋๋ค๋ ์ ์ ์ฝ๋ ๊ตฌํ์ ๊ตฌ์ฒด์ ์ผ๋ก ํ์ธํ ์ ์์ด ์๋น์ค์ ๋์์๋ฆฌ์ ๋ํ ํ๋ฆ์ ์ฒด๋ํ์์ต๋๋ค. ์ค๋ผํด์ ํ์ฉํ JDBC์ ๋ณต์กํ ์ฟผ๋ฆฌ๋ฌธ ์์ฑ๋ฅ๋ ฅ์ ๊ตฌ์ฒดํํ์ฌ ๊ต์ก ์๋น์ค ํ๋ซํผ ํ๋ก์ ํธ์ ์ ์ฉํ ์ ์์์ต๋๋ค. ์ด๋ณด๋ค ๋ ๋ณต์กํ ์๋น์ค ๊ตฌํ์๋ ํฐ ์ด๋ ค์ ์์ด ์ ์ํด๋๊ฐ ๊ฒ์ด๋ผ ์๊ฐํฉ๋๋ค.
-
โ ์๋ ์
: DAO ๊ตฌํ์ฒด ๋ฉ์๋๋ ํด๋น ์๋น์ค์ ๋ํด ์ผ๋์ผ๋ก ํธ์ถ๋๋ค๋ ์ ์ ์ฝ๋ ๊ตฌํ์ ๊ตฌ์ฒด์ ์ผ๋ก ํ์ธํ ์ ์์ด ์๋น์ค์ ๋์ ์๋ฆฌ์ ๋ํ ํ๋ฆ์ ์ฒด๋ํ์์ต๋๋ค.
ํต์ฌ ๊ธฐ๋ฅ์ ์ ๋ณํ๊ธฐ ์ํด ์ฌ์ฉ์์ ์ ์ฅ์์ ์์คํ ์ ๋ฐ๋ผ๋ณด๊ณ ๊ธฐ๋ฅ๋ค์ ์ถ์ถํ ์ ์ด ์ข์์ต๋๋ค.
ํ ์ด๋ธ๋ค์ ์ผ๊ด์ฑ์ ์งํค๊ณ ํด๋์ค๋ ์ฌ ์ฌ์ฉ์ฑ์ ๋์ด๊ธฐ ์ํด ์์ ๊ด๊ณ์ธ ํด๋์ค๋ค์ ํ ํ ์ด๋ธ๋ก ๊ตฌ์ฑํ์ง ์์๋ ๋์๋ ์ ์ด ์ ํ๋ ๊ฒ ๊ฐ๋ค.
โ ์์ฌ์ด ์
: ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๊ด๊ณ ์ค์ ์์ ์กฐ๊ธ์ด๋ผ๋ ๋ณต์กํด์ง๋ฉด ์ณ๊ฒ ์ค๊ณํ๊ณ ์๋ ๊ฒ์ธ์ง ํท๊ฐ๋ฆฌ๋ ์ํฉ์ด ์์๋๋ฐ ๋ ๋ฅ์ํ๊ฒ ์ค๊ณํ ์ ์์๋ค๋ฉด ๋ค์ํํ ์ ์์์ ๊ฒ ๊ฐ์ต๋๋ค.
์ ์ค์ผ์ด์ค ๋ค์ด์ด๊ทธ๋จ ๊ธฐ๋ฅ์ ์กํฐ ์ค์ฌ์ผ๋ก ์์ฑํ๋ ๊ฒ์ด ์ฒ์์ ํฐ ์ค๋ฅ์๋๊ฒ์ด ์์ฌ์์ด ๋จ์ต๋๋ค.
๋ ๊ฐ์ธ์ ๋จ์ ํ ์คํธ๋ฅผ ์กฐ๊ธ ๋ ์ธ์ฌํ๊ฒ ์ดํผ๋ฉด์ ์์ฑํ๋ค๋ฉด ์๊ฐ์ ๋จ์ถ ์ํฌ ์ ์๊ณ ํตํฉ ํ ์คํธ๋ฅผ ์ํ ํ๊ฒ ์งํํ ์ ์์์ํ ๋ฐ ๊ทธ๋ฌ์ง ๋ชปํ ์ ์ด ์์ฝ๋ค. ์ถํ์๋ ๊ฐ์ธ ์์ ์งํ์ ์กฐ๊ธ ๋ ์ธ์ฌํ๊ฒ ์ดํด์ผ ํจ์ ๊นจ๋ฌ์๋ค.
1. to_Char์ด์ฉํด์ ์ธ์๋ฆฌ ์ , ๋ฅผ ๋ฃ์ด์ ํํํ ๋ Lecture DAO์ ํ๋ price์ ๋ฐ์ดํฐ ํ์
- to_char๋ก ํํํ ๊ฐ์ String์ผ๋ก ์ฝ์ด์๊ธฐ์ price ํ๋์ ๋ฐ์ดํฐ ํ์
์ String์ผ๋ก ๋ณ๊ฒฝํ๋ค. ๊ทธ๋ฌ๋ ์ค์ ํ
์ด๋ธ์์ price์ ๋ฐ์ดํฐ ํ์
์ NUMBER๋ก ์ง์ ํ๊ธฐ์ vo์์๋ int๋ก ์ค์ ํด์ผ ํ๋ค. ์ด๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ์ง ๊ณ ๋ฏผ ์ค์ด๋ค.
2. Eclipse์ Oracle DB์์ ๋ฐ์ดํฐ ํ์
์ ์ฐจ์ด
- Number vs Integer= int
: ์ดํด๋ฆฝ์ค์ eXERD๋ฅผ ์ด์ฉํ์ฌ DB ๋ชจ๋ธ๋ง์ ์์ฑํ๋ฉด ์ ์์ ๋ฐ์ดํฐ ํ์
์ด integer๋ก ์กํ๋ค. ๊ทธ๋ฌ๋ ์ค์ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ ์ค๋ผํด DB์์๋ integer๋ฅผ ์ ์ฌ์ฉํ์ง ์์ผ๋ฉฐ, ์ฌ์ฉ ์ ์์ฒด์ ์ผ๋ก NUMBER ํ์
์ผ๋ก ๋ณํํ๋ค. ๋ฐ๋ผ์ ํ์ ํ, DB ๋ชจ๋ธ๋ง ์์ฑ ์ ์ ์ํ ๋ฐ์ดํฐ ํ์
์ NUMBER๋ก ํต์ผํ๋ค.