<Resource name="jdbc/OracleTest"
auth="Container"
type="oracle.jdbc.pool.OracleDataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@localhost:1521:XE"
user="hr"
password="hr"
maxActive="20"
maxIdle="10"
maxWait="-1" />
위 형식으로 다음 두 군데 중 하나에 설정
- context.xml의 <Context>태그사이
- server.xml의 아래쪽 <Context>태그사이
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*, javax.sql.*, javax.naming.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>JNDI Test</title>
</head>
<body>
<%
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/OracleTest");
conn = ds.getConnection();
out.write("<p>DB Connection..</p>");
String sQuery = "SELECT * FROM EMPLOYEES";
stmt = conn.createStatement();
rs = stmt.executeQuery(sQuery);
while (rs.next()) {
out.write(rs.getString(1) + "<br />");
}
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (rs != null) try {rs.close(); } catch (Exception ex2) {}
if (stmt != null) try {stmt.close(); } catch (Exception ex3) {}
if (conn != null) try {conn.close(); } catch (Exception ex4) {}
}
%>
</body>
</html>
아래 블로그에서 참고함
'Java > JDBC' 카테고리의 다른 글
oracle package & cursor 사용 (0) | 2010.10.05 |
---|---|
Common-DbUtils Test (0) | 2010.04.18 |
완존 편리한 org.apache.commons.dbutils (0) | 2010.04.15 |
JSP로 맹글어본 커넥션 (0) | 2010.04.15 |