Java Connection database read data garbled

Source: Internet
Author: User
Tags stmt

Because this is a general-purpose code, such as China's usual use of GBK, GB2312, Big5 and so on only for Chinese, but not for the other text is not applicable, in order to make this problem with the text encoding universal, so I set the UTF8 this code.

Coding consistency involves four aspects: application coding, database system coding, database encoding, application and database system connection coding.

1.mysql settings, my system character set is Latin, but also enough to remember to change after the discovery AH

2.java settings, remember yourself at the beginning of the time, set is: Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/xsxx", "root", "123456" );

In this way, when stored, there will be garbled characters of Chinese storage

In fact, only need to add "Characterencoding=utf8" to achieve the Chinese storage.

The results are as follows:

3. Code:

/*****
* Java connection MySQL
* @author Yanlong
*2017/5/7
*/
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.Statement;
Import java.util.Collection;
Import java.sql.SQLException;

Import javax.sql.Statement;

public class Testjdbc {
public static void Main (string[] args) {
ResultSet Rs=null;
Statement Stmt=null;
Connection Conn=null;
try{
/* Load and register the JDBC driver for MySQL */
Class.forName ("Com.mysql.jdbc.Driver");
/* Establish a connection to MySQL */

Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/xsxx", "root", "123456");
Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/xsxx?characterencoding=utf8", "root", "123456");
/* Access the database and execute the SQL statement */

Stmt=conn.createstatement ();
/* Add record */
System.out.println ("After adding records:");
String sqll= "INSERT into XS value (111111, ' small public who ', ' Water Conservancy ')";
Stmt.executeupdate (SQLL);
Rs=stmt.executequery ("SELECT * from XS");
while (Rs.next ()) {
System.out.println (Rs.getint ("id"));
System.out.println (rs.getstring ("name"));
System.out.println (Rs.getstring ("major"));
}
Rs=stmt.executequery ("Select *from xs");
while (Rs.next ()) {
System.out.println (Rs.getint ("id"));
System.out.println (rs.getstring ("name"));
System.out.println (Rs.getstring ("major"));
}

}catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try{
if (rs!=null) {
Rs.close ();
Rs=null;
}
if (stmt!=null) {
Stmt.close ();
Stmt=null;
}
if (conn!=null) {
Conn.close ();
Conn=null;
}
}catch (SQLException e) {
E.printstacktrace ();

}
}
}
}

Java Connection database read data garbled

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.