본문 바로가기
개발/DB 커넥션

JSP + Oracle 연결 테스트 예제 (JNDI + PreparedStatement)

by 언제나초심. 2010. 11. 2.
반응형

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
//===================================================
//디비연결 변수
Connection  conn  = null;
PreparedStatement   pstmt  = null;
ResultSet   rs    = null;

//쿼리 생성
String query      = " SELECT 'RESULT', ? FROM DUAL ";

try {
    //---------------------------------------------------
    // Oracle Connection Start
    Context initCtx = new InitialContext();
    Context envCtx = (Context)initCtx.lookup("java:/comp/env");
    DataSource ds = (DataSource)envCtx.lookup("jdbc/orclMngr");
    conn = ds.getConnection();

    //---------------------------------------------------
    //쿼리 적용
    pstmt = conn.prepareStatement(query);

    //---------------------------------------------------
    //파라미터 적용
    pstmt.setString(1, "파라미터 테스트");

    //---------------------------------------------------
    // 쿼리 실행
    rs = pstmt.executeQuery();

    // 쿼리 결과 처리
    while(rs.next()) {          
        out.println(rs.getString(2));
    }
}catch(SQLException se){
    out.println("[CONTENT]쿼리 에러 : SQLException ");
}catch(NullPointerException ne){
    out.println("[CONTENT]널 에러 : NullPointerException ");
}catch(Exception e){
    out.println("[CONTENT]에러 : Exception ");
} finally { 
    if (rs != null) try {rs.close(); }catch(SQLException ex) {}
    if (pstmt != null) try {pstmt.close(); } catch(SQLException ex) {}
    if (conn != null) try {conn.close(); }catch(SQLException ex) {}
}
%>


반응형