JAVA - Zero to Hero / JDBC - Java Database Connectivity

Setting up JDBC

🌐 Install JDBC Driver in VS Code (Windows + macOS)

πŸ“ Step 1: Download the JDBC Driver

Choose the right driver for your database:

Database

Driver Name

Official Download

MySQL

MySQL Connector/J

πŸ”— Download

PostgreSQL

PostgreSQL JDBC Driver

πŸ”— Download

SQLite

SQLite JDBC (Xerial)

πŸ”— Download

Oracle

Oracle JDBC Thin Driver

πŸ”— Download

SQL Server

Microsoft JDBC Driver

πŸ”— Download

🧾 You'll get a .jar file, such as mysql-connector-java-8.0.xx.jar.


πŸ’Ό Step 2: Add JDBC .jar to Your Java Project

πŸ“Œ For Both Windows & macOS:

  1. Copy the .jar file into your project folder (you can create a folder like lib/ if you want).

    • Example location:

      ProjectFolder/
      β”œβ”€β”€ lib/
      **β”‚   └── mysql-connector-java-8.0.29.jar**
      └── src/
          └── YourJavaFile.java
  2. Open VS Code, go to your project.

  3. Press Ctrl + Shift + P (or Cmd + Shift + P on Mac) β†’ Type:

    Preferences: Open Settings (JSON)
  4. Add this configuration:

{
  "java.project.referencedLibraries": [
    "lib/mysql-connector-java-8.0.29.jar"
  ]
}

πŸ“ Make sure the path matches where you placed the JAR file (e.g., lib/, or root folder).

Save and restart VS Code if needed.


πŸ–₯️ Extra: Run the Project

βœ… Compile (Windows/macOS terminal):

javac -cp ".;lib/mysql-connector-java-8.0.29.jar" YourJavaFile.java # Windows
javac -cp ".:lib/mysql-connector-java-8.0.29.jar" YourJavaFile.java # macOS/Linux

▢️ Run:

java -cp ".;lib/mysql-connector-java-8.0.29.jar" YourJavaFile       # Windows
java -cp ".:lib/mysql-connector-java-8.0.29.jar" YourJavaFile       # macOS/Linux

Note the difference: ; on Windows, : on macOS/Linux when using -cp (classpath).