How Macos uses Visual Studio code to connect Mysql with Java


Beginner Java, read green hand The tutorial of connecting Java to Mysql is missing the process of configuring jar files. Only Windows environment and Linux environment can be downloaded
For novices, I'm very confused, so record it If you have the same environment as me, I hope you can help

Use environment

  • Computer: Macbook Air 2020
  • System: Macos 11
  • Code tool: Visual Studio Code
  • Database: cloud mysql5 seven
  • Language: Java 14.0.1

I don't have any projects. I just study. In this regard, I can't find specific solutions online

Configuration process

1. Download Jar file

  • The download address is the official website of Mysql MySQL :: Download Connector/J , you need to download the plug-in for connecting Java to Mysql
  • Select Platform Independent and download the in ZIP format

2. Key points of importing Jar file

  • Unzip the download file, and only take mysql-connector-java-5.1.49 in the folder Jar file

  • Open Visual Studio Code, enter the software settings (shortcut key ⌘,), and enter Java in the search bar of the settings project. referencedLibraries 1 , click on settings Edit in JSON

  • Copy the full address of the jar file 2 , posted on settings JSON file 3

3. Java connection code

The code is the complete code on the rookie. The database address, account number and password have been processed by me

import java.sql.*;

public class MysqlDemo {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://";
    static final String USER = "hahaha";
    static final String PASS = "hahaha";
    public static void main(String[] args) {
        Connection  conn = null;
        Statement   stmt = null;

        try {
            // Register driver
            // Open link
            System.out.println("Connecting Database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            // Execute query
            System.out.println("Instancize Statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "select title,url from hahaha.log_mp_article";
            ResultSet rs = stmt.executeQuery(sql);

            // Query results
            while ( {
                // The field type needs to be specified
                String title    = rs.getString("title");
                String url      = rs.getString("url");
                // Print information
                System.out.print("title: " + title);
                System.out.print("address: " + url);
            // Disconnect database
        } catch (SQLException  se) {
            // JDBC error
        } catch (Exception e) {
        } finally {
            // Close the database connection again
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se2) {

Run again successfully to obtain the database content

Trampled pit

  1. If the file is not imported after downloading, an error is reported
  1. On the Internet, most of them choose to import build path in Maven or Eclipse to import drivers, but VSCode has no corresponding path, so people can't start
  2. The solution is to add classpath, but it is based on Windows system, and Macos is still too small for users
  3. The Mysql plug-in download official website selects the operating system without the option of Macos, which makes people wonder. I downloaded it all and found the jar file before I knew it was on Platform Independent
  4. In the same environment as me, the jar file does not need to be further decompressed Because some solutions are imported after extracting the jar


When there is no answer in the end, the official documents are reliable I am inspired by the official document and need to put it into VSCode One is the official documentation of Mysql plug-in and the other is the official documentation of VSCode But it's all in English. It needs English foundation

MySQL Connector/J 8.0 Developer Guide

Managing Java Projects in VS Code


  1. This setting depends on the VSCode plug-in Project Manager for Java. When you use Java for the first time in the software, you will be prompted to download Java. My plug-in automatically downloads this plug-in when you download the Extension Pack for Java for the first time If you don't download it, remember to download it ↩︎

  2. My file address is / users / Danzhao / onedrive / Doc / Java / mysql-connector-java-5.1.49 jar ↩︎

  3. My is no Java project. The reference libraries line is, so I manually added and entered the complete content ↩︎

Keywords: Java MySQL JDBC Visual Studio Code macOS

Added by youngsei on Fri, 11 Feb 2022 15:41:47 +0200