Java JDBC link mysql insert data display garbled in Chinese

Source: Internet
Author: User
Tags mysql insert set set

1. Read data Normal, insert Chinese garbled view the character set set at the link, use Utf-8

String url = "Jdbc:mysql://127.0.0.1:3306/website?useunicode=true&characterencoding=utf-8";
The complete code is as follows:

Public Mysqlconnect () {String driver = "com.mysql.jdbc.Driver";//URL points to the database name to be accessed websitestring url = "jdbc:mysql:// 127.0.0.1:3306/website?useunicode=true&characterencoding=utf-8 ";//username String user =" root "when MySQL is configured;// Java connection MySQL configuration password string password = "Sanyue"; try {//Load driver Class.forName (driver); conn = (Connection) Drivermanager.getconnection (url,user, password), if (!conn.isclosed ()) System.out.println ("succeeded connecting to The database! "); /Statement used to execute SQL statement Statement = (Statement) conn.createstatement ();} catch (Exception e) {e.printstacktrace (); statement = null;}}
2. Check MySQL and the database itself by default whether the UTF8 encoding

Set character_set_database= "UTF8" is used here;
After the modification after restarting MySQL will not work, so please find your MySQL configuration file my.ini or my.cnf in [mysqld] to add Character_set_server=utf8, note that some are Default-character-set=utf8, if an error occurs when restarting the server, you can view the error log, and finally give me a few errors I encountered during the configuration

Mysql> Show variables like "character%", +--------------------------+----------------------------+| Variable_name            | Value                      |+--------------------------+----------------------------+| character_set_client     | UTF8                       | | character_set_connection | UTF8                       | | character_set_database   | UTF8                       | | character_set_filesystem | binary                     | | character_set_results    | UTF8                       | | Character_set_server |     UTF8 |                       | character_set_system     | UTF8                       | | character_sets_dir       |/usr/ share/mysql/charsets/|+--------------------------+----------------------------+8 rows in Set (0.00 sec)

3. See if the page itself is UTF8 encoded.

4. See if the encoding used by the page is UTF8 encoded

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%>

Basically these steps will not go wrong if they are normal, but there are a few things.

For example, I was using the set character_set_database= "UTF8" in this configuration, and the character encoding that was directly modified in the database, but never after restarting or after exiting the link, will revert back to the original latin1. So always thought the database itself code no problem.

In addition, links to the Java link database

String url = "Jdbc:mysql://127.0.0.1:3306/website?useunicode=true&characterencoding=utf-8";
don't write wrong, the middle & actually for &amp; Note (here because the compiler becomes &)

String url = "Jdbc:mysql://127.0.0.1:3306/website?useunicode=true&amp; Characterencoding=utf-8";


Here are some of the errors encountered:

MySQL error log

Innodb:unable to lock./ibdata1, Error:11
Innodb:check that already has another mysqld process
Innodb:using the same InnoDB data or log files.
Innodb:unable to lock./ibdata1, Error:11
Innodb:check that already has another mysqld process
Innodb:using the same InnoDB data or log files.
Innodb:unable to lock./ibdata1, Error:11
Innodb:check that already has another mysqld process
Innodb:using the same InnoDB data or log files.

Workaround:

[Email protected]:/var/lib/mysql# ps-ef | grep MySQL
MySQL 24477 1 0 16:35? 00:00:00/usr/sbin/mysqld
Root 24533 1908 0 16:35 pts/0 00:00:00/bin/sh/usr/bin/mysqld_safe
MySQL 25382 24533 0 16:37 pts/0 00:00:00/usr/sbin/mysqld--basedir=/usr--datadir=/var/lib/mysql--plugin-dir=/usr /lib/mysql/plugin--user=mysql--log-error=/var/log/mysql/error.log--pid-file=/var/run/mysqld/mysqld.pid--socket =/var/run/mysqld/mysqld.sock--port=3306
Root 25476 1908 0 16:37 pts/0 00:00:00/bin/sh/usr/bin/mysqld_safe
MySQL 25839 25476 0 16:37 pts/0 00:00:00/usr/sbin/mysqld--basedir=/usr--datadir=/var/lib/mysql--plugin-dir=/usr /lib/mysql/plugin--user=mysql--log-error=/var/log/mysql/error.log--pid-file=/var/run/mysqld/mysqld.pid--socket =/var/run/mysqld/mysqld.sock--port=3306
Root 26204 21859 0 16:38 pts/16 00:00:00 grep--color=auto MySQL
[Email protected]:/var/lib/mysql# kill-9 24477
[Email protected]:/var/lib/mysql# kill-9 24533

....


Error log

[Email protected]:/var/lib/mysql# tail. /.. /log/mysql/error.log
150616 16:40:19 innodb:using Linux native AIO
150616 16:40:19 innodb:initializing buffer pool, size = 128.0M
150616 16:40:19 innodb:completed initialization of buffer pool
150616 16:40:19 innodb:highest Supported file format is Barracuda.
150616 16:40:20 innodb:waiting for the background threads to start
150616 16:40:21 innodb:5.5.43 started; Log sequence Number 6583039
150616 16:40:21 [ERROR]/usr/sbin/mysqld:unknown variable ' Default-character-set=utf8 '
150616 16:40:21 [ERROR] Aborting

Solution:

Open MySQL config file (here in/etc/mysql/my.cnf)

Found in [mysqld]

Replace with Character_set_server=utf8. Default-character-set=utf8



Java JDBC link mysql insert data display garbled in Chinese

Related Article

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.