参考: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获取连接

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

源码:

 package org.wzh.testjndi.controller;

 import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource; @SuppressWarnings("serial")
public class ServletDemo extends HttpServlet { @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try (PrintWriter pw = resp.getWriter()) {
pw.print(0);
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection(); Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from info");
while(resultSet.next()) {
System.out.println(resultSet.getInt(1) + " **** " + resultSet.getString(2));
pw.print(resultSet.getInt(1) + " **** " + resultSet.getString(2));
}
pw.print(1);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
} }
 <?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>TestJndi</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>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref> <servlet>
<servlet-name>demo</servlet-name>
<servlet-class>org.wzh.testjndi.controller.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>

JavaEE--JNDI(下,实现)的更多相关文章

  1. SpringMVC框架下的异常处理

    在eclipse的javaEE环境下:导包.... 1. 在 @ExceptionHandler 方法的入参中可以加入 Exception 类型的参数, 该参数即对应发生的异常对象 2. @Excep ...

  2. SpringMVC框架下的拦截器

    在eclipse的javaEE环境下:导包.... web.xml文件中的配置: <?xml version="1.0" encoding="UTF-8" ...

  3. 在SpringMVC框架下实现数据的国际化(即数据实现多国文字之间的转换)

    在eclipse中javaEE环境下:导入必要的架包 web.xml配置文件: <?xml version="1.0" encoding="UTF-8"? ...

  4. 在SpringMVC框架下实现文件的 上传和 下载

    在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...

  5. SpringMVC框架下实现JSON(类方法中回传数据到jsp页面,使用jQuery方法回传)

    JSON的实现,即将需要的数据回传到jsp页面: 1>.加入实现Json的三个架包到lib中:2>.目标方法上边加入注解,需要返回的值3>.在jsp页面中书写jQuery方法: ec ...

  6. SpringMVC框架下数据的增删改查,数据类型转换,数据格式化,数据校验,错误输入的消息回显

    在eclipse中javaEE环境下: 这儿并没有连接数据库,而是将数据存放在map集合中: 将各种架包导入lib下... web.xml文件配置为 <?xml version="1. ...

  7. javaEE环境搭建-eclipse

    1.       javaEE环境搭建: (1)     JDK1.8 (2)     eclipse-JavaEE (3)     tomcat-7.0.90 下载地址: https://tomca ...

  8. JNDI 笔记(一) 概述

    很多地方都会用到JNDI,一大堆的缩写加上一大堆不清不楚的概念描述,使得在看到的时候都不认识,更不要说使用了.   JNDI,Java Naming Directory Interface,J2EE的 ...

  9. 浅入tomcat

    前言:学习笔记,以供参考 1.什么是服务器 所谓的服务器其实就是一段别人写好的程序,服务器有两个能力. a.可以帮助我们来管理资源. b.可以将资源向外界发布以便于外界来访问这个资源. 2.资源有哪些 ...

  10. Eclipse搭建SSH(Struts2+Spring+Hibernate)框架教程

    | 版权声明:本文为博主原创文章,未经博主允许不得转载. 前言 确实,刚创博客,对于这个陌生的东西还是有些许淡然.这是我的第一篇博文,希望能给你们有帮助,这就是我最大的乐趣! 好了下面进入正题: SS ...

随机推荐

  1. nodejs 编译时对项目进行配置

    1.启动项目设置配置信息 2.读取项目配置文件 3.根据配置的文件读取自定义属性 4.根据相关属性配置其他差异 5.其他

  2. html的适配

    html值得一说的应该就是适配 !!适配是与手机同时存在的 写好一个页面在手机端打开,会发现这个页面显示很小,那是因为设备的视口宽度viewport不等于设备宽度device-width,而页面是根据 ...

  3. 3 JVM配置参数

  4. jquery动态选中radio,获取radio选中值

    //动态选中radio值,1:表示radio的name 2:表示后台传过来的radio值$(":radio[name='1'][value='" + 2 + "']&qu ...

  5. 数据结构——java Queue类

    定义 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作. LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用 图例 Que ...

  6. SwiftUI中多设备运行方法

    https://blog.csdn.net/weixin_42679753/article/details/94465674 https://www.jianshu.com/p/17fc7929fcb ...

  7. pyhton matplotlib可视化图像基础(二维函数图、柱状图、饼图、直方图以及折线图)

    //2019.07.22pyhton中matplotlib模块的应用pyhton中matplotlib是可视化图像库的第三方库,它可以实现图像的可视化,输出不同形式的图形1.可视化图形的输出和展示需要 ...

  8. java.neo的ByteBuffer与Netty 的ByteBuf

    JDK的ByteBuffer的缺点: 1.final byte[] hb;这是JDKde ByteBuffer对象中用于存储数据的对象声明;可以看到,其字节数组是被声明为final的,也就是长度是固定 ...

  9. JuJu团队11月28号工作汇报

    JuJu团队11月28号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达 解决了数据接口的bug 生成generator形式, 并用熟悉Julia处理数据的方法 处理数据步 ...

  10. Window Server 2019 配置篇(7)- 利用脚本创建OU,组和用户

    好的,服务器到上一步为止,基本的雏形已经确立了,接下来我们要完善一些细节 打开AD-admin服务器,创建一个脚本,要求能够从csv文件中读出数据并建立OU,组和用户,以及他们的密码 这里有一个参考的 ...