db\usePool
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Connection"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.sql.DataSource"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.naming.Context"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<h2> Connection Pool을 이용한 select</h2>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Context initCtx = new InitialContext(); //javax naming으로 import
//Context는 interface인데 그것을 상속하는 클래스인 InitialContext();를 이용한다.
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/itbank"); //javaxDataSource 를 import
con = ds.getConnection();
stmt = con.createStatement();
String sql = "select * from dbtest";
rs = stmt.executeQuery(sql);
%>
<table border="1">
<tr align="center">
<td> id </td>
<td> passwd </td>
<td> name </td>
<td> logtime </td>
</tr>
<%
while(rs.next()) {
%>
<tr align="center">
<td> <%=rs.getString("id") %> </td>
<td> <%=rs.getString("passwd") %> </td>
<td> <%=rs.getString("name") %> </td>
<td> <%=rs.getTimestamp("logtime") %> </td>
</tr>
<%
}
%>
</table>
<%
}catch(NamingException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(con!=null) con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
%>
'개인 공부방 > JSP' 카테고리의 다른 글
session 생성 (0) | 2011.12.19 |
---|---|
쿠키 만들기 (0) | 2011.12.16 |
Connection Pool 1. 서버설정 (2) | 2011.12.16 |
JDBC (0) | 2011.12.15 |
DB작업 (0) | 2011.12.14 |