'데이터베이스'에 해당되는 글 1

  1. 2011/10/19 데이터 베이스 프로그래밍 기초

데이터 베이스 프로그래밍 기초

크리에이티브 커먼즈 라이선스
Creative Commons License
04. JSP 에서 JDBC 프로그래밍 하기

4.1 JDBC의 구조

4.2 JDBC 드라이버 준비하기

4.3 JDBC 프로그래밍의 코딩 스타일
<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page pageEncoding = "euc-kr" %>
<%@ page trimDirectiveWhitespaces = "true" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<html>
<head>
	<title>회원 목록</title>
</head>
<body>
MEMBER 테이블의 내용
<table width="100%" border="1">
<tr>
	<td>이름</td>
	<td>아이디</td>
	<td>이메일</td>
</tr>
<%
	// 1. JDBC 드라이버 로딩
	Class.forName("com.mysql.jdbc.Driver");

	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	try {
		String jdbcDriver = "jdbc:mysql://localhost:3306/chap12?" +
							"useUnicode=true&characterEncoding=euckr";
		String dbUser = "jernin20";
		String dbPass = "qowhdrnr";

		String query = "select * from MEMBER order by MEMBERID";

		// 2. 데이터베이스 커넥션 생성
		conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);

		// 3. Statement 생성
		stmt = conn.createStatement();

		// 4. 쿼리 실행
		rs = stmt.executeQuery(query);

		// 5. 쿼리 실행 결과 출력
		while(rs.next()){
%>
<tr>
	<td><%= rs.getString("NAME") %></td>
	<td><%= rs.getString("MEMBERID") %></td>
	<td><%= rs.getString("EMAIL") %></td>
</tr>
<%
		}
	} catch(SQLException ex){
		out.println(ex.getMessage());
		ex.printStackTrace();
	} finally {
		// 6. 사용한 Statement 종료
		if(rs != null) try{ rs.close(); } catch(SQLException ex){}
		if(stmt != null) try{ stmt.close(); } catch(SQLException ex){}
		if(conn != null) try{ conn.close(); } catch(SQLException ex){}
	}
%>
</table>
</body>
</html>

</html>

실행결과


4.4 DBMS와의 통신을 위한 JDBC 드라이버

주요 데이터베이스에 대한 DJBC 드라이버에 해당하는 클래스

MySQL : com.mysql.jdbc.Driver
오라클 : oracle.jdbc.driver.OracleDriver
MS SQL 서버 : com.microsoft.sqlserver.jdbc.SQLServerDriver 

4.5 데이터베이스 식별을 위한 JDBC URL

jdbc:[DBMS]:[데이터베이스식별자]

jdbc:mysql://HOST[:PORT]/DBNAME[?param=value&param1=value2&...]

MySQL에서 한글 데이터를 올바르게 하기 위해서는 다음과 같이 추가 파라미터를 이용해서 캐릭터 셋을 알맞게 지정해 주어야 한다.

jdbc:mysql://localhost:3306/chap12?useUnicode=true&characterEncoding=euckr

오라클에서  제공하는 JDBC 드라이버의 경우

jdbc:oracle:thin:@HOST:PORT:SID
jdbc:oracle:thin:@172.0.0.1:1521:ORCL

4.6 데이터베이스 커넥션

4.7 Statement를 사용한 쿼리 실행

/update/updateForm.jsp
<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page pageEncoding = "euc-kr" %>
<%@ page trimDirectiveWhitespaces = "true" %>
<html>
<head>
	<title>이름 변경</title>
</head>
<body>
<form action="/chap12/update/update.jsp" method="post">
<table border="1">
<tr>
	<td>아이디</td>
	<td><input type="text" name="memberID" size="10"></td>
	<td>이름</td>
	<td><input type="text" name="name" size="10"></td>
</tr>
<tr>
	<td colspan="4"><input type="submit" value="변경"></td>
</tr>
</table>
</form>

</body>
</html>

실행결과 



/update/update.jsp
<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page pageEncoding = "euc-kr" %>
<%@ page trimDirectiveWhitespaces = "true" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.SQLException" %>

<%
	request.setCharacterEncoding("euc-kr");

	String memberID = request.getParameter("memberID");
	String name = request.getParameter("name");

	int updateCount = 0;

	Class.forName("com.mysql.jdbc.Driver");

	Connection conn = null;
	Statement stmt = null;

	try {
		String jdbcDriver = "jdbc:mysql://localhost:3306/chap12?" +
							"useUnicode=true&characterEncodiong=euckr";
		String dbUser = "jernin20";
		String dbPass = "qowhdrnr";

		String query = "update MEMBER set NAME = '"+ name +"' "+
						"where MEMBERID = '"+memberID+"'";

		conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
		stmt = conn.createStatement();
		updateCount = stmt.executeUpdate(query);
	} finally {
		if(stmt != null) try{ stmt.close(); } catch(SQLException ex){}
		if(conn != null) try{ conn.close(); } catch(SQLException ex){}
	}
%>
<html>
<head>
	<title>이름 변경</title>
</head>
<body>
<% if(updateCount > 0) { %>
<%= memberID %>의 이름을 <%= name %>(으)로 변경
<% } else { %>
<%= memberID %> 아이디가 존재하지 않음
<% } %>
</body>
</html>


실행결과 


 
4.8 ResultSet에서 값 읽어오기

viewMember.jsp
<%@ page contentType = "text/html; charset=euc-kr" %>
<%@ page pageEncoding = "euc-kr" %>
<%@ page trimDirectiveWhitespaces = "true" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>
<%
	String memberID = request.getParameter("memberID");
%>
<html>
<head>
	<title>회원 정보</title>
</head>
<body>
<%
	Class.forName("com.mysql.jdbc.Driver");

	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	try {
		String jdbcDriver = "jdbc:mysql://localhost:3306/chap12?" +
							"useUnicode=true&characterEncoding=euckr";
		String dbUser = "jernin20";
		String dbPass = "qowhdrnr";

		String query = "select * from MEMBER where MEMBERID = '"+ memberID + "'";

		conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
		stmt = conn.createStatement();

		rs = stmt.executeQuery(query);

		if(rs.next()){
%>
<table border="1">
<tr>
	<td>아이디</td>
	<td><%= memberID %></td>
</tr>
<tr>
	<td>암호</td>
	<td><%= rs.getString("PASSWORD") %></td>
</tr>
<tr>
	<td>이름</td>
	<td><%= rs.getString("NAME") %></td>
</tr>
<tr>
	<td>이메일</td>
	<td><%= rs.getString("EMAIL") %></td>
</tr>
</table>
<%
		} else {
%>
<%= memberID %>에 해당하는 정보가 존재하지 않습니다.
<%
		}
	} catch(SQLException ex){
%>
에러 발생 : <%= ex.getMessage() %>
<%
	} finally {
		if(rs != null) try{ rs.close(); } catch(SQLException ex){}
		if(stmt != null) try{ stmt.close(); } catch(SQLException ex){}
		if(conn != null) try{ conn.close(); } catch(SQLException ex){}
	}
%>
</body>
</html>

실행결과 


 
저작자 표시 비영리 변경 금지
트랙백0 댓글0