After nearly one day, I finally connected to the MySQL database. Here is a brief summary:
1. Configure the MySQL database first.
2. Download the MySQL driver mysql-connector-java-5-jar and add it to the current project
Package second; import Java. SQL. *; public class inensshow {public static string driver = "com. mySQL. JDBC. driver "; // MySQL database JDBC driver public static string url =" JDBC: mysql: // localhost: 3306/db1 "; // database urlpublic static connection conn; // The database connection class public static statement stmt; // The statement interface object is used to accommodate SQL statements. It can execute static SQL statements and return the results public static resultset Rs; // processing result set public static string SQL; // SQL statement public static void conn_ SQL () {try {class. forname (driver); // enable the database connection driver} catch (classnotfoundexception e) {e. printstacktrace (); system. out. println ("driver not found:" + E);} Try {conn = drivermanager. getconnection (URL, "root", "123456"); // connect to the database} catch (sqlexception SQL _e) {SQL _e.tostring (); system. out. println ("cann' t connection MySQL. ") ;}} public static void clo_ SQL () {try {Conn. close (); //} catch (sqlexception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {conn_ SQL (); // Add // modify // Delete try {stmt = Conn. createstatement (); SQL = "select * from user"; rs1_stmt.exe cutequery (SQL); While (RS. next () {string name = Rs. getstring ("name"); string passw = Rs. getstring ("password"); system. out. println ("usename:" + name + "userpassword:" + passw) ;}} catch (exception e) {e. printstacktrace () ;}clo_ SQL ();}}