Connecting Java Apps to MySQL/PostgreSQL

Connecting Java Applications to MySQL and PostgreSQL

Introduction

When building a Java application, you often need to store and manage data.
To do that, Java apps connect to databases like MySQL or PostgreSQL.
This guide explains how to connect your Java app to these databases using JDBC (Java Database Connectivity).


Prerequisites

Before starting, make sure you have:

✅ Java installed
✅ MySQL or PostgreSQL installed
✅ JDBC driver for the database
✅ A sample database created
✅ An IDE (like Eclipse or IntelliJ)


Step-by-Step: Connecting Java to MySQL

1. Add MySQL JDBC Driver

Download the MySQL Connector/J from:
https://dev.mysql.com/downloads/connector/j/

Add the .jar file to your project’s classpath.

In Maven:

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency>

2. Sample Java Code

import java.sql.*; public class MySQLConnection { public static void main(String[] args) { String url = "jdbc:mysql://localhost:3306/mydb"; // Replace 'mydb' with your database String user = "root"; // Your MySQL username String password = "password"; // Your MySQL password try { Connection conn = DriverManager.getConnection(url, user, password); System.out.println("Connected to MySQL!"); conn.close(); } catch (SQLException e) { System.out.println("Connection failed: " + e.getMessage()); } } }

Step-by-Step: Connecting Java to PostgreSQL

1. Add PostgreSQL JDBC Driver

Download the PostgreSQL JDBC driver from:
https://jdbc.postgresql.org/

In Maven:

<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.1</version> </dependency>

2. Sample Java Code

import java.sql.*; public class PostgreSQLConnection { public static void main(String[] args) { String url = "jdbc:postgresql://localhost:5432/mydb"; // Replace 'mydb' with your DB String user = "postgres"; // Your PostgreSQL username String password = "password"; // Your password try { Connection conn = DriverManager.getConnection(url, user, password); System.out.println("Connected to PostgreSQL!"); conn.close(); } catch (SQLException e) { System.out.println("Connection failed: " + e.getMessage()); } } }

Important Notes

  • Use DriverManager.getConnection() to connect.

  • Always close connections after use to avoid memory leaks.

  • Use try-with-resources for better exception handling.

  • Store credentials securely (not hardcoded in real apps).

  • Use PreparedStatement for SQL queries to prevent SQL injection.


Conclusion

Connecting a Java app to MySQL or PostgreSQL is simple with JDBC.
Once connected, you can run SQL queries, read/write data, and build real-world apps like:

  • Web apps

  • Inventory systems

  • Student portals

  • Finance dashboards

Start with basic connection, and you’ll be working with full CRUD operations in no time!


Learn Full Stack Java Course


Read More 




Comments

Popular posts from this blog

Tosca System Requirements and Installation Guide (Step-by-Step)

How to Install Selenium for Python Step-by-Step

Tosca Commander: A Beginner’s Overview