Issue with special characters in Glassfish/Java EE6/MySQL - java

I am working on a small application that stores data in a MySQL database.
The data is various fields, some are in Turkish, Russian or Arabic. When inserting new record it is saved in the database in "??????" and other wired chars.
The architecture of the application is like this
JSP Form -> Servlet -> EJB -> JPA(EclipseLink) -> Glassfish (JDBC Pool <-> JDBC Resource)->MySQL
I already :
Changed the database and all tables and fields encoding to UTF-8
I added the "?CharSet=utf8&useUnicode=true&characterEncoding=utf8" to the URL of the database in the jdbc pool config in glassfish
But the issue didnt get solved

I found the solution for this issue, before dealing with the request paramters, just insert:
request.setCharacterEncoding("UTF-8");
And every thing will be ok
Link where I found the solution

I'm able to see only one source of trouble (maybe you omitted it in your question): I don't see the source encoding being considered.
From the JSP Form the user fills, you should somehow get the character set the data was encoded in; then, in the Servler or EJB you convert that data in your destinarion character set (utf-8) before storing it in the database (Java offers some API for this task, but I never used them directly so I can't give you any example).
I don't know about the parameters you added to the JDBC url, so I can't comment on them.

Related

How to insert JSON data to sql server without converting into Java beans

My requirement is like, i have a json file data as below.,
{Key1: value1, key2: value 2,....} with file name example.json
It should be inserted into sql server as,
Take name: example
Data as below,
Id key1 key2
1 value 1 value 2
I'm searching for a solution such that, there won't be any Java layer complexities, like first converting this data into Java beans then inserting into sql using java database drivers...
Implementation should be language and database independent. In future if i change my database server, then it should be a minimal change.
The coupling should be very loose. Like if i introduce a new key in JSON file, that should be minimal change in the solution.
Tia
So, use some library that can do http request to the server. For eg, if you want insert your json data to server in android app , then use simple libraries like Volley , OkHttp etc.
Problem with your question is , we dont know which server you r using and what application you are doing,, and which programming language you wanna use,, So, im assuming is java android,,

Unicode messages in oracle raise_application_error

I am working on enabling globalization support in my DB.
I have done migrating character set to UTF (AL16UTF16).
After migration, I can pass Unicode characters from Java to Oracle and store in table's NVARCHAR2 column. Also I can retrieve from DB and pass to Java.
But, If I do a raise_application_error with the Unicode data. It sends the error message to java like below
; nested exception is java.sql.SQLException: ORA-20001: ¿¿¿ ¿¿¿¿¿¿¿¿¿
Can anyone tell me what's wrong? and how can I get the Unicode error messages in java?
Thanks in advance.
The problem is I have done character set migration using the below steps, but it doesn't work for me.
1.Backup the database.
2.Run CSSCAN command.
3.Restart the DB with RESTRICT mode.
4.Run CSALTER script.
5.Restart the DB.
After that I have tried using the below steps.
1.Take backup of the DB using expdp command.
2.Create a new database with required character set (Unicode AL32UTF8).
3.Import the backup dump file into the newly created DB.
That's all. It works!
Now I don't need to use NVARCHAR2 data type to store unicode data (VARCHAR2 itself stores Unicode). raise_application_error also works fine (sends error messages with Unicode data to Java).
Thanks.

How to diagnose and fix hibernate / jpa apparently not sending unicode characters correctly as a parameter to a query on SQL 2008

I've got a database with names in it. These names sometimes contain non ascii characters e.g. González. I've got the collation settings such that if I search for WHERE LastName LIKE '%Gonzalez%' I get González's record back. In Management Studio I can search for both WHERE LastName LIKE '%Gonzalez%' and WHERE LastName LIKE '%González%' and both return the correct value. However when I use JPA / Hibernate the query that gets sent to the database clearly doesn't represent the á character correctly as I get 0 results.
When utilising the show_sql attribute I can see the actual query is fine, and If I copy and paste that query and replace the ? characters with '%González%' I get the correct results. Likewise if I search for Gonzalez through the web interface I get results, so I'm confident it's the á that is causing me problems, and it's only JPA / Hibernate that is causing the issue. (Having said that the issue could be the AJAX submission to the servlet that is causing the issue, but the parameter is sent as ?LastName=Gonz%C3%A1lez which I think is right?)
So if it's JPA / Hibernate how do I diagnose / fix the issue?
The show_sql logging configuration attribute only lets you see the formatted SQL statement, generated by Hibernate. To troubleshoot the problem further, you need to make sure the values, hibernate replaces the *'?'*s with, are actually correct. Look at the thread on how to see param values in hibernate log and adjust your application log settings.
The second step I'd suggest to add - is in your AJAX request, encode all your params as Base64 string, and then decode it back to UTF-8 string on the controller, handling the request.
The flow of logic should be as follow:
Client receives input 'González'
client encodes the input into 'R29uesOhbGV6' and passes it in AJAX request
controller, handling the request, decodes the parameter back to 'González'
controller passes the value down to hibernate logic, where hibernate generates SQL and executes it
in the application log, you see that hibernate actually passes 'González' parameter down to the database

Problem logging utf-8 strings in MySQL from Java

I am trying to log a string sent from client side to MySQL database from a Java application. The string sent from client is UTF-8 encoded. I have confirmed this as I have taken packet traces using tool like wireshark. The string that the client sends are 3 characters which are Latin OE (0xc593), and beta (0xc39f), Euro sign (0xE2 0x82 0xAC). I am using prepared statement way of setstring to log the string into database. The table is created with support for utf-8 char encoding. Now, when I see the logged string in database I find this
select hex(message) from table1
C385 C293 C383 C29F C3A2 C282 C2AC
Seems like something is changing the string in the middle. Could anyone help me to solve this problem?
Thanks.
According to the MySQL docs,
Client applications that need to
communicate with the server using
Unicode should set the client
character set accordingly; for
example, by issuing a SET NAMES 'utf8'
statement.
You should also check the character_set_client and character_set_connection system variables to get an idea of how MySQL is attempting to interpret your string.
You can check the collation that individual tables are using by running the SHOW TABLE STATUS IN database query, as well.
Hopefully that will give you a clearer picture of exactly what the MySQL server is trying to do with the strings you're sending from the client. Reading up in the docs should be enlightening, as well.
Good luck :-)

ms sql2000 arabic problem

I hava a table in ms sql2000 with a column defined as nvarchar
when query this table in java i get data for this column like this :
يا هلا بالشباب الحلوين يا شباب ا٠شلونكو؟.
When i try php with adodb i get the data as it should be ,in arabic.
but i need to use java not php ,please can any one help me.
i use a normal sql statement "select * from news"
i use the latest Microsoft jdbc driver(sqljdbc4.jar).
i have no direct access to the sql server.
That looks to me like an encoding issue, make sure you're using the proper encoding in Java to get the text back. Some variant of unicode obviously.
At every character processing step (getting data, modifying data, saving data, displaying data, etcetera) ensure that you're using UTF-8 character encoding.
If it is a client application, you usually only have to worry about it in the database table and if necessary also the JDBC connection string.
If it is a webapplication, then you need to take more into account: request and response encoding. For GET requests this is an appserver setting and for POST requests and all responses you can set it in the appropriate request/response objects.

Categories