Coding in mysql in linux (UTF-8)
Today, I spent one day studying the encoding problem in mysql, and it was not a waste of effort.
At any time, the encoding must be unified, which is a prerequisite for ensuring that the data can be correctly written into the database.
First you need to configure your mysql Default encoding method for UTF-8.
For details, see the configuration of my. cnf.
(# Cp/usr/local/mysql/support-files/my-medium.cnf/etc/my. cnf)
# The following are important ================================================== ===
[Client]
# Password = your_password
Port = 3306
Socket =/tmp/mysql. sock
Default-character-set = utf8
[Mysqld]
Port = 3306
Socket =/tmp/mysql. sock
Skip-locking
Key_buffer = 16 M
Max_allowed_packet = 1 M
Table_cache = 64
Sort_buffer_size = 512 K
Net_buffer_length = 8 K
Read_buffer_size = 256 K
Read_rnd_buffer_size = 512 K
Myisam_sort_buffer_size = 8 M
Default-character-set = utf8
#====================================
After the configuration is complete, go to mysql and run # status. The following result is displayed.
--------------
Mysql Ver 14.12 Distrib 5.0.41, for pc-linux-gnu (i686) using readline 5.0
Connection id: 1
Current database: jsp
Current user: root @ localhost
SSL: Not in use
Current pager: stdout
Using outfile :''
Using delimiter :;
Server version: 5.0.41-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket:/tmp/mysql. sock
Uptime: 1 hour 12 min 21 sec
Threads: 2 Questions: 390 Slow queries: 0 Opens: 18 Flush tables: 1 Open tables: 8 Queries per second avg: 0.090
--------------
This shows that mysql encoding method is UTF-8.
The following is a problem with tomcat encoding. I set each page to contentType = "text/html; charset = UTF-8", but the request is used. getParameter () is garbled, which only indicates that the encoding method is incorrect during the request.
Add request. setCharacterEncoding ("UTF-8") to the page for processing parameters ");
. Then OK ....
The following is the test file.
// Index. jsp
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" import = "java. SQL. *" errorPage = "" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<% @ Include file = "conn. jsp" %>
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<Div align = "center" class = "style1">
<P> Add Student Information </p>
<Form name = "form1" method = "post" action = "add. jsp">
<P> <span> name: </span>
<Input type = "text" name = "name">
</P>
<P>
& Nbsp;
<Input type = "submit" name = "queding" value = "submit">
& Nbsp;
<Input type = "reset" name = "chongtian" value = "reset">
</P>
<P> & nbsp; </p>
</Form>
<P> & nbsp; </p>
<%
ResultSet rs1_stmt.exe cuteQuery ("select * from jsp ");
Rs. beforeFirst ();
While (rs. next ())
{
%>
<P> <% = rs. getString ("name") %> </p>
<%
}
Rs. close ();
Stmt. close ();
Conn. close ();
%>
</Div>
</Body>
</Html>
// Add. jsp
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" import = "java. SQL. *" errorPage = "" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<% @ Include file = "conn. jsp" %>
<Html>
<Head>
<Title> </title>
</Head>
<Body>
<%
Request. setCharacterEncoding ("UTF-8"); // you can try it out first.
String name = request. getParameter ("name ");
String SQL = "insert into jsp (name) values ('" + name + "')";
Stmt.exe cuteUpdate (SQL );
Stmt. close ();
Conn. close ();
%>
<Center>
<H2> Add Student Information <P> <% = name %> <br>
The new data has been added to the database!
</P>
<P> </p>
<Form name = "form1" method = "post" action = "index. jsp">
<Input type = "submit" id = "back" name = "back" value = "return">
</Form>
<P> & nbsp; </p>
</Center>
</Body>
</Html>
// Conn. jsp
<%
String host = "localhost: 3306 ";
String user = "root ";
String pw = "******";
String db = "jsp ";
String tab = "jsp ";
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
String url = "jdbc: mysql: //" + host + "/" + db;
Connection conn = DriverManager. getConnection (url, user, pw );
Statement stmt = conn. createStatement ();
%>
Create a database
Mysql> create database jsp;
Mysql> use jsp;
Mysql> create table jsp (
-> Name varchar (20) default NULL
-> );
Run mysql> show create database jsp;
+ ---------- + -------------------------------------------------------------- +
| Database | Create Database |
+ ---------- + -------------------------------------------------------------- +
| Jsp | create database 'jsp '/*! 40100 default character set utf8 */|
+ ---------- + -------------------------------------------------------------- +
Mysql> show create table jsp;
+ ------- + Response +
| Table | Create Table |
+ ------- + Response +
| Jsp | create table 'jsp '(
'Name' varchar (20) default NULL
) ENGINE = MyISAM default charset = utf8 |
+ ------- + Response +
Mysql> inster into jsp (name) values ('job ');
Mysql> select * from jsp;
+ ------ +
| Name |
+ ------ +
| Job |
+ ------ +
With this explicit view, mysql is okay.