加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 以tomcat作为服务器连接到derby数据库

发布时间:2020-12-14 23:36:47 所属栏目:Java 来源:网络整理
导读:我如何连接到derby数据库(netbeans附带)?我使用Tomcat作为服务器.之前我使用以下语句连接到derby数据库,但之后我使用glassfish作为服务器. Context context = new InitialContext();DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/PollDa
我如何连接到derby数据库(netbeans附带)?我使用Tomcat作为服务器.之前我使用以下语句连接到derby数据库,但之后我使用glassfish作为服务器.
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/PollDatasource");
Connection connection = ds.getConnection();

但现在使用Tomcat作为服务器,我不知道如何做到这一点.

注意:Tomcat和Derby预先安装了我目前使用的netbeans IDE

解决方法

在Tomcat中找到conf / context.xml,然后编辑并编写如下内容:
<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
    driverClassName="com.YourDriver" 
    url="jdbc:derby://localhost:1527/nameOfTheDatabase;create=true"
    username="username" password="password" maxActive="20" 
    maxIdle="10" maxWait="-1" />

注1:使用上面的URL,驱动程序将是org.apache.derby.jdbc.ClientDriver

注意2:您还可以在项目的META-INF / context.xml中添加以上信息.这将成为特定于应用程序.如果您在tomcat的context.xml中添加信息,该信息将变为全局.

注3:从this website下载jar.Download db-derby-10.9.1.0-bin.zip.它包含许多文件,包括derby.jar和derbyclient.jar(以及大量文档).derbyclient.jar包含我们的朋友组织. apache.derby.jdbc.ClientDriver.class. derby.jar包含org.apache.derby.jdbc.EmbeddedDriver.将下载的jar保存在Tomcat的lib文件夹中.

并在您的应用程序web.xml“resource-ref”:

<resource-ref>
    <description>my connection</description>
    <res-ref-name>jdbc/PollDatasource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

您可能想看看这些问题:

> Isn’t it necessary to mention the name of archive in the Resource tag?
> When is the tag I added in context.xml gets read?
> What are steps followed in the look-up? what is looked first web.xml or context.xml?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读