我有一个使用glassfish服务器并使用EclipseLink(JPA 2.1)的动态应用程序。我使用NetBeans IDE创建了此应用程序。我以前可以将jdbc配置直接放在persistence.xml中,没有任何问题。但现在它迫使我为glassfish创建一个数据源名称并将jdbc配置放入glassfish-resources.xml文件中(我认为这与JPA版本有关!)
问题是我想将mysql的字符编码设置为utf8,因此,我在urdb下面使用了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.
我想知道如何在&符号中添加多个参数? :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;&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>
答案 0 :(得分:3)
尝试将;&
转换为&
,并确保删除前导分号。我认为它试图将convertToNull
的值视为convertToNull;
,这是无效的......
所以......
jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull;&characterEncoding=UTF-8;
会变成:
jdbc:mysql://localhost:3306/fastfood?zeroDateTimeBehavior=convertToNull&characterEncoding=UTF8;
修改:同时尝试从utf-8
按https://stackoverflow.com/a/3042646/623952
那里有很多信息,但我个人无法帮助您解决为什么它没有采取设置。您的初始错误是因为查询字符串不正确。现在这是设置问题......
JDBC MySQL UTF-8 string writing problem
problem with utf8 in java
答案 1 :(得分:3)
问题是<&em>之前的分号&符号逃脱。只需删除它。
答案 2 :(得分:0)
jdbc:mysql://1.1.1.1:3306/dev?useUnicode=yes&characterEncoding=UTF-8
上面的连接字符串对我有用。 所以像@EJP说的那样,删除第一个分号。