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

Analysis of the methods of visual structure layout design for children's websites

1. Warm and gentle Related address: http://www.web...

Introduction to fork in multithreading under Linux

Table of contents Question: Case (1) fork before ...

Incredible CSS navigation bar underline following effect

The first cutter in China github.com/chokcoco Fir...

React implements multi-component value transfer function through conetxt

The effect of this function is similar to vue的pro...

CSS sample code to achieve circular gradient progress bar effect

Implementation ideas The outermost is a big circl...

CSS achieves the effect of two elements blending (sticky effect)

I remember that a few years ago, there was an int...

How to insert video into HTML and make it compatible with all browsers

There are two most commonly used methods to insert...

MyBatis dynamic SQL comprehensive explanation

Table of contents Preface Dynamic SQL 1. Take a l...

vue-cli4.5.x quickly builds a project

1. Install vue-cli npm i @vue/cli -g 2. Create a ...

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...

Superficial Web Design

<br />I have always believed that Yahoo'...

Use of Linux usermod command

1. Command Introduction The usermod (user modify)...