0921 서블릿

정혜지·2022년 9월 22일

2022.09.21
// https://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html
// https://www.oracle.com/java/technologies/front-controller.html

"update board set title=? author=? email=? content=? num=? pw=?
// step04
@WebServlet(urlPatterns = {"/var"}, initParams = {@WebInitParam(name = "charset", value = "UTF-8")})

// JSTL 조건문

<h2>JSTL</h2>

<h3>step01 : Condition</h3>
<c:if test="${'a' == 'a'}">
	조건이 참이라면 실행되는 영역
</c:if>

<br><hr><br>

<%-- empty 활용하여 null 검증하기 --%>
<% request.setAttribute("customer", null); %>
<c:if test="${not empty requestScope.customer}">
	null일때에는 실행되지 않는 영역
</c:if>

<br><hr><br>

<%-- 다중 조건 : choose, when --%>
<% session.setAttribute("goal", 4); %>
<c:choose>
	<c:when test="${sessionScope.goal == 2}">
		멀티!
	</c:when>
	<c:when test="${sessionScope.goal == 3}">
		헤트트릭! 
	</c:when>
	<c:otherwise>
		해당 조건이 없을때 실행 영역
	</c:otherwise>
</c:choose>

// model.domain 이름, 나이 출력
<%
ArrayList users = new ArrayList();
users.add(new User("IT", 26));
users.add(new User("DEV", 30));

	session.setAttribute("users", users);
%>

<c:forEach items="${sessionScope.users}" var="data" varStatus="LoopStatus">
	${LoopStatus.count}번 데이터 - 이름 : ${data.name} 나이 : ${data.age} <br>
</c:forEach>
  1. DB 시스템의 동시 접속자 수를 강제적으로 제한 하는 기술

    • Connection Pooling[CP]
  2. 적용 방법

    1. 각 서버별 매뉴얼에 맞게 설정
      http://apache.org
    2. 시스템 사양에 적합한 Connection 수 조절
    3. 자바 소스 상에서의 코드는 표준화 되어 있음
    4. 주의사항
      • Connection은 재사용 개념
      • 사용 직후에는 자원 반환 코드 필수(Connenction 잔존 서버 메모리에 반환)
        : close()
  3. 원리

    1. 정해진 Connection 수에 한해서만 생성 및 유지
    2. 서버 시작시에 이미 몇개를 생성해서 대기해 놓을 수도 있음
  4. 적용 기술

    1. 벤더사가 제시한 매뉴얼에 맞게 설정파일 작성
      1. DB의 종류 - driver 정보
      2. DB의 접속 정보 - url, id, pw
      3. 동시 접속자 - 20
        ...
  5. context.xml
    <Resource
    name="jdbc/mysql" - 설정 정보를 구분하기 위한 고유한 자원의 별칭
    auth="Container" - 이 자원의 관리 권한은 container 즉 서버가 관리 함을 의미
    type="javax.sql.DataSource" - 자바 소스와 서버 설정 정보의 중간 매개체 객체 타입
    driverClassName="com.mysql.cj.jdbc.Driver" - oracle DB driver 지정
    url="jdbc:mysql://localhost:3306/scott" - oracle 접속을 위한 url 설정
    username="" - ID
    password="" - PW
    maxTotal="20" - 동시 접속 max Connection 개수
    maxIdle="10" - 10개의 객체는 늘 대기 개수
    maxWaitMillis="-1" /> - 20개 초과한 대기 시간, -1 따라서 무한 대기의미

// DBUtil
// 1단계 : Driver 로딩
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

// 2단계 : DB 연결 가능 메소드
public static Connection getConnection() throws SQLException {

// return DriverManager.getConnection("jdbc:mysql://localhost:3306/scott?serverTimezone=Asia/Seoul", "scott", "tiger");
return DriverManager.getConnection("jdbc:mysql://mysql-test.cdijggtrxo14.us-east-1.rds.amazonaws.com:3306/scott", "root", "root1234");
}

// 6단계 : 자원반환 메소드
public static void close(ResultSet rset, Statement stmt, Connection con) throws SQLException {
	if(rset != null) {
		rset.close();
	}
	if(stmt != null) {
		stmt.close();
	}
	if(con != null) {
		con.close();
	}
}

public static void close(Statement stmt, Connection con) throws SQLException {
	if(stmt != null) {
		stmt.close();
	}
	if(con != null) {
		con.close();
	}
}
public static String getDnameByDeptno(int deptno) q {
	Connection con = null;
	PreparedStatement pstmt = null;
	ResultSet rset = null;
	
	String sql = "SELECT dname FROM DEPT WHERE deptno = ?";
	String dname = null;
	
	try {
		con = DBUtil.getConnection();
		
		pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, deptno);
		
		rset = pstmt.executeQuery();
		
		if(rset.next()) {
			dname = rset.getString("dname");
		}
		
	} finally {
		DBUtil.close(rset, pstmt, con);
	}
	
	return dname;
}  

package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.DBUtil;

public class DeptDAO {
//deptno로 dname 검색
//Query : "SELECT dname FROM DEPT WHERE deptno = ?"
public static String getDnameByDeptno(int deptno) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rset = null;

	String sql = "SELECT dname FROM DEPT WHERE deptno = ?";
	String dname = null;
	
	try {
		con = DBUtil.getConnection();
		
		pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, deptno);
		
		rset = pstmt.executeQuery();
		
		if(rset.next()) {
			dname = rset.getString("dname");
		}
		
	} finally {
		DBUtil.close(rset, pstmt, con);
	}
	
	return dname;
}  

}

  1. data - > DB 구축

  2. Write.html -> 값을 입력

  3. list.jsp -> 입력된 값을 확인

  4. 게시글 제목 클릭
    -> 해당게시글 상세페이지
    -> 수정하기 위한 목적
    -> 직접 데이터를 수정
    -> 수정하기 버튼 클릭
    -> Update , 삭제

profile
오히려 좋아

0개의 댓글