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