Save unicode characters in DB using java - java

I need to save some uni-code characters in Sql server 2005 DB with JDBC, When i try to save "O’CON" into DB column having type NVarchar using a stored procedure it saves "O?CON" `(where ’ character is not `` or ')
I did some R&D and found that i need to add useUnicode=true&characterEncoding=UTF-8 some where in my DBcon.properties file that contains all the details of DB connection,
Can some one help me out where to add this into properties file or it i can add it at run time when i create a connection object.
Or if someone can help me how to save unicodes characters in DB using java.
Thanks.

To connect to database you use connect string specific to your database and driver.
I used MS MSQ long time ago but I remember that they had jtds driver. Connect string for such driver looks like:
jdbc:jtds:sqlserver://hostname:1433/my_database;useUnicode=true;characterEncoding=UTF-8
But from that time MS create their own JDBC driver and if you use it then I think you can add:
sendStringParametersAsUnicode=true
to your connect string. It is decribed at http://technet.microsoft.com/en-us/library/ms378988.aspx
In a case of problems show us your connect string and code where it is used.

Related

How to store and retrieve date wise data in MySQL through java?

I want to store and retrieve data (a single column table for each date) date wise in MySQL database through java. Any suggestions on how to do it?
Any transaction with database through java is possible using JDBC library. JDBC is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connect to the database.
Here is an overview of the basic steps involved:
Registering the driver class
Class.forName("com.mysql.jdbc.Driver").newInstance();
Creating connection
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=username&password=password");
Creating statement
Write your MySQL query for storing or retrieving data from database date wise
Executing queries
Based on the query, you may get some records returned as a result or the count of rows affected
Closing connection
For more details, please refer following links:
link1
link2

UTF-8 Queries with JDBC

i want to ask the MYSQL an UTF-8 Query but it does not work fine . when i try the following query , the result comes up truly :
String query = "select * from Terms where Term = 'lol'";
but with the following query doesn't make a response :
String query = "select * from Terms where Term = 'خدابخش'";
where the
'خدابخش'
part is in Persian and UTF-8 .
note that the connection to the database is fine .
Chances are that you may need to set your character encoding in your JDBC connection. If you are using MySQL JDBC Connector you do it using the property characterEncoding. Somewhat like this:
jdbc:mysql://localhost/some_db?useUnicode=yes&characterEncoding=UTF-8
You may want to read the reference on encoding and character sets in your connector JDBC documentation.
This is the one that mentions the use of characterEncoding for the MySQL JDBC Connector:
Connector JDBC: Using Character Sets and Unicode
One or more of the following is true:
The Java compiler, compiling your code, is set to read the source file with a different encoding in which the source file was actually stored. In other words, there is a discrepancy between the encoding that your editor uses, the encoding in which the file is actually saved, and the encoding with which the Java compiler is reading your source code.
Your database isn't set correctly to accept/store Unicode characters. Ensure that your database is set correctly. Looks like you're using MySQL. You may want to create a dump of the database using mysqldump and witness how the database was created with respect to character sets.

Is there a way to config MySQL Connector J driver to insert empty string as 0 to decimal/integer field?

I have a requirement to upgrade company's db legacy system from MySQL ver 4.1 to ver 5.5 ,I currently found out that if i insert empty string to decimal/integer field via java program ,It will throw exception but if i write the same statement and insert it directly via mysql command line the record will be inserted normally(the empty field will become 0),so this lead me to think that there are some problem with jdbc driver , is driver enforce some rule upon statement before pass it to db? i really dont want to re-write the old program to support this change.
thx in advance for your answer :)
You can assign value null not empty string.
You are changing your DB version so all codes may support. So you have to change

Execute Mysql query contains chinese character using java program

When I execute SQL query contains chines character, the query is executed successfully. When I open the mysql query browser I could see only ???? instead of Chinese texts. If the same query executed from the mysql query browser it works fine.
Try connecting with following switch
String url = "jdbc:mysql://host/database?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes";
All encoding must be the same (there are exceptions but let's forget them): the encoding used by the DB, the connection encoding use by the Java process and the one used by the query browser. You need to change the one used by Java, since the other two seem to be OK.

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 :-)

Categories