Alex Mathew
Alex Mathew

Reputation: 1554

How we can add unicode Text in MySQL using JAVA

How we can add unicode Text in MySQL using JAVA and reading it using JAVA

Upvotes: 0

Views: 1329

Answers (1)

The Alchemist
The Alchemist

Reputation: 3425

First, the character set of your MySQL VARCHAR column should be UTF-8.

ALTER TABLE t MODIFY latin1_varchar_col VARCHAR(M) CHARACTER SET utf8;

Then, you should just be able to use Statement.setString() without worry:

PreparedStatement updateSales = con.prepareStatement("UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75); 
updateSales.setString(2, "Colombian"); 
updateSales.executeUpdate():

Things to be careful of:

  • If you are reading text from a file, make sure you are reading the file in the right character set. See the constructor of InputStreamReader.
  • Be careful if you have columns in the database that are not in UTF-8 format!

That's all I can think of at the moment.

Upvotes: 1

Related Questions