In the Java program to connect MySQL, first to the MySQL website to download the driver, and install, the default installation on the C drive (my is the default installation, the directory is C:\Program files\mysql\mysql Connector J), If MySQL is also the default installation, the path you just installed is below the MySQL directory, the address is http://www.mysql.com/products/connector/, and if it is Java, download the JDBC Driver for MySQL ( CONNECTOR/J)
After the download completes the installation, the Mysql-connector-java-5.1.30-bin.jar is introduced in the development tool after the installation, and the following code is introduced:
Package Com.jll.model;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.LinkedList;
Import java.util.List;
public class Driver {
public static void Main (string[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List List = new LinkedList ();
try {
Class.forName ("Com.mysql.jdbc.Driver");
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost/mydata?" +
"User=root&password=root");
stmt = Conn.createstatement ();
rs = Stmt.executequery ("SELECT * from article");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
}
try {
while (Rs.next ()) {
int i = rs.getint ("id");
List.add (i);
}
} catch (SQLException e) {
E.printstacktrace ();
}finally{
try {
Rs.close ();
Stmt.close ();
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
for (int i = 0;i<list.size (); i++) {
System.out.println (List.get (i));
}
}
}