配置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&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;ServerTimezone=GMT&amp;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数据库连接池配置

原创粉丝点击