국비 46일차 Servlet DB 연동 절차
2021. 3. 15. 18:27
2021.03.15 국비교육 46일차
서블릿 - DB 연동(PreparedStatement)
package com.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class EmpListServlet
*/
@WebServlet("/EmpListServlet")
public class EmpListServlet extends HttpServlet {
public String driver = "oracle.jdbc.driver.OracleDriver";
public String url = "jdbc:oracle:thin:@localhost:1521:xe";
public String userid = "scott";
public String passwd = "tiger";
public void init() {
try {
Class.forName(driver); // 드라이버 로딩
System.out.println("드라이버 로딩 성공");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection(url, userid, passwd);
String sql = "select empno, ename, sal, to_char(hiredate, 'yyyy/mm/dd') hiredate, deptno from emp order by empno";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<body>");
String my = "<table border='1'>";
my += "<tr>";
my += "<td>사원번호</td>";
my += "<td>사원이름</td>";
my += "<td>사원월급</td>";
my += "<td>입사일</td>";
my += "<td>부서번호</td>";
my += "</tr>"; // table의 head까지만
while (rs.next()) { // 레코트 한줄한줄 출력
my += "<tr>";
my += "<td>" + rs.getInt("empno") + "</td>";
my += "<td>" + rs.getString("ename") + "</td>";
my += "<td>" + rs.getInt("sal") + "</td>";
my += "<td>" + rs.getString("hiredate") + "</td>";
my += "<td>" + rs.getInt("deptno") + "</td>";
my += "</tr>";
}
my += "<table>";
out.print(my);
out.print("</body>");
out.print("</html>");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
JDBC활용 DB연동 순서 (Service, DTO, DAO 설정방법)
1. WebContent -> WEB-INF -> lib에 oracle, jar 파일 넣기
2. DTO생성
3. Service - dao 연동
4. controller 작성
Mybatis 활용 DB연동 순서
1. WebContent -> WEB-INF -> lib에 oracle, mybatis jar 파일 넣기
2. com.config 패키지 생성 후 기본 세팅하기
2-1 Configuration.xml (연동)
2-2 Mapper.xml (쿼리생성)
2-3 jdbc.properties (기본 4가지 정보 드라이버, 주소, userid, passwd)
2-4 MySqlSessionFactory.java (Sqlsession 만들기)
3. DTO파일 생성
4. Service에서 session생성해서 dao로 넘겨줌 (commit이 필요할때 까먹지 말기)
5. dao에서 session 받아서 session + method로 데이터 받아옴
'ETC > 국비교육' 카테고리의 다른 글
국비 48일차 Servlet(init, context 파라미터 , filter, redirect, forward) (0) | 2021.03.17 |
---|---|
국비 47일차 Servlet scope (0) | 2021.03.16 |
국비 45일차 서블릿 파라미터 처리(values, names, map) (0) | 2021.03.12 |
국비 44일차 Servlet (라이프사이클, thread unsafe, 응답처리, 요청처리, 파라미터 처리) (0) | 2021.03.12 |
국비 42일차 Javascript DOM level 0, DOM level 2 이벤트 처리 (0) | 2021.03.09 |