Model1
일반적으로 jsp, servlet의 model1 방법으로는 DAO 파일에서 DB서버로부터 connection을 얻고 select, insert, delete, update 등의 메소드를 구현하였다.
Model2
하지만 model2에서는 원칙적으로 중복이 되는 코드를 최대한 나누어서 호출하는 방식으로 작성을 하자는 것이다. 이 때 DAO에 작성했던 메소드들을 이제는 service로 구현하게 된다. DAO에서 작성했던 메소드들을 interface 안 추상메소드로 설정한 다음 이를 구현한 클래스를 작성하는 방식이다.
DAO.java
package dao;
import dto.Dto;
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 java.util.List;
public class Dao {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
static {
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "hr";
String password = "hr";
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
// 전체조회 - Read
public List<Dto> getList() {
con = getConnection();
String sql =
"SELECT no, title, created_at, completed FROM todotbl order by no";
List<Dto> list = new ArrayList<>();
try {
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
Dto dto = new Dto();
dto.setNo((rs.getInt(1)));
dto.setTitle(rs.getString(2));
dto.setCreated_at(rs.getDate(3));
dto.setCompleted(rs.getBoolean(4));
list.add(dto);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pstmt, rs);
}
return list;
}
public Dto getRow(String no) {
con = getConnection();
String sql = "SELECT * FROM todotbl where no = ?";
Dto dto = null;
try {
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(no));
rs = pstmt.executeQuery();
if (rs.next()) {
dto = new Dto();
dto.setNo((rs.getInt(1)));
dto.setTitle(rs.getString(2));
dto.setCreated_at(rs.getDate(3));
dto.setCompleted(rs.getBoolean(4));
dto.setDescription(rs.getString(5));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pstmt, rs);
}
return dto;
}
public int update(Dto updatedto) {
con = getConnection();
String sql =
"UPDATE TODOTBL SET COMPLETED = ?, DESCRIPTION =? WHERE NO = ?";
int result = 0;
try {
pstmt = con.prepareStatement(sql);
pstmt.setBoolean(1, updatedto.isCompleted());
pstmt.setString(2, updatedto.getDescription());
pstmt.setInt(3, updatedto.getNo());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pstmt);
}
return result;
}
public int delete(String no) {
con = getConnection();
String sql = "DELETE FROM TODOTBL WHERE NO = ?";
int result = 0;
try {
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, Integer.parseInt(no));
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pstmt);
}
return result;
}
public void close(Connection con, PreparedStatement pstmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void close(Connection con, PreparedStatement pstmt) {
try {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int insert(Dto insertdto) {
con = getConnection();
String sql =
"insert into todotbl(NO,TITLE,DESCRIPTION) values(todo_seq.nextval,?,?)";
int result = 0;
try {
pstmt = con.prepareStatement(sql);
pstmt.setString(1, insertdto.getTitle());
pstmt.setString(2, insertdto.getDescription());
result = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(con, pstmt);
}
return result;
}
}
Service Interface
public interface TodoService {
List<Dto> getList();
Dto getRow(String no);
boolean update(Dto updatedto);
boolean delete(String no);
boolean insert(Dto insertdto);
}
Service Class which implements Interface
public class TodoServiceImpl implements TodoService {
Dao dao = new Dao();
@Override
public List<Dto> getList() {
return dao.getList();
}
@Override
public Dto getRow(String no) {
return dao.getRow(no);
}
@Override
public boolean update(Dto updatedto) {
return dao.update(updatedto) == 1;
}
@Override
public boolean delete(String no) {
return dao.delete(no) == 1;
}
@Override
public boolean insert(Dto insertdto) {
return dao.insert(insertdto) == 1;
}
}
결과적으로 보통으로는 DAO의 객체를 만들어 dao.getList(), dao.getRow()와 같은 방식으로 작성을 하였으나 이제는 model2의 방법으로 service 객체로 service.getList(), service.getRow()와 같은 방식으로 작성을 하게 된다.
'Backend > JSP Servlet' 카테고리의 다른 글
File upload with input tag (0) | 2024.03.26 |
---|---|
Connection객체와 getConnection()메소드 (0) | 2024.03.21 |
Session (0) | 2024.03.15 |
JSP 내장객체 HttpSession (0) | 2024.03.15 |
JSP 기본코드 (0) | 2024.03.14 |