/**
* Created by admin on 2017/1/28.
*/
Import java.sql.*;
public class MySQL {
public static void Main (string[] args) {
JDBC Drive, you need to download the drive yourself, or you cannot connect to the database
String Driver = "Com.mysql.jdbc.Driver";
Connection Address
String url = "Jdbc:mysql://127.0.0.1:3306/china";
String user = "root";
String Password = "admin";
try{
Load MySQL Driver
Class.forName (driver);
Connection conn = drivermanager.getconnection (URL, user, password);
if (!conn.isclosed ()) {
System.out.println ("Connect successfully!");
}
Statement Statement = Conn.createstatement ();
String sql = "show databases;";
ResultSet res = statement.executequery (SQL);
String data = null;
while (Res.next ()) {
System.out.println (Res.first ());
}
}
catch (ClassNotFoundException e) {
SYSTEM.OUT.PRINTLN ("Connect fail can ' t not found driver");
E.printstacktrace ();
}
catch (SQLException e) {
E.printstacktrace ();
}
}
}
JDBC Connection MySQL