Talk about the garbled solution of Java Program and database interaction

Source: Internet
Author: User

This paper explains the problems I have encountered

1. Through the JDBC Direct connection, connect Mysql database, from the program to write data to the database garbled solution.

When a program writes a piece of data to the Student table, the content written to the database is garbled.

Such as

Solution:

1. When creating a database:

CREATE DATABASE ' Db '
CHARACTER SET ' UTF8 '
COLLATE ' Utf8_general_ci ';


2. when the table is built :      

CREATE TABLE ' TableA ' (
' ID ' varchar (+) not NULL default ' ',
' UserID ' varchar (+) not NULL default ', ')
Engine=innodb DEFAULT Charset=utf8;

But I feel the relationship is not very big, but can be used as a means of trying.

3. This program is effective, is to modify the Mysql installation directory configuration file

You need to modify the encoding for the two locations in the My.ini

First place

Second place:

After setting, restart the service via the command

Final problem resolution.

2. If it is garbled to read data from the MySQL database through jdbc , but the data in the database itself is not garbled, then you can use the URL The solution for adding parameters.

The URL is spelled 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 issue occurs:

Error : 1130-host allowed to connect to this MYSQL server

Workaround:

1. Change the table method.

It may be that your account is not allowed to log on remotely, only on localhost. This time, as long as the computer on the localhost , log in to MySQL , the "MySQL" database to change the " " Host" item in User "table", renamed " %" from "localhost "

Mysql-u root-pvmwaremysql>use MySQL;

Mysql>update User Set host = '% ' where user = ' root ';

To refresh the authorization, the line code must have: FLUSH privileges;

4.jdbc-odbc Bridge Connection method reading data from the database garbled solution

Do not read directly, you can use the following ways to convert the read, problem solving

Stringstuname=new String (Rs.getbytes (2), "Utf-16le");

Conclusion:JDBC and database interaction garbled is often encountered in the development of difficult problems, I hope the summary here can 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.