Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to put ampersand (&) in a jdbc url?

I have a dynamic application with glassfish server and using EclipseLink (JPA 2.1). I used to could put jdbc configuration in the persistence.xml directly and didn't have any problem. But now it forces me to create a datasource name for glassfish and putting jdbc configuration into glassfish-resources.xml file.

the problem is that I want to set character encoding for mysql to utf8 and thus, I used below url for jdbc url:

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

but i get this exception:

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.

I want to know how to add multiple parameters in the url with ampersand? :D

also I want not to create a JNDI data source, instead I put the jdbc properties in the persistence.xml manually and ensured that I specified the JDBC driver correctly and added the MySQL JDBC driver to library, but I got the error message saying:

"no suitable driver found"

the complete content of the glassfish-resource is as following:

<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>
like image 587
jacksparrow92 Avatar asked Nov 04 '13 07:11

jacksparrow92


People also ask

How do you get ampersand on keyboard?

To create the ampersand symbol using a U.S. keyboard, hold down the Shift and press the 7 key on the top of the keyboard.

Do you put a space between &?

Leave one space before and after an ampersand. Use an apostrophe to show where letters have been omitted, for example, don't, shouldn't, o'clock. An apostrophe is also used to show possession, for example, Susan's new car. Leave no space between an apostrophe and the letters of the word it belongs to.

What is this symbol called &?

An ampersand is a sign for the word and. It's written or typed as the symbol &. It's a modification of the term “and per se and,” which has Latin origins. The ampersand can indicate that the listed items are grouped together as part of a name.


1 Answers

Try to turn;&amp; into just & and make sure to remove the leading semicolon. I think it is trying to read the value of convertToNull as convertToNull;, which is invalid...

So...

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

would become:

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

edit: also try removing the dash from utf-8 per https://stackoverflow.com/a/3042646/623952

There's a lot of information out there but I can't personally help you troubleshoot why it isn't taking the setting. Your initial error was because the querystring was incorrect. This is now a settings problem...

JDBC MySQL UTF-8 string writing problem
problem with utf8 in java

like image 114
gloomy.penguin Avatar answered Oct 02 '22 00:10

gloomy.penguin