Java JDBC Connection with Heroku

余生颓废 提交于 2020-01-17 06:07:28

问题


I'm trying to create a Java Web App on Heroku. When connecting the App to Heroku Database I encountered a Issue: org.postgresql.util.PSQLException: ERROR: relation "sc_user" does not exist

The connection part is:

URI dbUri = new URI(System.getenv("DATABASE_URL"));
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();

BasicDataSource connectionPool = new BasicDataSource();

if (dbUri.getUserInfo() != null) {
    connectionPool.setUsername(dbUri.getUserInfo().split(":")[0]);
    connectionPool.setPassword(dbUri.getUserInfo().split(":")[1]);
}
connectionPool.setDriverClassName("org.postgresql.Driver");
connectionPool.setUrl(dbUrl);
connectionPool.setInitialSize(3);

Connection conn = connectionPool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM SC_User");

I see on Heroku Dashboard that 3 connections are used from pool, but the last line throws exception saying no relation sc_user.

I created the table with add-on tool Adminium, is there something i'm missing in the commands? Thanks for your help!


回答1:


When you create a column in postgres with Upper Letter the DBMS use it between "SC_User" si in case your column is in upper Letter you have to use :

stmt.executeQuery("SELECT * FROM \"SC_User\""); 
//--------------------------------^--------^

else you have to use the correct name with lowercase

stmt.executeQuery("SELECT * FROM sc_user");
//---------------------------------^^

read more about that here : Cannot query Postgres database that has column names with capital letters




回答2:


Try delimiting the table name with quotations

ResultSet rs = stmt.executeQuery("SELECT * FROM \"SC_User\"");


来源:https://stackoverflow.com/questions/43922975/java-jdbc-connection-with-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!