How to use Jndi (Java naming and directory Interface) is an application interface that provides developers with a common, unified interface to find and access various naming and directory services, similar to JDBC, built on an abstraction layer. Jndi provides a unified way to find and access services on the network. By specifying a resource name that corresponds to one record in the database or naming service, it returns the information necessary to establish the database connection.
In actual development, we also sometimes use the server provided to our database connection pool, for example, we hope that the Tomcat server in the startup can help us create a database connection pool, then we do not need to manually create database connection pool in the application, Use the Tomcat server directly to create a good database connection pool. To help us create a database connection pool when the Tomcat server is restarted, we need to simply configure the Tomcat server.
The configuration process is as follows:
(1) Place the database driver (Jar package) in the Lib folder under the Tomcat installation directory.
(2) Manually create a context.xml file in the project's webcontent\meta-inf\ directory, edit the contents, the final contents are as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<Context>
<resource name= "Jdbc/test" auth= "Container" type= "Javax.sql.DataSource" maxactive= "maxidle=" "maxwait=" 10000 "username=" sa "password=" 525825GAOzhe "
Driverclassname= "Com.microsoft.sqlserver.jdbc.SQLServerDriver"
Url= "Jdbc:sqlserver://localhost:1433;databasename=test"/>
</Context>
(3) in the above webcontent\meta-inf\ directory manually create a web.xml file, edit the contents, the final content is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi: schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id= " webapp_id "version=" 3.1 ">
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
(3) Create a JSP page, the database access test, the code is as follows:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<%@ page import= "java.sql.*, javax.sql.*,javax.naming.*,com.microsoft.*"%>
<! 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> Database Connectivity Test </title>
<body>
<%
Connection Con=null;
try{
Context Ctx=new InitialContext ();
DataSource ds= (DataSource) ctx.lookup ("Java:comp/env/jdbc/test");
Con=ds.getconnection ();
SYSTEM.OUT.PRINTLN ("Connection pool connectioned!!");
Con.close ();
}catch (Namingexception e) {
System.out.println (E.getmessage ());
}catch (SQLException e) {
E.printstacktrace ();
}finally{
Con.close ();
}
%>
</body>
So far, the connection pool for the database has been created.