본문 바로가기
컴퓨터이야기/JAVA&JSP

초간단 DB연결 테스트

by 변화지시자 2013. 9. 17.
반응형

------ jdbc.jsp ------

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.sql.*"%>

<%
    Connection con = null;
        try{
            String driver ="oracle.jdbc.driver.OracleDriver";
            String jdbcUrl = "jdbc:oracle:thin:@210.90.104.117:1521:EVSCHEDULE";
            String dbId = "dbOra1";
            String dbPass = "dbOra1";

            Class.forName(driver);
            con = DriverManager.getConnection(jdbcUrl, dbId, dbPass);
            out.println("제대로 연결 되었습니다.");

        }catch(Exception e){
            out.println("<h3>연결에 실패하였습니다.</h3>");
            e.printStackTrace();
        }

%>

 

 

------ web.xml ------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>dbTest</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
 
    <resource-ref>
        <description>Connection</description>
        <res-ref-name>jdbc/OracleDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

 

 

------ context.xml ------

<Context>
    <Resource name="jdbc/OracleDB"
    auth="Container"
    type="javax.sql.DataSource"
    username="dbOra1"
    password="dbOra1"
    driverClassName="oracle.jdbc.driver.OracleDriver"
    factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
    url="jdbc:oracle:thin:@210.90.104.117:1521:EVSCHEDULE"
    maxActive="500"
    maxIdle="100"/> 
</Context>

 

 

------ dbcpTest.jsp ------

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*" %>
<%@ page import="javax.naming.*" %>
<%
    Connection conn = null;
   
    try {
        Context init = new InitialContext();
        DataSource ds = (DataSource) init.lookup("java:comp/env/jdbc/OracleDB");
        conn = ds.getConnection();
       
        out.println("<h3>연결되었습니다.</h3>");
    }catch(Exception e){
        out.println("<h3>연결에 실패하였습니다.</h3>");
        e.printStackTrace();
    }
%>

 

 

 

 

 

dbTest.war
다운로드