-1

I have problems when I insert into tables words with accent marks. So I think that I have to "activate" UTF-8 to fix that error.

I'm not using Class for name. That's my code:

miInitialContext = new InitialContext();
miDS = (DataSource) miInitialContext.lookup(InformacionProperties.getStrDataSource());
Connection conexion = miDS.getConnection();
Statement myStatement = conexion.createStatement();
myStatement.executeUpdate("INSERT INTO table values ......)

How can I "activate" that UTF8 with my code?

DRVR
  • 9
  • 1
  • You need to use the correct character set, which may or may not be utf8. You should determine what character set you need to use before changing any of these settings. – Shadow Oct 24 '17 at 19:18
  • I want to use Spanish characters: á, é, í, ó, ú, ñ. What can I do? If I insert a row with one of that characters, that character changes. – DRVR Oct 24 '17 at 19:51
  • First of all, you can do a little bit of research into the various character sets. Then you could determine what character set you currently use and whether it is suitable for you. Then determine what character set your application uses when it connects to your mysql database. After all these steps you can make an educated decision whether you need to change any character sets - if any. – Shadow Oct 24 '17 at 21:30
  • I only want to connect inserts with utf8 but I dont know why. Can you tell me how??? – DRVR Oct 24 '17 at 23:04
  • LIke that: https://stackoverflow.com/questions/730359/problems-reading-writing-utf-8-data-in-mysql-from-java-using-jdbc-connector-5-1 but i dont have DriveManager – DRVR Oct 24 '17 at 23:07
  • See "best practice" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 27 '17 at 03:03

1 Answers1

-1

Use set names to set your connection charset. However, that won't matter much if the columns/tables/databases you interact with aren't configured with a compatible charset. For instance, with a latin1 column testcol, inserting utf8 data will result in an error like

INSERT INTO `test`.`table` (`testcol`) VALUES ('test_val'), ('Ídata');
ERROR 1366 (HY000): Incorrect string value: '\xE2\x88\x9A\xC3\xA7d...' for column 'testcol' at row 2

So you'll need to update the table structure

ALTER TABLE t MODIFY testcol CHAR(50) CHARACTER SET utf8;

Which then fixes the issue:

INSERT INTO `test`.`table` (`testcol`) VALUES ('test_val'), ('Ídata');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

(See the mysql docs for details).

This post has good documentation on finding the character sets of various structures.

(Thanks to Shadow for the SET NAMES part)

Spencer
  • 297
  • 2
  • 10
  • Nope, this is not correct. See the use of `set names`. – Shadow Oct 24 '17 at 19:18
  • Aha, thanks. I didn't actually know about that. I guess I'll update my answer. – Spencer Oct 24 '17 at 19:26
  • I would not bother. 1) the OP may not even need utf8 2) even if she/he does, this question has already been answered here on SO several times. 3) jdbc has its own syntax for setting character sets, you should not use set names. – Shadow Oct 24 '17 at 19:45