Tomcat 서버 에서 연결 탱크 를 사용 하여 Oracle 데이터 베 이 스 를 연결 합 니 다.

다음은 Tomcat 서버 에서 연결 탱크 를 사용 하여 데이터 베 이 스 를 연결 하 는 동작 을 소개 합 니 다.웹.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>project</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>DBConnection</description>
<res-ref-name>siniteksirm</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
웹-app 사이 에코드 를 삽입 합 니 다.사용 할 리 소스 이름 을 지정 합 니 다.2.tomcat 의 context.xml 파일 수정:Context 태그 사이 에 다음 코드 를 추가 합 니 다
 
<Resource name="siniteksirm" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@192.168.1.196:1521:orcl"
username="paxt"
password="paxt"
maxActive="20"
maxIdle="10"
maxWait="-1"
testOnBorrow="true"
validationQuery="select 1 from dual"/>
3:Oracle 의 데이터베이스 구동 을 선택 하여 Tomcat 의 lib 패키지 에 가입 합 니 다.이 항목 은 Ojdbc 14.jar.4:jsp 페이지 를 제공 합 니 다
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.sql.*" %>
<%@ page import="javax.sql.DataSource" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
DataSource ds = null;
try{
Context context = new InitialContext();
ds = (DataSource)context.lookup("java:comp/env/siniteksirm");
Connection conn = ds.getConnection();
PreparedStatement pst = conn.prepareStatement("select * from sdc_fundbase where rownum <= 2");
ResultSet rs = pst.executeQuery();
while(rs.next()){
out.println(rs.getString("fund4"));
out.println("<br/>");
}
if(ds != null){
out.println(" ");
}
}catch(Exception e){
e.printStackTrace();
out.println(" ");
}
%>
</body>
</html>
Tomcat 를 시작 하면 페이지 에 접근 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기