Detailed explanation of how to connect to MySQL database using Java in IntelliJ IDEA

Detailed explanation of how to connect to MySQL database using Java in IntelliJ IDEA

1. Download MySQL database and install and configure it

Download address: https://dev.mysql.com/downloads/installer/

insert image description here

2. Download the JDBC Connector

Download address: mysql-connector-java-8.0.22

insert image description here
insert image description here

After downloading the compressed package and decompressing it, find the mysql-connector-java-8.0.22.jar file and put it in the path you specified.

3. Import jar package into the project

insert image description here

Test class Test.java code for testing database connection:

import java.sql.Connection;
import java.sql.DriverManager;

public class Test {
  public static void main(String[] args) {
    String driverName = "com.mysql.cj.jdbc.Driver";
    
    String dbURL="jdbc:mysql://localhost:3306/websql?&useSSL=false&serverTimezone=Asia/Shanghai"; //websql is the database name String userName = "root";
    
    String userPwd = "root";
    
    try {
      Class.forName(driverName);
      
      Connection con = DriverManager.getConnection(dbURL, userName, userPwd);
      
      System.out.println("Connecting to database successfully");
      
    } catch (Exception e) {

      e.printStackTrace();

      System.out.print("Connection failed");

    }
  }

}

This is the end of this article about how to use Java to connect to MySQL database in IntelliJ IDEA. For more information about how to use Java to connect to MySQL database in IntelliJ IDEA, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to connect to database in IDEA
  • Five ways to use JDBC to connect to the database in Java (IDEA version)
  • Detailed steps to use IDEA to configure Tomcat and connect to MySQL database (JDBC)
  • JDBC-idea import mysql to connect java jar package (mac)
  • Java-based MAC system IDEA connects to MYSQL database JDBC process

<<:  In-depth understanding of the creation and implementation of servlets in tomcat

>>:  vue+element-ui implements the head navigation bar component

Recommend

Use Visual Studio Code to connect to the MySql database and query

Visual Studio Code is a powerful text editor prod...

In-depth analysis of Linux NFS mechanism through cases

Continuing from the previous article, we will cre...

IDEA2020.1.2 Detailed tutorial on creating a web project and configuring Tomcat

This article is an integrated article on how to c...

Detailed steps for configuring Tomcat server in IDEA 2020

The steps for configuring Tomcat in IDEA 2020 are...

Detailed explanation of how to configure Nginx web server sample code

Overview Today we will mainly share how to config...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Linux remote control windows system program (three methods)

Sometimes we need to remotely run programs on the...

How to use environment variables in nginx configuration file

Preface Nginx is an HTTP server designed for perf...

Nginx configuration to achieve multiple server load balancing

Nginx load balancing server: IP: 192.168.0.4 (Ngi...

Example of implementing TikTok text shaking effect with CSS

In daily development, front-end students often ar...

JavaScript parseInt() and Number() difference case study

Learning objectives: The two functions parseInt()...

Practice of dynamically creating dialog according to file name in vue+el-element

Table of contents background accomplish 1. Encaps...