简体   繁体   English

org.postgresql.util.PSQLException:错误:“-”处或附近的语法错误

[英]org.postgresql.util.PSQLException: ERROR: syntax error at or near “-”

I have a PostreSQL database which I am trying to access with a Java app.我有一个 PostreSQL 数据库,我正在尝试使用 Java 应用程序访问它。 When it comes to the following code:当涉及到以下代码时:

public class UserRepo {

    private String baseQuery = "SELECT * FROM bank-console.user_credentials ";


    public Optional<UserInfo> findUserByCredentials(String username, String password) {

        Optional<UserInfo> _user = Optional.empty();

        try (Connection conn = ConnectionFactory.getInstance().getConnection()){
            String sql =  baseQuery +
                    "WHERE username = ? AND password = ?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);

            ResultSet rs = pstmt.executeQuery();

            _user = mapResultSet(rs).stream().findFirst();

        } catch (SQLException sqle) {
            sqle.printStackTrace();
        }

        return _user;
    }

It throws the error message exactly as the question title.它完全按照问题标题抛出错误消息。 When I take the text of the baseQuery and the String sql and enter them in my PostgreSQL app exactly (replacing the question marks with actual values), it retrieves the table perfectly.当我获取 baseQuery 和 String sql 的文本并在我的 PostgreSQL 应用程序中准确输入它们(用实际值替换问号)时,它会完美地检索表。 Where is the syntax error here?这里的语法错误在哪里?

My pom file contains我的 pom 文件包含

<dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.12</version>
 </dependency>

In response to @Mike Organek's comment, the answer is that bank-console should not have a hyphen.针对@Mike Organek 的评论,答案是银行控制台不应该有连字符。 Using double-quotes did not work because the baseQuery is already within double quotes.使用双引号不起作用,因为 baseQuery 已经在双引号内。 Single-quotes was a syntax error in itself.单引号本身就是一个语法错误。 So what worked was to change bank-console to bankconsole.所以有效的是将 bank-console 更改为 bankconsole。 My SQL app allowed me to do that without losing the tables.我的 SQL 应用程序允许我这样做而不会丢失表。 Then just change the baseQuery accordingly.然后只需相应地更改 baseQuery。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引起:org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near ":" org.postgresql.util.PSQLException:错误:在“with”处或附近出现语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near "with" org.postgresql.util.PSQLException:错误:“:”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “:” org.postgresql.util.PSQLException:错误:语法错误在“$ 1”或附近 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1” org.postgresql.util.PSQLException:错误:“ 7”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “7” org.postgresql.util.PSQLException:错误:“)”处或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “)” org.postgresql.util.PSQLException:错误:“(”或附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error at or near “(” org.postgresql.util.PSQLException:错误:Java 中«,» 附近的语法错误 - org.postgresql.util.PSQLException: ERROR: syntax error near «,» in Java Java 中的错误:org.postgresql.util.PSQLException:错误:语法错误附近; - Error in Java: org.postgresql.util.PSQLException: ERROR: syntax error near ; date_trunc org.postgresql.util.PSQLException:错误:“ $ 1”或附近的语法错误 - date_trunc org.postgresql.util.PSQLException: ERROR: syntax error at or near “$1”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM