Package Com.java;
Import Java.sql.DriverManager;
Import com.mysql.jdbc.Connection;
Import Com.mysql.jdbc.ResultSet;
Import com.mysql.jdbc.Statement;
public class testmysql{
public static void Main (string[] args) {
try {
Class.forName ("Com.mysql.jdbc.Driver"); Load MySQL JDBC driver
Class.forName ("Org.gjt.mm.mysql.Driver");
System.out.println ("Success loading Mysql driver!");
}
catch (Exception e) {
System.out.print ("Error loading Mysql driver!");
E.printstacktrace ();
}
try {
Connection connect = (Connection) drivermanager.getconnection (
"Jdbc:mysql://localhost:3306/school", "Root", "" ");
The connection URL is the jdbc:mysql//server address/database name, and the following 2 parameters are login username and password, respectively
System.out.println ("Success connect Mysql server!");
Statement stmt = (Statement) connect.createstatement ();
ResultSet rs = (ResultSet) stmt.executequery ("SELECT * from student");
User is the name of your table
while (Rs.next ()) {
System.out.print ("+rs.getstring" ("Sno"));
System.out.print ("+rs.getstring" ("sname"));
System.out.print ("+rs.getstring" ("sdept"));
System.out.println ("+rs.getstring (" sage "));
}
}
catch (Exception e) {
System.out.print ("Get Data error!");
E.printstacktrace ();
}
}
}
Java and MySQL database connection test