|
Last change
on this file was 1eb7a55, checked in by Elena Markovska <elena.elenamarkovska@…>, 11 days ago |
|
Initial commit - Scholaris project code
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Rev | Line | |
|---|
| [1eb7a55] | 1 | package DAO;
|
|---|
| 2 |
|
|---|
| 3 | import java.sql.*;
|
|---|
| 4 |
|
|---|
| 5 | public class JDBCUtils {
|
|---|
| 6 |
|
|---|
| 7 | private static final String URL = "jdbc:postgresql://localhost:5432/universitydb";
|
|---|
| 8 | private static final String USER = "postgres";
|
|---|
| 9 | private static final String PASSWORD = "postgres";
|
|---|
| 10 |
|
|---|
| 11 | public static Connection getConnection() {
|
|---|
| 12 | Connection conn = null;
|
|---|
| 13 | try {
|
|---|
| 14 | // Se koristi PostgreSQL drajverot
|
|---|
| 15 | Class.forName("org.postgresql.Driver");
|
|---|
| 16 | conn = DriverManager.getConnection(URL, USER, PASSWORD);
|
|---|
| 17 | System.out.println("Uspeshna konekcija so PostgreSQL!");
|
|---|
| 18 | } catch (ClassNotFoundException e) {
|
|---|
| 19 | System.err.println("PostgreSQL drajverot ne e najden!");
|
|---|
| 20 | throw new RuntimeException(e);
|
|---|
| 21 | } catch (SQLException e) {
|
|---|
| 22 | System.err.println("Greshka pri povrzuvanje so bazata!");
|
|---|
| 23 | throw new RuntimeException(e);
|
|---|
| 24 | }
|
|---|
| 25 | return conn;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | public static void printSQLException(SQLException ex) {
|
|---|
| 29 | for (Throwable e : ex) {
|
|---|
| 30 | if (e instanceof SQLException) {
|
|---|
| 31 | e.printStackTrace(System.err);
|
|---|
| 32 | System.err.println("SQLState: " + ((SQLException) e).getSQLState());
|
|---|
| 33 | System.err.println("Error Code: " + ((SQLException) e).getErrorCode());
|
|---|
| 34 | System.err.println("Message: " + e.getMessage());
|
|---|
| 35 | Throwable t = ex.getCause();
|
|---|
| 36 | while (t != null) {
|
|---|
| 37 | System.out.println("Cause: " + t);
|
|---|
| 38 | t = t.getCause();
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 | } |
|---|
Note:
See
TracBrowser
for help on using the repository browser.