Java Insert Database garbled solution __c#

Source: Internet
Author: User
Java Insert Database garbled solution
1. Situation when the problem occurs;

Using Navicat for Mysql, the tables created are:

room_id varchar
Room_name Char
Room_info varchar

Where the character set is used: UTF8 (optional in navicat)


Java accesses a database in the following ways:

<span style= "FONT-SIZE:14PX;" >public Connection getconnection () {//Set the return value to Connection method
		try {//Load Database driver class
			Class.forName (" Com.mysql.jdbc.Driver ");
			SYSTEM.OUT.PRINTLN ("Database-driven load succeeded");
		} catch (ClassNotFoundException e) {
			e.printstacktrace ();
		}
		try {//Get the database connection object by accessing the URL of the database
			con = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydatabase", "root" , "123123");
			SYSTEM.OUT.PRINTLN ("Database connection succeeded");
		} catch (SQLException e) {
			e.printstacktrace ();
		}
		return con; Returns a connection object as required by method
	}</span></span>

Java gets the information from the Web page is normal, print out the running statement is:

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:14PX;" >insert into Table_room (room_id, Room_name, Room_info) VALUES ("H1", "good", "very good")
</span></span>

However, the use of navicat for MySQL to see the results of the operation found that the inserted Chinese characters are problematic, English normal, Chinese have become ...
2. Solution to the problem

Invalid Workaround:

To modify the encoding in Navicat, I modified the character set to GB2312 but it didn't work.

the right way to solve

The linked database is an error in the way the URL was filled out, which should be:

con = drivermanager.getconnection ("jdbc:mysql://localhost:3306/mydatabase?useunicode=true&characterencoding= UTF8 "," root "," 123123 ");

Note the following additions to the

? Useunicode=true&characterencoding=utf8

If the above method is unsuccessful, the following recommendations refer to:

1. Change UTF8 to UTF-8

2. This writing

? useunicode=true&amp;characterencoding=utf-8

Reference: www.cnblogs.com/amboyna/archive/2008/06/18/1224570.html


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.