How does Java connect to MySQL8.0 JDBC
In this article, the editor introduces in detail "how to connect Java to MySQL8.0 JDBC". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to connect Java to MySQL8.0 JDBC" can help you solve your doubts.
one。 Import jar package
1. Download the jar package
two。 Import
Create a new folder called lib under the project folder
Put the downloaded jar package in the lib folder, then right-click the lib folder, select Add as Library..., and then click ok
two。 Code part
1. Load driver
Class.forName ("com.mysql.cj.jdbc.Driver")
two。 User information and url
String url = "jdbc:mysql://localhost:3306/ database name? & useSSL=false&serverTimezone=UTC"
3. Database object Connection
Connection connection = DriverManager.getConnection (url,username,password)
4. Execute the database object connection
Statement statement = connection.createStatement ()
Code presentation
Package com.lofun.fuxi.JDBC;import java.sql.*;public class jdbc_used {public static void main (String [] args) throws ClassNotFoundException, SQLException {/ / 1. Load driver Class.forName ("com.mysql.cj.jdbc.Driver"); System.out.println ("load driver succeeded!"); / / 2. User information and url String url = "jdbc:mysql://localhost:3306/ activity? & useSSL=false&serverTimezone=UTC"; String username = "root"; / / database user name String password = "123456"; / / database password / / 3. If the connection is successful, the database object Connection represents the database Connection connection = DriverManager.getConnection (url,username,password); / / 4. The object that executes SQL Statement Statement statement = connection.createStatement (); String sql = "SELECT * FROM students"; ResultSet resultSet_01 = statement.executeQuery (sql); / / the result set of the query that encapsulates all the query results statement.executeQuery () executes the sql statement while (resultSet_01.next ()) {System.out.println (resultSet_01 .getObject ("name")) / / resultSet_01 .getObject gets the specified data type} / / closes resultSet_01.close (); statement.close (); connection.close () }} after reading this, the article "how to connect Java to MySQL8.0 JDBC" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.