Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java connect to MySQL8.0 JDBC

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report