1. First install MySQL, add the bin directory to the PATH environment variable
2. Modify the SQL login name to 123456.update mysql.user set Authentication_string=password (' 123456 ') where user= ' root '
3.mysql download mysql-connector-java-5.1.44, project properties build path->configure build path->libraries->add external jars Add Add mysql-connector-java-5.1.44 folder inside the Mysql-connector-java-5.1.44-bin.jar component
4. Open MySQL service login, show database, add table
net start MySQL
mysql-uroot-p123456
Show databases
Use ABC
5 Connection Codes
Import java.sql.*;
public class Maindemo {
public static void Main (string[] args) {
try{
Call the Class.forName () method to load the driver
Class.forName ("Com.mysql.jdbc.Driver");
SYSTEM.OUT.PRINTLN ("Load MySQL driver successfully!") ");
}catch (ClassNotFoundException E1) {
System.out.println ("MySQL driver not found!");
E1.printstacktrace ();
}
String url= "JDBC:MYSQL://LOCALHOST:3306/ABC"; URL for JDBC
Connection Conn;
try {
conn = drivermanager.getconnection (URL, "root", "123456");
Create a statement object
Statement stmt = Conn.createstatement (); Create a statement object
System.out.print ("Successfully connected to the database! ");
String sql= "SELECT * from Abcbiao1";
ResultSet rs=stmt.executequery (SQL);
System.out.println ("number" + "\ T" + "name" + "\ T" + "age");
while (Rs.next ())
{
System.out.print (Rs.getint (1) + "\ T");
System.out.print (rs.getstring (2) + "\ T");
System.out.println ();
}
Rs.close ();
Stmt.close ();
Conn.close ();
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Connection not successful");
E.printstacktrace ();
}
}
}
Java link MySQL