16장. JDBC: 데이터베이스-JSP 연동
1. 개요
2. JDBC 드라이버 로딩 및 DBMS 접속
3. 데이터베이스 쿼리 실행
4. 쿼리문 실행 결과값 가져오기
4. 쿼리문 실행 결과값 가져오기
1. SELECT
SELECT문의 실행 결과는 ResultSet형으로 반환된다.
ResultSet: SELECT문을 실행해서 얻어온 레코드 값을 테이블 형태로 가진 객체
Statement를 사용하는 경우
ResultSet executeQuery(String sql) throws SQLException
PreparedStatement를 사용하는 경우
ResultSet executeQuery() throws SQLException
ResultSet 객체의 메서드 종류
메서드 | 반환 유형 | 설명 |
getXxx(int Columnindex) | xxx | 설정한 columnIndex(필드 순번)의 값을 설정한 xxx형으로 가져옴 |
getXxx(String ColumnName) | xxx | 설정한 ColumnName(필드 순번)의 필드 값을 설정한 xxx형으로 가져옴 |
absolute(int row) | boolean | 설정한 row 행으로 커서 이동 |
beforeFirst() | void | 첫 번째 행의 이전으로 커서 이동 |
afterLast() | void | 마지막 행의 다음으로 커서 이동 |
first() | void | 첫 번째 행으로 커서 이동 |
last() | void | 마지막 행으로 커서 이동 |
next() | boolean | 다음 행으로 커서 이동 |
previous() | boolean | 이전 행으로 커서 이동 |
close() | void | Result 객체를 반환할 때 사용 |
예제: Statement 객체로 SELECT 쿼리문 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file = "dbconn.jsp" %>
<table width="300" border=1>
<tr>
<th>아이디
<th>비밀번호
<th>이름
<%
Statement stmt = null;
ResultSet rs = null;
try{
String sql = "select * from member";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
String id = rs.getString("id");
String pw = rs.getString("passwd");
String name = rs.getString("name");
%>
<tr>
<td><%=id%>
<td><%=pw%>
<td><%=name%>
<%
}
}catch(SQLException ex){
out.println("Member 테이블 호출 실패<br>");
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
%>
</table>
</body>
</html>
예제2 PreparedStatement 객체로 SELECT 쿼리문 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file = "dbconn.jsp" %>
<table width="300" border=1>
<tr>
<th>아이디
<th>비밀번호
<th>이름
<%
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
String sql = "select * from member";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()){
String id = rs.getString("id");
String pw = rs.getString("passwd");
String name = rs.getString("name");
%>
<tr>
<td><%=id%>
<td><%=pw%>
<td><%=name%>
<%
}
}catch(SQLException ex){
out.println("Member 테이블 호출 실패<br>");
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(conn != null)
conn.close();
}
%>
</table>
</body>
</html>
Statement 객체로 UPDATE 쿼리문 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file="dbconn.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
Statement stmt = null;
ResultSet rs = null;
try{
String sql = "select id, passwd from member where id = '" + id + "'"; //form에서 입력한 id와 id가 일치하는 행을 DB에서 가져옴
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){ //form에서 입력한 id와 일치하는 id가 데이터베이스에 있을 경우 실행
String rId = rs.getString("id");
String rPasswd = rs.getString("passwd");
if(id.equals(rId)&&passwd.equals(rPasswd)){ //id와 비밀번호가 일치할 경우 입력받은 name으로 name값 수정
sql = "update member set name = '" + name + "'where id = '" + id + "'";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
out.println("Member 테이블을 수정했습니다.");
}else
out.println("일치하는 비밀번호가 아닙니다.");
}else
out.println("Member 테이블에 일치하는 아이디가 없습니다.");
}catch(SQLException ex){
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
%>
</body>
</html>
PreparedStatement 객체로 UPDATE 쿼리문 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file="dbconn.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
String sql = "select id, passwd from member where id = ?"; //form에서 입력한 id와 id가 일치하는 행을 DB에서 가져옴
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()){ //form에서 입력한 id와 일치하는 id가 데이터베이스에 있을 경우 실행
String rId = rs.getString("id");//리턴받은 row의 id칼럼 값
String rPasswd = rs.getString("passwd");//리턴받은 row의 passwd 칼럼 값
if(id.equals(rId)&&passwd.equals(rPasswd)){ //id와 비밀번호가 일치할 경우 입력받은 name으로 name값 수정
sql = "update member set name = ? where id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, id);
pstmt.executeUpdate();
out.println("Member 테이블을 수정했습니다.");
}else
out.println("일치하는 비밀번호가 아닙니다.");
}else
out.println("Member 테이블에 일치하는 아이디가 없습니다.");
}catch(SQLException ex){
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(conn != null)
conn.close();
}
%>
</body>
</html>
Statement 객체로 DELETE 쿼리문 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file="dbconn.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
Statement stmt = null;
ResultSet rs = null;
try{
String sql = "select id, passwd from member where id = '" + id + "'";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if(rs.next()){
String rId = rs.getString("id");
String rPasswd = rs.getString("passwd");
if(id.equals(rId)&&passwd.equals(rPasswd)){
sql = "delete from member where id = '" + id +"' and passwd = '"+passwd+"'";
stmt = conn.createStatement();
stmt.executeUpdate(sql);
out.println("Member 테이블을 삭제했습니다.");
}else
out.println("일치하는 비밀번호가 아닙니다.");
}else
out.println("Member 테이블에 일치하는 아이디가 없습니다.");
}catch(SQLException ex){
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(stmt != null)
stmt.close();
if(conn != null)
conn.close();
}
%>
</body>
</html>
PreparedStatement 객체로 DELETE 실행 결과 가져오기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Database SQL</title>
</head>
<body>
<%@ include file="dbconn.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
String sql = "select id, passwd from member where id = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
rs = pstmt.executeQuery();
if(rs.next()){
String rId = rs.getString("id");
String rPasswd = rs.getString("passwd");
if(id.equals(rId)&&passwd.equals(rPasswd)){
sql = "delete from member where id = ? and passwd = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, passwd);
pstmt.executeUpdate();
out.println("Member 테이블을 삭제했습니다.");
}else
out.println("일치하는 비밀번호가 아닙니다.");
}else
out.println("Member 테이블에 일치하는 아이디가 없습니다.");
}catch(SQLException ex){
out.println("SQLException: "+ex.getMessage());
}finally{
if(rs != null)
rs.close();
if(pstmt != null)
pstmt.close();
if(conn != null)
conn.close();
}
%>
</body>
</html>
'정리노트' 카테고리의 다른 글
[JSP] MVC (0) | 2023.12.26 |
---|---|
[JSP] JSTL (0) | 2023.12.22 |
[JSP] 데이터베이스, JDBC (0) | 2023.12.20 |
[JSP]쿠키, 데이터베이스 (0) | 2023.12.19 |
[JSP] 세션 (0) | 2023.12.18 |