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.