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) 
  • I have installed Intellij Idea 13.0. And this simpl project for SpringMVC JPA Hibernate. I set up the tomkat and postgres on the existing version in the computer. And the java compiler and hibernate selected the same in the same way. Explain polesta because of any errors such an error can be? - Tal'at
  • Show stacktrace with errors. Without it, nothing definite can be said. - Nofate
  • I checked in the dibag after the query is executed (res = prsment.executeQuery ();) goes to the catch exception (SQLException e) {System.out.println ("Connection to the database failed"); } - Tal'at
  • one
    And the table with that name is in your devdb database. Feeling that either the wrong server or the wrong base is selected - Mike
  • one
    Yes, everything coped, just called the name with small letters and it turned out, and now I knew that the name of the tables should be like this - Talat

1 answer 1

Try specifying the schema name in the JDBC connect string:

 jdbc:postgresql://localhost:5432/devdb?currentSchema=mySchema 

where mySchema replace with the name of the schema in which the users table is located. The currentSchema should be used in Postgresql versions starting from version 9.4, and for earlier versions use the searchpath:

 jdbc:postgresql://localhost:5432/devdb?searchpath=mySchema 
  • Or in the request write instead of just users, users with the scheme: SELECT * FROM mySchema.users - Sergey