【问题标题】:Can't connect application to database无法将应用程序连接到数据库
【发布时间】:2011-07-27 17:48:24
【问题描述】:

我在使用 Eclipse 数据源浏览器将我的 Web 应用程序连接到数据库时遇到问题。

这就是我所做的:

也许我确实配置了错误的驱动程序。这就是我从 eclipse Helios 配置驱动程序定义的方式。 Window->Preferences->DataManagement->Connectivity->Driver Definitions:

1

2

3

4

我能够启动应用程序服务器,甚至可以通过浏览器访问应用程序。但我无法与数据库交互。这是 webapp 的配置文件的样子:

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="jdbc/GroupBuySystem">
<class>entities.Administ</class>
<class>entities.Buyer</class>
<class>entities.Comment</class>
<class>entities.Log</class>
<class>entities.Offer</class>
<class>entities.Seller</class>
</persistence-unit>
    </persistence>

sun-resources.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0  Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/myDatasource" object-type="user" pool-name="Derby_groupbuydb_userPool"/>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="org.apache.derby.jdbc.ClientDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="Derby_groupbuydb_userPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
    <property name="PortNumber" value="1527"/>
    <property name="DatabaseName" value="groupbuydb"/>
    <property name="User" value="user"/>
    <property name="Password" value="pwd"/>
    <property name="URL" value="jdbc:derby://localhost:1527/groupbuydb;create=true"/>
<property name="driverClass" value="org.apache.derby.jdbc.ClientDriver"/>
</jdbc-connection-pool>
</resources>

我还想提一下,我使用以下命令从控制台启动数据库:

C:\glassfishv3\bin>asadmin 启动数据库

我做错了什么?为什么我无法连接到数据库?

【问题讨论】:

  • Tnx :) 我希望我能找到解决这个问题的方法,我被它困住了,我无法继续我的编程。有人有什么想法吗?
  • 你能从另一个应用程序连接到服务器吗?例如来自ij,或来自H2 database 的控制台工具?
  • 我想知道您是否真的需要分别指定 URL URL 中的所有属性(服务器名称、端口、数据库名称)...如果是, 如果“大小写”正确(小写 serverName、大写 PortNumber 等等)。
  • 是的,我还有其他可以在此服务器上运行的应用程序(但目前已关闭项目),如图 1 所示,每个应用程序都有其单独的数据连接。我只能启动服务器并在浏览器中运行应用程序。我不使用 H2 控制台工具。
  • 当我右键单击项目时添加的文件 sun-web.xml -> 新的 JDBC 资源,所有默认情况下的信息我认为都是正确的。

标签: java eclipse jpa jdbc derby


【解决方案1】:

你有错误的 jar 文件。为您的驱动程序使用 derbyclient.jar 而不是 derby.jar

derby.jar 用于嵌入模式的 derby。 derbyclient.jar 用于网络/服务器模式,这就是您在连接字符串中定义的内容:

<property name="URL" value="jdbc:derby://localhost:1527/groupbuydb;create=true"/>

【讨论】:

    猜你喜欢
    • 2020-11-26
    • 2016-09-27
    • 1970-01-01
    • 2012-10-09
    • 2017-10-08
    • 2020-04-23
    • 2021-04-06
    • 2018-07-25
    • 1970-01-01
    相关资源
    最近更新 更多