Talking about the garbled solution for Java program and database Interaction

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.