기술지원 문의
Resin DB connection pool 설정 및 샘플코드
임항순 / 2003-02-20 00:00
Resin DB connection pool 설정
(resin.conf or web.xml에)
caucho mysql Driver사용시
mysql의 mysql Driver사용시
Oracle의 경우
------------------
testDatabase.java
------------------
package test;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;
public class TestDatabase extends HttpServlet {
DataSource pool;
public void init()
throws ServletException
{
try {
Context env = (Context) new InitialContext().lookup(\"java:comp/env\");
pool = (DataSource) env.lookup(\"jdbc/test\");
if (pool == null)
throw new ServletException(\"`jdbc/test is an unknown DataSource\");
} catch (NamingException e) {
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType(\"text/html; charset=euc-kr\");
PrintWriter out = res.getWriter();
Connection conn = null;
try {
conn = pool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(\"select ID, NAME from USER\");
out.println(\"USER:
\");
while (rs.next()) {
out.print(rs.getString(1));
out.print(\" \");
out.print(rs.getInt(2));
out.println(\"
\");
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw new ServletException(e);
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}
}
-----------
test.jsp
-----------
<%@ page import=java.sql.*, javax.sql.*, javax.naming.* %>
<%
Context ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(\"java:comp/env/jdbc/test\");
Connection conn = ds.getConnection();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(\"select ID, NAME from USER\");
%>
USER:
<%while (rs.next()) { %>
<%= rs.getString(1) %> <%= rs.getString(2) %>
<%
}
} finally {
conn.close();
}
%>