Configure the connection pool in Tomcat

Source: Internet
Author: User

After being deployed to Tomcat, the configuration file under the src directory will be automatically copied to the application's classes directory like the class file.

1. Copy the Oracle driver jar package to the <catalina_home>/lib directory;
Create a WEB Project in myeclipse andOracle driver jarAdd it to the buildpath of the project.
Bytes --------------------------------------------------------------------------------------------------------------------

2. Add context under the webroot/META-INF directory of the project. XML file (actually reflected in the /webapps/app/directory). Content:


JDBC/1872 " auth = "Container" type = "javax. SQL. datasource "
maxactive =" 100 "maxidle =" 30 "maxwait =" 10000 "
username =" equipmanage "Password =" test "
driverclassname =" oracle. JDBC. driver. oracledriver "
url =" JDBC: oracle: thin: @ 192.168.187.9: 1521: hngolf "/>

quit

3. reference database connection pool resources in Web. xml:

xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi: schemalocation = "http://java.sun.com/xml/ns/javaee
">


1872 connection
JDBC/1872
javax. SQL. datasource
container

<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

Note: JDBC/1872 and the value of the Resource Name attribute in context. XML in step 2 are consistent.
Bytes --------------------------------------------------------------------------------------------------------------------

4. Compile CN. tsp2c. Stu. jdbcutil. Java
Package CN. tsp2c. Stu;

Import java. SQL. connection;
Import java. SQL. sqlexception;

Import javax. Naming. context;
Import javax. Naming. initialcontext;
Import javax. Naming. namingexception;
Import javax. SQL. datasource;

Public final class jdbcutil {

Public static connection getconnection (){
Datasource DS = NULL;

Context context = NULL;
Try {
Context = new initialcontext ();
DS = (datasource) Context. Lookup ("Java: COMP/ENV/jdbc/1872 ");
Return Ds. getconnection ();
} Catch (namingexception e ){
E. printstacktrace ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return NULL;
}
}

Note: note thatJDBC/1872.It is consistent with the value of the Resource Name attribute in context. XML in step 2.
Bytes --------------------------------------------------------------------------------------------------------------------

5. Compile index. jsp
<% @ Page Language = "Java" Import = "Java. util. *, CN. tsp2c. Stu. *" pageencoding = "GBK" %>
<% @ Page import = "Java. SQL. Connection" %>
<% @ Page import = "Java. SQL. preparedstatement" %>
<% @ Page import = "Java. SQL. resultset" %>
<% @ Page import = "Java. SQL. sqlexception" %>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>

<Title> my JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<! --
<LINK rel = "stylesheet" type = "text/CSS" href = "styles.css">
-->
</Head>

<Body>
<Form method = "Post" name = "stuform" Action = "">
<%!
Connection conn = NULL;
Preparedstatement psmt = NULL;
Resultset rs = NULL;
%>
<%
Try {
Conn = jdbcutil. getconnection ();
If (conn = NULL ){
//
}
String sqlstr = "select * From tb_student ";
Psmt = conn. preparestatement (sqlstr );
Rs = psmt.exe cutequery ();
%>
<Table border = "1">
<Tr> <TH>
Student ID </Th> <TH> name </Th> <TH> age </Th> <TH>
Do not </Th> <TH> phone number </Th> <TH> address </Th> </tr>
<%
While (Rs. Next ()){
%>
<Tr>
<TD> <% = Rs. getstring ("Sid") %> </TD>
<TD> <% = Rs. getstring ("sname") %> </TD>
<TD> <% = Rs. getint ("Age") %> </TD>
<TD> <% = Rs. getstring ("gender") %> </TD>
<TD> <% = Rs. getstring ("phone") %> </TD>
<TD> <% = Rs. getstring ("Address") %> </TD>
</Tr>
<%
}
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {
Rs. Close ();
Psmt. Close ();
Conn. Close ();
} Catch (sqlexception e ){
E. printstacktrace ();
}
}
%>
</Table>
</Form>
</Body>
</Html>

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.