Java JDBC

Java JDBC

Java JDBC connection

package jdbc;
import java.sql.*;

public class PostgreSQLCRUDExample {
    // JDBC URL, username, and password
    static final String JDBC_URL = "jdbc:postgresql://localhost:5432/practicedb";
    static final String USERNAME = "postgres";
    static final String PASSWORD = "123456";

    public static void main(String[] args) {
        Connection connection = null;
        try {
            // Registering the JDBC driver
            Class.forName("org.postgresql.Driver");

            // Establishing the connection
            connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);

            // CREATE operation example
//            createRecord(connection, "John", 30);

            // READ operation example
            readRecords(connection);

            // UPDATE operation example
//            updateRecord(connection, "John", 35);

            // DELETE operation example
//            deleteRecord(connection, "John");

            // Closing the connection
            connection.close();
        } catch (ClassNotFoundException e) {
            System.out.println("PostgreSQL JDBC driver not found.");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("Failed to connect to the PostgreSQL database.");
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // Method to create a record
    private static void createRecord(Connection connection, String name, int age) throws SQLException {
        String insertSQL = "INSERT INTO example_table (name, age) VALUES (?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(insertSQL);
        preparedStatement.setString(1, name);
        preparedStatement.setInt(2, age);
        preparedStatement.executeUpdate();
        System.out.println("Record created successfully.");
        preparedStatement.close();
    }

    // Method to read records
    private static void readRecords(Connection connection) throws SQLException {
        String selectSQL = "SELECT emp_id, emp_name, dept_name, salary FROM \"30daysSQLQueryChallenge\".employee;";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(selectSQL);
        while (resultSet.next()) {
            int emp_id = resultSet.getInt("emp_id");
            String emp_name = resultSet.getString("emp_name");
            String dept_name = resultSet.getString("dept_name");
            int salary = resultSet.getInt("salary");
            System.out.println("emp_id: " + emp_id + ", emp_name: " + emp_name + ",dept_name:" + dept_name + "salary: " + salary);
        }
        resultSet.close();
        statement.close();
    }

    // Method to update a record
    private static void updateRecord(Connection connection, String name, int newAge) throws SQLException {
        String updateSQL = "UPDATE example_table SET age = ? WHERE name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(updateSQL);
        preparedStatement.setInt(1, newAge);
        preparedStatement.setString(2, name);
        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            System.out.println("Record updated successfully.");
        } else {
            System.out.println("Record not found for update.");
        }
        preparedStatement.close();
    }

    // Method to delete a record
    private static void deleteRecord(Connection connection, String name) throws SQLException {
        String deleteSQL = "DELETE FROM example_table WHERE name = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(deleteSQL);
        preparedStatement.setString(1, name);
        int rowsAffected = preparedStatement.executeUpdate();
        if (rowsAffected > 0) {
            System.out.println("Record deleted successfully.");
        } else {
            System.out.println("Record not found for delete.");
        }
        preparedStatement.close();
    }
}

Did you find this article valuable?

Support ๐Ÿ“’ Notes123 by becoming a sponsor. Any amount is appreciated!

ย