package DAO;

import java.sql.*;

public class JDBCUtils {

    private static final String URL = "jdbc:postgresql://localhost:5432/universitydb";
    private static final String USER = "postgres";
    private static final String PASSWORD = "postgres";

    public static Connection getConnection() {
        Connection conn = null;
        try {
            // Se koristi PostgreSQL drajverot
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            System.out.println("Uspeshna konekcija so PostgreSQL!");
        } catch (ClassNotFoundException e) {
            System.err.println("PostgreSQL drajverot ne e najden!");
            throw new RuntimeException(e);
        } catch (SQLException e) {
            System.err.println("Greshka pri povrzuvanje so bazata!");
            throw new RuntimeException(e);
        }
        return conn;
    }

    public static void printSQLException(SQLException ex) {
        for (Throwable e : ex) {
            if (e instanceof SQLException) {
                e.printStackTrace(System.err);
                System.err.println("SQLState: " + ((SQLException) e).getSQLState());
                System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
                System.err.println("Message: " + e.getMessage());
                Throwable t = ex.getCause();
                while (t != null) {
                    System.out.println("Cause: " + t);
                    t = t.getCause();
                }
            }
        }
    }
}