如何连接到 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 .下载 db-derby-10.9.1.0-bin.zip。它包含许多文件,包括 derby.jar 和 derbyclient.jar(以及许多文档)。derbyclient.jar 包含我们的 friend org.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>
你可能想看看这些问题:
关于java - 使用tomcat作为服务器连接到derby数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428964/