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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11428964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:05:08  来源:igfitidea点击:

connecting to derby database with tomcat as the server

javajakarta-eetomcatdatabase-connectionderby

提问by Suhail Gupta

How do i connect to derby database (that comes with the netbeans)? I am using Tomcat as the server. Earlier i used the following statements to connect to the derby database,but then i used glassfishas the server.

我如何连接到 derby 数据库(与 netbeans 一起提供)?我使用Tomcat作为服务器。早些时候我使用以下语句连接到 derby 数据库,但后来我用作glassfish服务器。

Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/PollDatasource");
Connection connection = ds.getConnection();

But now using Tomcat as the server i am unaware how to do this.

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

Note : Tomcat and Derby are pre installed with netbeans IDE that i am using currently

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

回答by Raknel

In Tomcat find conf/context.xml, then edit and write something like this:

在 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" />

Note 1: With the above URL the driver will be org.apache.derby.jdbc.ClientDriver

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

Note 2 : You can also add the above information in META-INF/context.xml of your project. This becomes application specific.If you add the information in tomcat's context.xml that becomes global.

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

Note 3: Download the jar from this website.Download db-derby-10.9.1.0-bin.zip.It contains many files, including derby.jar and derbyclient.jar (along with much documentation).derbyclient.jar contains our friend org.apache.derby.jdbc.ClientDriver.class. derby.jar contains org.apache.derby.jdbc.EmbeddedDriver. Keep the downloaded jar in lib folder of Tomcat.

注3:从这个网站下载jar。下载db-derby-10.9.1.0-bin.zip。它包含很多文件,包括derby.jar和derbyclient.jar(还有很多文档)。derbyclient.jar包含我们的朋友org.apache.derby.jdbc.ClientDriver.class。derby.jar 包含org.apache.derby.jdbc.EmbeddedDriver. 将下载的jar 保存在Tomcat 的lib 文件夹中。

and in your application web.xml "resource-ref":

并在您的应用程序 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>

You may want to look at these questions :

你可能想看看这些问题:

回答by carlspring

You need to:

你需要:

1) Copy your derbyclient-*.jarto ${TOMCAT_HOME}/lib.

1) 将您的复制derbyclient-*.jar${TOMCAT_HOME}/lib.

2) Edit your server.xmland add the following lines to the section GlobalNamingResources:

2) 编辑您的server.xml并将以下几行添加到 GlobalNamingResources 部分:

 <Resource auth="Container" 
           driverClassName="org.apache.derby.jdbc.EmbeddedDriver" 
           maxActive="8" maxIdle="4" 
           name="jdbc/my-ds" type="javax.sql.DataSource" 
           url="jdbc:derby:mydb;create=true" 
           username="myuser" password="mypassword" />

3) In your context definition, add:

3)在您的上下文定义中,添加:

 <Context docBase="myapp"
          path="/myapp"
          reloadable="true"
          ...>
    <ResourceLink name="jdbc/my-ds"
                  global="jdbc/my-ds"
                  type="javax.sql.DataSource" />
 </Context>

4) Restart Tomcat.

4) 重启Tomcat。

回答by TedTrippin

The example you have requires JNDI. See the relevant tomcat versions docs on setting that up.

您的示例需要 JNDI。有关设置,请参阅相关的 tomcat 版本文档。

Or use a connection string, here's a page from derby docs http://db.apache.org/derby/integrate/plugin_help/derby_app.html

或者使用连接字符串,这是来自 derby 文档的页面http://db.apache.org/derby/integrate/plugin_help/derby_app.html