天天看点

JNDI 配置服务器数据源

1.什么是JNDI?

中文:java名称目录接口 一种技术

2.JNDI做什么?

JNDI可以把java应用程序访问数据库管理和配置交给存放java应用的服务器(tomcat)。

3.没有JNDI之前?

JDBC连接数据库,java应用程序与mysql提供的驱动程序关联 映射一个Driver的Class对象(Driver的path:com.mysql.jdbc.Driver)

然后用java提供的DriverMannager管理类调用getConnection(“jdbc:mysql://localhost:8080/数据库名字”,“用户名”,“密码”)方法

获得一个connection连接对象,说明java与指定数据库建立连接。

缺点:

   *当所有程序调试完成后,所有java源文件形成jar包,如果数据库类型改变orcal或用户名改变,必须改源代码

4.有JNDI之后

把变化的地方交给服务器来设置建立一个数据源,需要JDBC建立连接时引用,交给服务器管理配置,如果java应用程序需要就去那一个

5.使用JNDI建立数据库连接池

第一步:安装java应用服务器根目录找到conf文件夹里放的服务器上下文配置文件context编辑后增加数据源代码:

<Resource name=“jdbc/news” auth=“Container” type=“javax.sql.DataSource”//数据源名字 验证数据源的容器 类型

  maxActive=“100” maxIdle=“30” maxWait=“10000” username=“root”//最大连接数据库连接对象的数量100 最大空闲数是30(例如50个空闲等待N时间后变成maxWait状态)数据库用户名

password=“123” driverClassName=“com.mysql.jdbc.Driver” //数据库密码 驱动程序名称(mysql提供)

url=“jdbc:mysql://localhost:3306/newsdb?useUnicode=true&amp;characterEncoding=utf8”//创建连接的路径,编码格式UTF-8

/>

第二步:把java即将操作的数据库的驱动文件导入java应用服务器,粘贴至java应用服务器根目录lib文件夹(作用放程序需用引用的jar包),放入此包的jar

java应用服务器上所有项目都可以用。

第三步:web项目上web.xml进行注册:

 <resource-ref>

    <res-ref-name> jdbc/news </res-ref-name>//名字与数据源名字一致

    <res-type> javax.sql.DataSource </res-type>//类型:DateSource

    <res-auth> Container </res-auth>

  </resource-ref>

第四步:java应用程序处引用数据源

Context ic=new InitialContext();//接口、实现类

DataSource source=(DataSource)ic//实现类对象.lookup(“java:comp/env/jdbc/news”);//调用lookup方法返回一个数据源类型DateSource

conn=source.getConnection();//生成JDBC连接对象

<li class="tool-item tool-active is-like "><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" ><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup" target="_blank" rel="external nofollow" ></use>
                    </svg><span class="name">点赞</span>
                    <span class="count">6</span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G" target="_blank" rel="external nofollow" ></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" ><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang" target="_blank" rel="external nofollow" ></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/qidao12" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow" >
                <img src="https://profile.csdnimg.cn/5/2/0/3_qidao12" class="avatar_pic" username="qidao12">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/10.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/qidao12" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">qidao12</a></span>
                                        </div>
                <div class="text"><span>发布了20 篇原创文章</span> · <span>获赞 15</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=qidao12" target="_blank" rel="external nofollow"  target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>