티스토리 툴바

rss
BLOG main image
분류 전체보기 (101)
spring (15)
java (13)
database (19)
tool (9)
android (1)
iOS (5)
others (32)
blar blar (7)

91,076 Visitors up to today!
Today 4 hit, Yesterday 84 hit
Statistics Graph
Google
2007/01/18 14:49

인터넷에서 찾았더니.. 5.0용 JNDI Datasource 예제가 있길래 했더니 안된다..

 

그래서 톰캣홈피서 찾은글..


http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

 

시킨대로 하니까 잘된다..

투덜투덜..


eclipse 3.2

tomcat 5.5

    commons-dbcp-1.2.1.jar

    commons-pool-1.3.jar

    commons-collections-3.2.jar

    --> 위 세넘은 http://jakarta.apache.org/site/downloads/downloads_commons.html 여기서 다운

    classes12.jar --> 9i 클라이언트에 있던넘... ㅡ.ㅡ;

oracle 10g

java 1.5.0_08




다음은 내가 사용한 예제.. 까묵거나 지울까봐...


server.xml

<!-- 추가 -->

<Context docBase="jndi" path="/jndi" reloadable="true"
     source="org.eclipse.jst.j2ee.server:jndi">

     <Resource name="orcl/10g" auth="Container"
      type="javax.sql.DataSource" maxActive="100" maxIdle="30"
      maxWait="10000" username="scott" password="tiger"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      url="jdbc:oracle:thin:@192.168.1.254:1521:ORCL" />

</Context>


web.xml

<!-- 추가 -->

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>orcl/10g</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>


test.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="javax.naming.Context"%>
<%@ page import="javax.naming.InitialContext"%>
<%@ page import="javax.naming.NamingException"%>
<%@ page import="javax.sql.DataSource"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.Statement"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>jsp ds 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("orcl/10g");
        conn = ds.getConnection();
        stmt = conn.createStatement();    
        rs = stmt.executeQuery("select count(*) from test");
        if(rs.next()) {                            
            out.println("Value : ");
            out.println(rs.getString(1));
            out.println("<BR>");
        }                                           
    }catch(NamingException ne){
        out.println(ne.toString());
       
    }catch(SQLException se){
        out.println(se.toString());
       
    }finally{
        try { if (rs != null) rs.close(); } catch (Exception e) {}
        try { if (stmt != null) stmt.close(); } catch (Exception e) {}
        try { if (conn != null) conn.close(); } catch (Exception e) {}
    }
%>
</body>
</html>



아놔.. 이게 문제가 좀 있다..

오라클에서만 사용하는것들.. 내가 걸린건..blob이었다.. 이건 위 소스로 안돌아간다..

아래처럼 변경하면 된다.. 아 글고 이번에 알아낸건데.. 리스너네임에 콤마(.)가 들어가면 JDBC가 해석못한다.. 리스너네임에 콤마는 빼~~~

아래소스에 관한 참고 사이트

http://www.microdeveloper.com/html/JNDI_Orcl_Tomcat.html


server.xml

  <!-- Global JNDI resources -->
  <GlobalNamingResources>

    <!-- Test entry for demonstration purposes -->
    <Environment name="simpleValue" type="java.lang.Integer" value="30"/>

    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
       description="User database that can be updated and saved"
           factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
          pathname="conf/tomcat-users.xml" />

 <!-- Every connection to 'lcms.orcl' uses the same user -->
 <Resource name="lcms.orcl"
   auth="Container"
   type="oracle.jdbc.pool.OracleDataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="oracle.jdbc.pool.OracleDataSourceFactory"
   url="jdbc:oracle:thin:@***.***.***.***:1521:ora9"
   user="*****"
   password="*****"
   maxActive="20"
   maxIdle="10"
   maxWait="-1" />


  </GlobalNamingResources>


context.xml

<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
 
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

 <ResourceLink global="lcms.orcl" name="lcms.orcl" type="oracle.jdbc.pool.OracleDataSource"/>


</Context>



test.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="oracle.jdbc.pool.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String message = "Not Connected";
Connection conn = null;
ResultSet rst = null;
Statement stmt = null;
try {
 Context initContext = new InitialContext();
 //Context envContext = (Context) initContext.lookup("java:/comp/env");
 OracleDataSource ds = (OracleDataSource) ((Context) initContext.lookup("java:/comp/env")).lookup("lcms.orcl");

 if (ds == null)
  out.println("Error: No DataSource"+"<br>");
 if (ds != null)
  conn = ds.getConnection();
 if (conn != null) {
  message = "Got Connection " + conn.toString() + ", ";
  stmt = conn.createStatement();
  rst = stmt.executeQuery("select count(*) from ************");
 }
 if (rst.next())
  message = rst.getString(1);

 rst.close();
 rst = null;
 stmt.close();
 stmt = null;
 conn.close(); // Return to connection pool
 conn = null; // Make sure we don't close it twice
 
 out.println(message+"<br>");
 
} catch (Exception e) {
 e.printStackTrace();
} finally {
 // Always make sure result sets and statements are closed,
 // and the connection is returned to the pool
 if (rst != null) {
  try {
   rst.close();
  } catch (SQLException e) {
   ;
  }
  rst = null;
 }

 if (stmt != null) {
  try {
   stmt.close();
  } catch (SQLException e) {
   ;
  }
  stmt = null;
 }

 if (conn != null) {
  try {
   conn.close();
  } catch (SQLException e) {
   ;
  }
  conn = null;
 }
}

%>
</body>
</html>

크리에이티브 커먼즈 라이선스
Creative Commons License

'others' 카테고리의 다른 글

하이버네이트 영속성전이  (0) 2007/06/12
RSS 그림  (0) 2007/05/30
Tomcat 5.5 JNDI Datasource  (0) 2007/01/18
[책] 간만에 3권 구입  (0) 2006/12/28
유니코드 FAQ  (0) 2006/11/29
[책] 재미있는 IT 책들  (0) 2006/07/04


Trackback Address :: http://aircook.tistory.com/trackback/40 관련글 쓰기
Name
Password
Homepage
Secret