티스토리 뷰

Language/Java

jndi 테스트용 jsp

후늬뉨 2019. 1. 15. 13:18

 

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%
    Context ctx = null;
    DataSource ds = null;
    Connection conDs = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        //localbinding lookup1
        /*
        Context initContext = new InitialContext();
        ctx  = (Context)initContext.lookup("java:comp/env");
        ds = (DataSource)ctx.lookup("yerin_cloud"); 
        */
        
        
        /*
        //localbinding lookup2
        Context initContext = new InitialContext();
        ds  = (DataSource)initContext.lookup("java:comp/env/test");
        conDs = ds.getConnection();
        */

        //global binding lookup - jndi서버를 통한 resource lookup임.
        ctx = new InitialContext();
        ds = (DataSource) ctx.lookup("myDataSource");
        conDs = ds.getConnection();

        stmt = conDs.createStatement();
        String query = " select 'Connect Success!!!' from dual ";
        out.println("query : "+query+" ");
        rs = stmt.executeQuery( query );

        while (rs.next()) {
            out.print("result : " +rs.getString(1));
        }
    } catch ( Exception e ) {
        e.printStackTrace();
    } finally {
        if ( stmt != null ) try { stmt.close(); } catch(Exception e) {}
        if ( conDs != null ) try { conDs.close(); } catch(Exception e) {}
    }
%>

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함