JavaEE--JNDI(下,实现)

参考:https://blog.csdn.net/ouyida3/article/details/46699023

   https://www.landui.com/help/show-6158.html

   https://blog.csdn.net/yan372397390/article/details/50450332

 

以tomcat,mysql为例.

 

1配置tomcat的conf/context.xml

<Resource 
  driverClassName="com.mysql.jdbc.Driver" 
  maxActive="4" 
  maxIdle="2"
  maxWait="50" 
  name="jdbc/mysql" 
  password="mysql" 
  type="javax.sql.DataSource" url="jdbc:mysql://192.168.3.138:3306/test" 
  username="root"
/>

 

2将mysql的jar包放入tomcat的lib下.

 

3在项目的web.xml中引入

<resource-ref>
  <res-ref-name>jdbc/mysql</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>

注意res-ref-name要和context.xml中配置的name一致.

 

4获取连接

1 Context ctx = new InitialContext();
2 DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
3 Connection conn = ds.getConnection();

 

 

源码:

 1 package org.wzh.testjndi.controller;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.sql.Connection;
 6 import java.sql.ResultSet;
 7 import java.sql.Statement;
 8 
 9 import javax.naming.Context;
10 import javax.naming.InitialContext;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.sql.DataSource;
16 
17 @SuppressWarnings("serial")
18 public class ServletDemo extends HttpServlet {
19 
20     @Override
21     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22         try (PrintWriter pw = resp.getWriter()) {
23             pw.print(0);
24             Context ctx = new InitialContext();
25             DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
26             Connection conn = ds.getConnection();
27             
28             Statement statement = conn.createStatement();
29             ResultSet resultSet = statement.executeQuery("select * from info");
30             while(resultSet.next()) {
31                 System.out.println(resultSet.getInt(1) + " **** " + resultSet.getString(2));
32                 pw.print(resultSet.getInt(1) + " **** " + resultSet.getString(2));
33             }
34             pw.print(1);
35         } catch (Exception e) {
36             // TODO: handle exception
37             e.printStackTrace();
38         }
39     }
40 
41 }
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>TestJndi</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.html</welcome-file>
 9         <welcome-file>index.htm</welcome-file>
10         <welcome-file>index.jsp</welcome-file>
11         <welcome-file>default.html</welcome-file>
12         <welcome-file>default.htm</welcome-file>
13         <welcome-file>default.jsp</welcome-file>
14     </welcome-file-list>
15 
16     <resource-ref>
17         <res-ref-name>jdbc/mysql</res-ref-name>
18         <res-type>javax.sql.DataSource</res-type>
19         <res-auth>Container</res-auth>
20     </resource-ref>
21 
22     <servlet>
23         <servlet-name>demo</servlet-name>
24         <servlet-class>org.wzh.testjndi.controller.ServletDemo</servlet-class>
25     </servlet>
26     <servlet-mapping>
27         <servlet-name>demo</servlet-name>
28         <url-pattern>/demo</url-pattern>
29     </servlet-mapping>
30 </web-app>

 

posted @ 2018-04-08 12:08  MicroCat  阅读(199)  评论(0编辑  收藏  举报