配置JNDI(坑爹货)
来源:互联网 发布:暖通设计软件 编辑:程序博客网 时间:2024/05/18 22:43
自学servlet已经接近尾声,但这个玩意儿的配置真他妈的头疼(本人小白,花了我1天时间,于是写下以帮助有需要的人)
1.本人配置
Myeclipse version 2017 C1 5
Tomcat version 8.5.9
项目未配置在Tomcat的webapp中,需映射
2.开始配置JNDI
a.进入Tomcat配置文件夹:(我的是:)C:\Users\Richard\Desktop\Test\j2EEStudyWorkSpace.metadata.me_tcat85\conf
b.全局JNDI数据源配置,修改servlet.xml
在<GlobalNamingResources>
标签里写如下配置
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource" maxTotal="100" maxIdle="30" maxWaitMillis="10000" username="root" password="123456" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/usersinfodatabase?useUnicode=true&characterEncoding=UTF-8&useSSL=false&ServerTimezone=GMT&2B8"/>
注意:
以前的maxActive已经用maxTotal替换
以前的maxWait已经用maxWaitMillis替换
c.映射项目
复制context.xml文件到Catalina\localhost目录下并改名为项目名
我的是:
编辑如下:
<Context path="/simpleTest" docBase="C:\Users\Richard\Desktop\Test\j2EEStudyWorkSpace\simpleTest\WebRoot" reloadable="false" debug="0"> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <ResourceLink name="mysqlDataSource" global="jdbc/mysql" type="javax.sql.DataSource"/> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --></Context>
d.项目所在WEB-INF目录下web.xml中添加JNDI配置的资源引用
我的是:
在<web-app>
标签内编辑如下:
<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>
e.项目java中连接数据库的代码:
import java.io.IOException;import java.sql.Connection;import java.sql.SQLException;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.sql.DataSource;public class ConnDB { private Connection conn; public ConnDB(){ } public Connection getConnection() throws IOException,SQLException{// //找驱动// try{// Class.forName("com.mysql.jdbc.Driver");// System.out.println("成功加载驱动");// }// catch(ClassNotFoundException e){// e.printStackTrace();// System.out.println("驱动未找到");// }// //连接数据库// try{// String url="jdbc:mysql://localhost:3306/usersinfodatabase?useUnicode=true&characterEncoding=UTF-8&useSSL=false&ServerTimezone=GMT&2B8";// String username="root";// String password="3729tpx520";// conn=DriverManager.getConnection(url,username,password);// // System.out.println("成功连接数据库");// }// catch(SQLException ex){// System.out.println("连接数据库失败");// for(Throwable e:ex){// e.printStackTrace();// }// }// return conn; try { InitialContext ctx=new InitialContext(); DataSource ds=(DataSource) ctx.lookup("java:comp/env/mysqlDataSource"); conn=ds.getConnection(); System.out.println("连接数据库成功"); } catch (NamingException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); System.out.println("连接数据库失败"); } return conn; }}
注释中是未配置JNDI时的写法,一旦访问量大便会效率低下
3.参考文章
JNDI学习总结(一)——JNDI数据源的配置
Tomcat数据库连接池配置
阅读全文
0 0
- 配置JNDI(坑爹货)
- jndi配置(Tomcat7)
- tomcat配置数据源 (JNDI)
- JNDI配置
- JNDI配置
- 配置JNDI
- JNDI配置
- JNDI配置
- JNDI配置
- JNDI配置
- JNDI配置
- 配置Jndi
- JNDI配置
- jndi配置
- jndi配置
- 配置JNDI
- JNDI配置数据源(tomcat上配置)
- jndi配置属性的含义(转)
- Ubuntu 16.04 LTS 初体验
- MySQL字段类型到JAVA类型的对应及转换
- Activity Handler 使用 WeakReference
- 盘点遭遇WannaCry勒索病毒影响的几大受害者
- spring cloud sidecar
- 配置JNDI(坑爹货)
- C语言提高-27讲: 编写查找和排序函数(查成绩)
- 安卓开发框架(MVP+主流框架+基类+工具类)--- 基类
- 使用本地域名 访问本地项目,本地host文件配置
- Linux 下网络配置
- Node VS C#
- 《了不起的盖茨比》:梦想的力量与方向
- Intel/美光的3D Xpoint能够撑起未来存储的一片天吗?
- 极简工作法 第一章