1.java connect MySQL database: First need to load MySQL driver,: https://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.1.40.zip
2. Place the downloaded jar driver under the project's Lib folder, and then bulid in path.
3. Register the JDBC Driver:
Class.forName ("Com.mysql.jdbc.Driver");
4. Create a database connection for the connection object:
// replace your own database address Private Static final url= "Jdbc:mysql://localhost:3306/book"// database user name privatestatic final user= "root"// database password privatestaticfinal Password= "123456"Connection conn=drivermanager.getconnection (Url,user,password)
You need to import Java.sql.connection,java.sql.drivermanager instead of java.mysql.Connection here.
5. Perform the operation with the statement object:
Statement stmt =conn.createstatement ();//Get query Results ResultSet rs=stmt.executequery (SQL);
6. Close the database resource:
Rs.close (); Stmt.close (); Conn.close ();
Java JDBC Connection