using JDBC
These instructions were initially written by ChatGPT, then edited by the instructor.
Creating a PostgreSQL JDBC project in VS Code
Follow these steps to set up a new Java project in VS Code to interact with PostgreSQL via JDBC:
-
Install VS Code extensions
Make sure you have the Java Extension Pack installed in VS Code.
This gives you project support, autocompletion, and debugging. -
Create a new folder for your project
For example:QueryExecutorProject/
-
Inside the project folder, create and populate subfolders:
Create a
src/
folder andlib/
folder- Put your source file
QueryExecutor.java
into thesrc/
folder. - Put the external JAR
postgresql-42.7.7.jar
into thelib/
folder.
- Put your source file
- Open the project folder in VS Code
- In VS Code, go to File → Open Folder… and select
QueryExecutorProject
.
- In VS Code, go to File → Open Folder… and select
- Tell VS Code about your JAR files
- In the project root, create a
.vscode
folder (if it doesn’t exist). - Inside it, create a file called
settings.json
with the following contents:
{ "java.project.referencedLibraries": [ "lib/**/*.jar" ] }
This tells VS Code to include all JARs in the
lib/
folder. - In the project root, create a
- Edit Postgres settings in source code
- Open
QueryExecutor.java
. - Edit values such as
PG_PASSWORD
,PG_PORT_NUMBER
,schema_name
to match your installation of Postgres and the schema you wish to query.
- Open
- Run the program
- Open
QueryExecutor.java
. - You should see a Run and Debug button appear above the
main
method. - Click Run, and VS Code will compile and run the project, using the JAR on the classpath.
- Open
At this point, your QueryExecutor.java
can import and use classes from the external JAR, and VS Code will handle both editing and running.