|
|
|
|
민준이 |
|
|
|
|
|
|
|
|
|
«
2010/03
»
| 일 |
월 |
화 |
수 |
목 |
금 |
토 |
| |
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 |
|
|
|
|
| 월별보기 |
|
|
|
|
|
|
| | |
|
datasource 설정 및 예제(Tomcat)
|
| Category : 공부/JAVA |
Date : 2009/11/04 20:28 |
|
|
|
1. 첨부한 zip 파일 압축을 푼 후 jar파일을 라이브러리에 추가
================================
2. web.xml
================================
<resource-ref> <description>Oraclejava DB Connection</description> <!-- 다음이 바로 리소스의 이름 --> <res-ref-name>jdbc/oraclejava</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
=================================
3. Context XML파일의 설정예(Tomcat의 경우 Topmcat_Home/conf/catalina/localhost 아래에 컨텍스트이름.xml)
=================================
Context명이 web이라고 할때
<Context path="/web" reloadable="true" docBase="C:\java2\project\web" workDir="C:\java2\project\web\work">
<Resource name="jdbc/oraclejava" auth="Container" type="javax.sql.DataSource" username="scott" password="tiger" driverClassName="oracle.jdbc.driver.OracleDriver" url="jdbc:oracle:thin:@192.168.1.131:1521:ORCL"/>
</Context>
==================
4. datasource.jsp
==================
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <%@ page import="javax.naming.*" %> <%@ page import="java.sql.*" %> <%@ page import="javax.sql.*" %> <%! public String nvl(String param) { return param != null? param:""; } %> <html> <head> <title></title> <meta http-equiv="content-type" content="text/html; charset=euc-kr"> </head> <body> <% // 데이터베이스 접속 InitialContext ctx = null; DataSource ds = null; Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql="SELECT * FROM TAB "; try { ctx = new InitialContext(); ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oraclejava"); con = (Connection) ds.getConnection(); pstmt = con.prepareStatement(sql); rs = pstmt.executeQuery(); out.println("<table>"); while(rs.next()) { out.println("<tr>\n"); for (int i = 1; i < 3; i++) out.println("<td>" + nvl(rs.getString(i)) + "</td>"); out.println("</tr>\n"); } out.println("</table>"); } catch (NamingException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) try { rs.close(); } catch (Exception e) { // quit; } if (pstmt != null) try { pstmt.close(); } catch (Exception e) { // quit } if (con != null) try { con.close(); } catch (Exception e) { // quit } } %> </body> </html>
|
민준이
2009/11/04 20:28
2009/11/04 20:28
|
|
|
|
|
|
 |
http://webpro2004.com/trackback/121 |
|
|
|
|
|
|
|
| | |
|
|
|
|
|
최근 올라온 글 |
 |
|
|
|
|
|
|
|
|
|
|
블로그 최근 덧글 |
 |
|
|
|
|
|
|
|
|
|
|
최근 엮인글 |
 |
|
|
|
|
|
|
|
|
|
|
즐겨찾기
|
|
|
|
|
|
|
|
|
|
|
| Today :
|
14 |
|
| Yesterday :
|
43 |
|
| Total :
|
13717 |
|
|
|
|
|
|
|
| Powered by
|
Textcube 1.7.1 : Risoluto |
|
| Skin by
|
mulder21c |
|
|
|
|
|