데이터소스를 이용한 DB 사용 샘플 예제

<%@ page contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
 
 
<title>사용자자가 가지고 있는 모든 테이블 보기</title>
 
<h3>
<%
 Context ctx = null;
 DataSource ds = null;
 Connection con = null;
 Statement stmt = null;
 ResultSet rs = null;
  
 String sql = "select * from tab";
  
 try {
  ctx = new InitialContext();
  ds = (javax.sql.DataSource)ctx.lookup("ora9"); /* JNDI 명 */
  con = ds.getConnection();
  con.setAutoCommit(false);
  stmt = con.createStatement();
  rs = stmt.executeQuery(sql);
  while (rs.next()) {
   out.println(rs.getString(1) + "   " + rs.getString(2));
  }
 } catch (Exception e) {
  out.println(e.toString());
 } finally {
  if (rs != null) {
   try {
    rs.close();
   } catch (Exception e) {}
  }
  if (stmt != null) {
   try {
    stmt.close();
   } catch (Exception e) {}
  }
  if (con != null) {
   try {
    con.close();
   } catch (Exception e) {}
  }
 }
%>

Related Posts

답글 남기기

이메일 주소는 공개되지 않습니다.