import java.sql.*; import java.text.simpledateformat; import java.util.*; import java.util.Date; import java.text.*; import java.util.calendar; public class conn { public Static void main (String[] args) { conn lm = new conn (); string t= lm.getstringdate (); try { class.forname (" Com.mysql.jdbc.Driver ") //load MYSQL JDBC drivers } catch (exception e) { e.printstacktrace (); } try { Connection connect = Drivermanager.getconnection ("Jdbc:mysql://localhost:3307/student", "root", "Zhangyabiao"); //connection URL is jdbc: mysql//server address/database name  &NBSP, and the following 2 parameters are login username and password, respectively system.out.println ("[" +t+ "]" + "successfully connect to Database! "); statement stmt = connect.createstatement (); resultset rs = stmt.executequery ("Select * from zh"); //user the name of your table while (Rs.next ()) { System.out.println (Rs.getstring ("n") + ". " +rs.getstring ("N1") + " " +rs.getstring ("N2"); } system.out.println ("[" +t+ "]" + "successfully obtained data"); } catch (exception e) { system.out.print ("[" +t+ "]" + "failed to obtain data"); E.printstacktrace (); } } Public static string getstringdate () { date currenttime = new date (); SimpleDateFormat formatter = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); string datestring = Formatter.format (currenttime); return datestring; } }
Results test
/* [2017-05-08 22:05:48] successfully connected to the database! ...... [2017-05-08 22:05:48] Successful data acquisition results in the above, indicating that you are connected to the database successfully. */
Java How to connect MYSQ source code L explained