This article explains my problems
1. Connect to the Mysql database through direct jdbc connection, and write data from the program to the database in garbled solution.
When a program writes a piece of data to the Student table, garbled characters are generated when the database is written.
For example
Solution:
1. When creating a database:
Create database 'db'
Character set 'utf8'
COLLATE 'utf8 _ general_ci ';
2. When creating a table:
Create table 'tablea '(
'Id' varchar (40) not null default '',
'Userid' varchar (40) not null default '',)
ENGINE = InnoDB default charset = utf8;
However, I feel that the relationship is not great, but it can be used as a try.
3. This solution works effectively by modifying the configuration file of the Mysql installation directory.
You need to modify the encoding of the two locations in my. ini,
First
Second:
After setting, restart the service by running the command
The final problem is solved.
2. If garbled characters occur when reading data from the mysql database through jdbc, but the data in the database is not garbled, you can use the solution of adding parameters in the url.
The Url is written as follows:
Jdbc: mysql: /// localhost: 3306/database? UseUnicode = true & characterEncoding = UTF-8
3. If you replace localhost in the url with an IP address, the following problem occurs:
Error: 1130-host... is not allowed to connect to this MySql server
Solution:
1. Change the table.
It may be that your account is not allowed to log on remotely, but only on localhost. At this time, you only need to log in to mysql on the computer of localhost, and change the "host" entry in the "user" table in the "mysql" database to "%" from "localhost"
Mysql-u root-pvmwaremysql> use mysql;
Mysql> update user set host = '%' where user = 'root ';
Refresh authorization. This line of code must exist .. Flush privileges;
4. garbled solution for reading data from the database by JDBC-ODBC Bridge Connection
Do not directly read data. You can use the following method to convert and read data. The problem is solved.
Stringstuname = new String (rs. getBytes (2), "utf-16le ");
Conclusion: Garbled code occurs during JDBC and database interaction, which is a common problem during development. I hope the summary here will help you.