728x90
JDBC URL = jdbc:mysql://HOST[:PORT]/DBNAME[?param=value¶m2=value2&...]
<%@ page contentType = "text/html; charset=utf-8" %>
<%@ 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" %>
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/chap14?useUnicode=true&characterEncoding=utf8";
String jdbcDriver = "jdbc:apache:commons:dbcp:chap14";
String dbUser = "jspexam";
String dbPass = "jsppw";
String query = "select * from MEMBER order by MEMBERID";
// 2. 데이터베이스 커넥션 생성
//conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);
conn = DriverManager.getConnection(jdbcDriver);
// 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) {}
// 7. 커넥션 종료
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
%>
</table>
728x90
'백엔드 > JSP & Servlet' 카테고리의 다른 글
JDBC API를 이용해서 두 개 이상의 쿼리를 트랜잭션으로 묶어서 처리 (0) | 2021.07.23 |
---|---|
웹 어플리케이션 구동 시 JDBC 드라이버 로딩하기 (0) | 2021.07.23 |
JSP LONG VARCHAR 타입 값 일어오기 (mysql) (0) | 2021.07.23 |
이클립스에서 JSP로 개발 시 파일 경로 설정 이해 (0) | 2021.07.17 |
이클립스 JSP 컴파일 위치 (서블릿 클래스) (0) | 2021.07.15 |