简体   繁体   中英

can't insert utf-8 characters in mysql database using crontab to automate the program

I had asked the question before but because I didn't receive any guide, I'm asking it again. Here's the original question: fail to insert non-english characters to database using crontab to run the program

I wrote a java program that reads from a UTF-8 file and insert its contents to mysql database. I can call jar file in terminal and it works fine. after that I used crontab to automate running the program but this time only english chars and numbers will insert to DB.

My database encoding is "utf8-general-ci" and the server I'm running the program is centOs 6.3.

I should mention that I have another program that its functionality is similar to this and it works fine.

I have tested so many things but none of them fixed the problem. I don't know what's the problem.

I'm looking for a solution or any clue to continue and solve the problem. I'll appreciate any help.

EDIT: mysql version is :5.1.69

and here is 2 images. one is for the time I run the program with crontab and the other when running the program in terminal

在此处输入图片说明

在此处输入图片说明

I'm not realy an expert on this but here goes nothing. I think this is related to what's documented in http://dev.mysql.com/doc/refman/5.6/en/alter-table.html

Is your default character set on latin1 or something like that ? Perhaps you left it default and i think it then uses latin1.

In any case i think you have to do something like this:

ALTER TABLE target_table_name MODIFY latin1_text_col TEXT CHARACTER SET utf8;
ALTER TABLE target_table_name MODIFY latin1_varchar_col VARCHAR(M) CHARACTER SET utf8;

You can find out the default char set of the database by doing:

use db_name;
show variables like "character_set_database";
show variables like "collation_database";

I couldn't find anyway to fix the problem so I wrote a schedular program that runs the application and it works fine!!!

But I'm still confused why crontab makes that problem or what I did that program doesn't work fine!!!

Assume that you have set you db, table, and columns using utf8mb4, don't forget specify your connection to mysql using utf8 charset :

String url = "jdbc:mysql://ip:port/db?characterEncoding=utf8";
String username = "xxx";
String password = "xxx";

try {
    conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
    e.printStackTrace();
}

--

I'm curious about the root cause of your problems as well...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM