Using connection pooling to connect Oracle databases under a Tomcat server _oracle

Source: Internet
Author: User
Tags auth connection pooling tomcat server
The following is an operation to connect to a database using a connection pool under a Tomcat server

One: Modify the Web.xml file:
Copy Code code as follows:

<?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>

Insert the <resource-ref> code between the Web-app. Specifies the name of the resource to use.

Two: Modify the Context.xml file under Tomcat:

Add the following code between the context tags.
Copy Code code as follows:

<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"/>

Third: Select Oracle database driver to join Tomcat's LIB package. This project is: Ojdbc14.jar.

Four: Provide a JSP page:
Copy Code code as follows:

<%@ 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<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 ("Database Connection");
}
}catch (Exception e) {
E.printstacktrace ();
OUT.PRINTLN ("Database connection failed");
}
%>
</body>

Start Tomcat so that you can access the page.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.