JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得我们可以用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。
本文介绍通过JNDI作为连接池连接MySQL数据库。Tomcat连接JNDI有3种方式,该案例介绍其中的一种局部配置。
一、目录结构
二、关键代码
- 首先将mysql 的连接jar包(如:mysql-connector-java-5.1.48.jar)拷贝到Tomcat\lib目录中*
2.1 context.xml
文件位于WebContent\META-INF中,如果没有则创建。
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/web14">
<!--配置MySQL数据库的JNDI数据源-->
<Resource
name="mysqlDataSource"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/admin"/>
</Context>
2.2 web.xml
<?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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>gg</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--MySQL数据库JNDI数据源引用 -->
<resource-ref>
<description>MySQL DB Connection</description>
<res-ref-name>mysqlDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
2.3 测试Servlet
@WebServlet("/JDBCDataSourceExample")
public class JDBCDataSourceExample extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Context ctx = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("mysqlDataSource");
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("select admin_id, username from tb_admin");
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.print("<html><body><h2>Employee Details</h2>");
out.print("<table border=\"1\" cellspacing=10 cellpadding=5>");
out.print("<th>Employee ID</th>");
out.print("<th>Employee Name</th>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>" + rs.getInt("admin_id") + "</td>");
out.print("<td>" + rs.getString("username") + "</td>");
out.print("</tr>");
}
out.print("</table></body><br/>");
//lets print some DB information
out.print("<h3>Database Details</h3>");
out.print("Database Product: "+con.getMetaData().getDatabaseProductName()+"<br/>");
out.print("Database Driver: "+con.getMetaData().getDriverName());
out.print("</html>");
}catch(NamingException e){
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
stmt.close();
con.close();
ctx.close();
} catch (SQLException e) {
System.out.println("Exception in closing DB resources");
} catch (NamingException e) {
System.out.println("Exception in closing Context");
}
}
}
}
2.4 效果图
如果想学习Tomcat的其他JDNI配置方式,可以访问tomcat下jndi的三种配置方式。