개발하자

[JSP] jsp에 DB데이터 조회 및 삭제 하기 본문

TECH STACKS/JSP︲HTML ︲CSS ︲SCRIPT

[JSP] jsp에 DB데이터 조회 및 삭제 하기

개발리미 2023. 12. 14. 17:33
728x90

이미 DB와 프로젝트가 연동이 되어 있을 경우, 여기서는 Bean으로 데이터를 저장하여 관리중이다.

 


 

JDBC 실행과정

 

  • DB 드라이버 로드
    1. DB 서버 IP, ID, PW 등을 DriverManager 클래스의 getConnection() 메소드를 사용하여 Connection 객체 생성
    2. Connection에서 PreparedStatement 객체를 받음
    3. executeQuery를 수행하고 ResultSet 객체를 받아 데이터를 처리
    4. resultSet 결과 받기
    5. 사용하였던 ResultSet, PreparedStatement, Connection을 close JDBC 기본 실행과정이다.  
  • executeQuery() 와 executeUpdate() 함수
    - executeQuery()
       1. SELECT 구문을 수행할때 사용하는 함수
       2. ResultSet 객체에 결과값을 담을 수 있음
    - executeQuery()
       1.  INSERT / DELETE / UPDATE/ CREATE / DROP 구문을 수행할때 사용하는 함수
       2. INSERT / DELETE / UPDATE 관련 구문에서는 반영된 레코드의 건수를 반환
       3. CREATE / DROP 관련 구문에서는 -1 을 반환

예제1 (SELECT)

<%
// sqlMapClient 영역
WebApplicationContext context  = WebApplicationContextUtils.getWebApplicationContext( request.getSession().getServletContext() );
SqlMapClient sqlMapClient = (SqlMapClient) context.getBean( "sqlMapClient" );

PreparedStatement pstm = null;
StringBuffer query = new StringBuffer();
ResultSet rs =null;

try{
	sqlMapClient.startTransaction();	// 트랜젝션 시작
	Connection connect = sqlMapClient.getCurrentConnection();
	query = new StringBuffer();	
    
    	// SELECT 구문 
	query.append("SELECT * FROM TABLE WHERE SEQ_ID = 207 and TABLE_NAME = 'SELECT'  ");
	pstm = connect.prepareStatement(query.toString());
	rs = pstm.executeQuery();
    
    	// rs 저장
	resultList = getResultMapRows(rs);

} catch (Exception e) {
	e.printStackTrace();
	out.print(e);
} finally {
	// 닫기
	if (rs != null){ try { rs.close(); } catch(SQLException ee) { ee.printStackTrace(); throw ee; }}
	if (pstm != null){ try { pstm.close(); } catch(SQLException ee) { ee.printStackTrace(); throw ee; }}
	sqlMapClient.endTransaction();
}

%>

<%
	// 표출
	if(resultList != null && resultList.size() > 0){
        String seq = "";
        String table_name = "";
        int num = resultList.size();
        for(int i = 0 ; i < resultList.size() ; i++){			
            Map map = resultList.get(i);
            seq = map.get("SEQ_ID").toString();
            table_name = map.get("TABLE_NAME").toString();
%>
            <%=seq%>
            <%=table_name%>
<%
        }
	}
%>

 

 

예제1 (DELETE)

<%
// sqlMapClient 영역
WebApplicationContext context  = WebApplicationContextUtils.getWebApplicationContext( request.getSession().getServletContext() );
SqlMapClient sqlMapClient = (SqlMapClient) context.getBean( "sqlMapClient" );

PreparedStatement pstm = null;
StringBuffer query = new StringBuffer();
ResultSet rs =null;

try{
	sqlMapClient.startTransaction();	// 트랜젝션 시작
	Connection connect = sqlMapClient.getCurrentConnection();
	query = new StringBuffer();	
    
    	// SELECT 구문 
	query.append("DELETE FROM TABLE WHERE SEQ_ID = 207 and TABLE_NAME = 'SELECT'  ");
	pstm = connect.prepareStatement(query.toString());
	pstm.executeUpdate();
	
    	// 커밋
    sqlMapClient.commitTransaction();
	connect = null;
    
} catch (Exception e) {
	e.printStackTrace();
	out.print(e);
} finally{
	if (rs != null){ try { rs.close(); } catch(SQLException ee) { ee.printStackTrace(); throw ee; }}
	if (stmt != null){ try { stmt.close(); } catch(SQLException ee) { ee.printStackTrace(); throw ee; }}
	if (pstm != null){ try { pstm.close(); } catch(SQLException ee) { ee.printStackTrace(); throw ee; }}
	sqlMapClient.endTransaction(); 
}

%>

 

 

 


공부하면서 유용했던 부분 메모겸 공유하고자 끄적입니다.

고쳐야하는 부분있다면 댓글 남겨주시면 수정하겠습니다.

행복한 하루 보내세요 (❁´◡`❁)

728x90
반응형