5

我有一个带有 glassfish 服务器并使用 EclipseLink (JPA 2.1) 的动态应用程序。我以前可以直接将jdbc配置放入persistence.xml,没有任何问题。但现在它迫使我为 glassfish 创建一个数据源名称并将 jdbc 配置放入glassfish-resources.xml文件中。

问题是我想将mysql的字符编码设置为utf8,因此,我在下面的url中使用了jdbc url:

jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull;&characterEncoding=UTF-8;

但我得到了这个例外:

javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: The connection property 'zeroDateTimeBehavior' only accepts values of the form: 'exception', 'round' or 'convertToNull'. The value 'convertToNull;' is not in this set.

我想知道如何用&符号在url中添加多个参数?:D

我也不想创建 JNDI 数据源,而是手动将 jdbc 属性放在 persistence.xml 中,并确保我正确指定了 JDBC 驱动程序并将 MySQL JDBC 驱动程序添加到库中,但我收到错误消息:

“找不到合适的驱动程序”

glassfish-resource的完整内容如下:

<resources>
    <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="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" 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="mysql_rootPool" 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=""/>
        <property name="User" value="root"/>
        <property name="Password" value=""/>
        <property name="URL" value="jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull;&amp;characterEncoding=UTF-8;"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </jdbc-connection-pool>
    <jdbc-resource enabled="true" jndi-name="Fastfood" object-type="user" pool-name="mysql_rootPool"/>
</resources>
4

3 回答 3

3

问题是&符号转义之前的分号。只需将其删除。

于 2013-11-04T09:01:55.297 回答
3

尝试;&amp;变成 just&并确保删除前导分号。我认为它正在尝试读取convertToNullas的值convertToNull;,这是无效的......

所以...

jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull;&amp;characterEncoding=UTF-8;

会成为:

jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8;

编辑:也尝试从utf-8每个https://stackoverflow.com/a/3042646/623952中删除破折号

那里有很多信息,但我无法亲自帮助您解决为什么它没有采用该设置。您最初的错误是因为查询字符串不正确。现在是设置问题...

java中使用utf8的JDBC MySQL UTF-8字符串写入问题

于 2013-11-04T07:46:27.143 回答
2
jdbc:mysql://1.1.1.1:3306/dev?useUnicode=yes&amp;characterEncoding=UTF-8

上面的连接字符串对我有用。就像@EJP 说的那样,删除第一个分号。

于 2017-09-29T17:09:47.657 回答