【问题标题】:Unable to connect to the PostgreSQL getting relation "table_name" does not exist error无法连接到 PostgreSQL 获取关系“table_name”不存在错误
【发布时间】:2016-01-24 04:12:20
【问题描述】:

以下是我的 Java JDBC 代码,我在其中尝试连接到 PostgreSQL 数据库“铁路”

import java.sql.*;

class Jdbc_2{
    public statis void main( String [] args){
       
      Connection C = null;
      Statement stmt = null;

     try{
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/railway", "test". "test123");
        c.setAutoCommit(false);
        System.out.println("Database opened!")
        stmt = c.createStatement();
        String ip1 = "SELECT * FROM railway WHERE stcode1 = " + args[0] + "and stcode2 = " + args[1];
        ResultSet rs = stmt.executeQuery(ip1);
        if(!rs.next()){
          System.out.println("stcode1 and stcode2 doesn't exits");
        }
        if(rs.next()){
           System.out.println("stcode1 and stcode2 found!")
        }
        rs.close();
        stmt.close();
     }
   }
}

我可以看到在我的 postgres 中创建的“铁路”数据库。

但上面的代码仍然给我错误:

org.postgresql.util.PSQLException:ERROR: relation "railway" does not exist

  Position: 16

还有这个职位是什么意思?

【问题讨论】:

  • 这意味着您在查询中的字符 16 处有一个表名 (railway)。并且您用于登录数据库的 id 无权访问该表。因此,您会收到您列出的错误。
  • railway 在 postgres 中是一个 db 名称,但在 java 代码中它是一个表名。
  • 您应该在此处复制/粘贴代码,而不是将其放在图像上,然后将其放置在可以删除而无需任何警告的地方。

标签: java postgresql jdbc


【解决方案1】:

SELECT * FROM x 是一个表查询。当您连接到数据库时,您连接到数据库软件中的数据库,并且该数据库具有将应用查询的表。

在不知道你的表叫什么的情况下,我无法建议它应该是什么样子,除了 x 应该换成 [schema].[tablename]

您可以通过以下方式找到数据库中的表:

\c railway  -- Connect to the database
\dt         -- List database tables

在数据库控制台中。

【讨论】:

    猜你喜欢
    • 2011-01-03
    • 2012-01-13
    • 2020-09-03
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    相关资源
    最近更新 更多