Java Connection mysql! and can read Chinese

Source: Internet
Author: User

Package connect;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

public class Connectdb {

public static void Main (string[] args) {
Connection con;
String Driver = "Com.mysql.jdbc.Driver";
String url = "Jdbc:mysql://localhost:3306/zzh";
String user = "root";
String password = "abc123";

try {
Class.forName (driver);
con = drivermanager.getconnection (Url,user,password);
if (!con.isclosed ())
SYSTEM.OUT.PRINTLN ("Succeeded connecting to the database!");
Statement st = Con.createstatement ();
String sql = "SELECT * from shop";
ResultSet rs = st.executequery (SQL);
System.out.println ("------------------");
System.out.println ("? Execution results are as follows");
System.out.println ("------------------");
SYSTEM.OUT.PRINTLN ("Gender, Name, password");
System.out.println ("------------------");
String sex = null;
String name = NULL;
String pass = null;
while (Rs.next ()) {
Sex = rs.getstring ("Sex");
Name = rs.getstring ("name");
pass = rs.getstring ("Pass");
System.out.println (sex+ "\ t" +name+ "\ T" +pass);
}
Rs.close ();
Con.close ();
} catch (ClassNotFoundException e) {
System.out.println ("Sorry, can ' t find the driver!");
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
} finally{
SYSTEM.OUT.PRINTLN ("Successful acquisition of database Data");
}
}

}

PS: In principle if you access MySQL under the command line can display Chinese normally, then the program read out will certainly be able to display Chinese correctly.

No more need to use name = newString(name.getBytes("ISO-8859-1"),"gb2312"); 转码了

Ha ha

Java Connection mysql! and can read Chinese

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.