package com.force.samples.entity; import java.sql.*; public class RetrieveData { public static void main(String args[]) { Connection conn = null; PreparedStatement prsment = null; ResultSet res = null; try { String usname = "devdbuser"; String usPassword = "postgres"; String host = "jdbc:postgresql://localhost:5432/devdb"; String drayver = "org.postgresql.Driver"; Class.forName(drayver); conn = DriverManager.getConnection(host, usname, usPassword); prsment = conn.prepareStatement("SELECT * FROM Users"); res = prsment.executeQuery(); while (res.next()){ System.out.print(res.getString(2) + " "); System.out.print(res.getString(3) + " "); System.out.print(res.getString(4) + " "); System.out.print(res.getString(5) + " "); System.out.print(res.getString(6) + " "); System.out.print(res.getInt(7)); } /* while (res.next()){ int userID = res.getInt("userId"); String userN = res.getString("userName"); String userF = res.getString("userFam"); String userLog = res.getString("userLogin"); String userPsw = res.getString("userPass"); String userPos = res.getString("userEmail"); String userEm = res.getString("userEmail"); int userVozrast = res.getInt("userAge"); System.out.println(userID + " " + userN + " "+ userF + " " + userLog + " " + userPsw + " " + userPos + " " + userEm + " " + userVozrast); }*/ } catch (Exception n) { System.out.println("Другая ошибка, проверьте свой код"); } catch (ClassNotFoundException e) { System.out.println("Проверьте драйвера, правильно ли указали "); } catch (SQLException e) { System.out.println("Не удалось подключение к БД"); } finally{ try{ if (res!=null){ res.close(); } if (prsment!=null){ prsment.close(); } if (conn!=null){ conn.close(); } } catch (SQLException e) { System.out.println("Razyedinenie ne udalos, ili Oshibka pri SQL zaprosa"); } } } } To check if the connection to the database was successful, I tried and the output received an error that "Connection to the database failed"
Here is my stacktrace:
org.postgresql.util.PSQLException: ОШИБКА: отношение "users" не существует Позиция: 15 at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:559) at org.postgresql.jdbc2.AнbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302) at com.force.samples.entity.LGMAnaliz.main(LGMAnaliz.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)