DEV Community

MANIKANDAN
MANIKANDAN

Posted on

JDBC Connectivity in Java (Java Database Connectivity)

What is JDBC?

JDBC (Java Database Connectivity) is a Java API that lets Java programs talk to databases. It provides classes and interfaces to send SQL queries, get results, and manage database connections. Using JDBC, we can build applications that work with different databases like MySQL, PostgreSQL, Oracle, and more. Basically, it helps Java programs store, read, and update data in a database easily.

The main 6 steps for connection of JDBC

                     Import packages
                           |
                      Load Driver  
                           |
                     Register Deiver
                           |
                   Create a connection
                           | 
                     Create Statement
                           |
                         Close
Enter fullscreen mode Exit fullscreen mode
import java.sql.*;
import java.util.Scanner;

public class Crud_Op_Ps {

    public static void main(String[] args) {
        String url = "jdbc:postgresql://localhost:5432/crud_op";
        String user = "manikandan";
        String password = "tnlm@2004";

        try (Connection con = DriverManager.getConnection(url, user, password);//ip 
                Scanner sc = new Scanner(System.in)) {

            System.out.println("DB connected...");

            while (true) {
                System.out.println("\n--- CRUD Menu ---");
                System.out.println("1. Insert User");
                System.out.println("2. Update User");
                System.out.println("3. Delete User");
                System.out.println("4. View Users");
                System.out.println("5. Exit");
                System.out.print("Enter your choice: ");
                int choice = sc.nextInt();
                sc.nextLine();

                switch (choice) {
                case 1:
                    System.out.print("Enter name: ");
                    String name = sc.nextLine();
                    System.out.print("Enter email: ");
                    String email = sc.nextLine();

                    String insert = "INSERT INTO student(name, email) VALUES (?, ?)";
                    try (PreparedStatement ps = con.prepareStatement(insert)) {       //ip
                        ps.setString(1, name);
                        ps.setString(2, email);
                        ps.executeUpdate();
                        System.out.println("User inserted.");
                    }
                    break;

                case 2:
                    System.out.print("Enter old name: ");
                    String oldName = sc.nextLine();
                    System.out.print("Enter new name: ");
                    String newName = sc.nextLine();

                    String update = "UPDATE student SET name=? WHERE name=?";
                    try (PreparedStatement ps = con.--prepareStatement(update)) {//ip
                        ps.setString(1, newName);
                        ps.setString(2, oldName);
                        int rows = ps.executeUpdate();
                        if (rows > 0) {
                            System.out.println("User updated.");
                        } else {
                            System.out.println("No user found with that name.");
                        }
                    }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)