Notice
반응형
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 에라토스테네스의 체
- set
- 문제풀이
- deque
- js
- BREW
- googleChart
- SQL
- Oracle
- jsp
- TSX
- 백준
- java
- script
- input
- node
- Stack
- Eclipse
- 이클립스
- html
- 응용SW
- react
- 책추천
- 정처산기
- npm
- HashMap
- 수학
- IntelliJ
- 자료구조
- Algorithms
Archives
- Today
- Total
개발하자
[JSP] jsp에 DB데이터 조회 및 삭제 하기 본문
728x90
이미 DB와 프로젝트가 연동이 되어 있을 경우, 여기서는 Bean으로 데이터를 저장하여 관리중이다.
JDBC 실행과정
- DB 드라이버 로드
- DB 서버 IP, ID, PW 등을 DriverManager 클래스의 getConnection() 메소드를 사용하여 Connection 객체 생성
- Connection에서 PreparedStatement 객체를 받음
- executeQuery를 수행하고 ResultSet 객체를 받아 데이터를 처리
- resultSet 결과 받기
- 사용하였던 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
반응형
'TECH STACKS > JSP︲HTML ︲CSS ︲SCRIPT' 카테고리의 다른 글
[JS] 로딩 화면 만들기 (3) | 2023.12.28 |
---|---|
[SCRIPT] 구글차트 파이차트 (반원) (0) | 2023.12.15 |
[SCRIPT] 구글차트 툴팁 깜빡임 해결 (0) | 2023.12.13 |
[SCRIPT] 구글차트 막대그래프 (막대에 데이터 표출) (0) | 2023.12.13 |
[SCRIPT] jsp 프린트 기능 (2) | 2023.12.12 |