2-parameter PreparedStatement is throwing syntax error, SQL State 42601, near second argument

huangapple go评论72阅读模式
英文:

2-parameter PreparedStatement is throwing syntax error, SQL State 42601, near second argument

问题

我正在创建一个简单的Java应用程序它使用一个我已经创建好的PostgreSQL数据库数据库已经创建好了但应用程序还没有)。
其中一个TABLE被命名为'Employee'。
它有一个名为'eid'的属性表示员工编号

我正尝试使用PreparedStatement来实现数据库的通用SELECT查询我希望有两个参数是通用的我还想获取结果的长度因此我创建了第二个PreparedStatement来查找结果长度

代码看起来很简单但我一直在收到相同的语法错误

以下是我试图运行的代码

    public static String[] select(Connection conn, String arguments) {

        try {

            PreparedStatement preparedSelect = conn.prepareStatement("SELECT ? FROM ?");
            PreparedStatement preparedCount = conn.prepareStatement("SELECT COUNT(*) FROM ?");

            String[] arrOfStr = arguments.split(" ", 0);

            System.out.println(arrOfStr[0].equals("eid"));
            System.out.println(arrOfStr[1].equals("Employee"));

            preparedSelect.setString(1, arrOfStr[0]);
            preparedSelect.setString(2, arrOfStr[1]);
            preparedCount.setString(1, arrOfStr[1]);

            ResultSet rsSelect = preparedSelect.executeQuery();
            ResultSet rsCount = preparedCount.executeQuery();

            ...

在第一行的参数中,我希望SELECT查询是:"SELECT eid FROM Employee"。
在第二行的参数中,我希望SELECT查询是:"SELECT COUNT(*) FROM Employee"。

使用此应用程序的用户会在程序的早期输入位于"select"函数参数中的"arguments"参数。"arrOfStr"字符串数组将包含这两个字符串:"eid"和"Employee"。

为了检查"arrOfStr"是否具有正确的值,我打印了通过将结果与我想要的字符串(即"eid"和"Employee")进行比较的.equals()函数产生的布尔值。

在控制台中,结果是(并不令人惊讶):

true
true

因此,在接下来的3行中,我将字符串值"eid"和"Employee"设置到了PreparedStatement中。

最后,我在接下来的2行中执行了这些查询。这两个查询执行(我尝试过交换它们的顺序)都会给我一个非常相似的错误。对于给定的代码,我得到以下错误:

SQL State: 42601
ERROR: syntax error at or near "$2"
  Position: 16

这表示字符串"Employee"的语法存在问题。

当我直接转到我的PostgreSQL数据库,并输入:

SELECT eid FROM Employee;

我得到的输出是:

1663
1983
1357
...

有人能解释一下这个语法错误吗?


<details>
<summary>英文:</summary>

I am creating a simple java application that uses a postgresql database which I have already created (the database has been created, not the application).
One of the TABLEs is called &#39;Employee&#39;.
It has an attribute called &#39;eid&#39;, short for employee id.

I am trying to use a PreparedStatement to generalize a database SELECT query. I want there to be two parameters which are generalized. I would also like to get the length of the result, so I created a second PreparedStatement to find that.

The code seems quite simple, but I keep getting the same syntax error.

Here is the code I am trying to run:

    	public static String[] select(Connection conn, String arguments) {
		
		    try {
			
			    PreparedStatement preparedSelect = conn.prepareStatement(&quot;SELECT ? FROM ?&quot;);
			    PreparedStatement preparedCount = conn.prepareStatement(&quot;SELECT COUNT(*) FROM ?&quot;);
			
			    String[] arrOfStr = arguments.split(&quot; &quot;, 0);
			
			    System.out.println(arrOfStr[0].equals(&quot;eid&quot;));
			    System.out.println(arrOfStr[1].equals(&quot;Employee&quot;));
			
			    preparedSelect.setString(1, arrOfStr[0]);
			    preparedSelect.setString(2, arrOfStr[1]);
			    preparedCount.setString(1, arrOfStr[1]);
			
			    ResultSet rsSelect = preparedSelect.executeQuery();
			    ResultSet rsCount = preparedCount.executeQuery();

                ...

In the first line&#39;s argument, I would like the SELECT query to be: &quot;SELECT eid FROM Employee&quot;.
In the second line&#39;s argument, I would like the SELECT query to be: &quot;SELECT COUNT(*) FROM Employee&quot;.

The user who uses this application enters (earlier in the program) the &quot;arguments&quot; argument found in the arguments of the &quot;select&quot; function. The &quot;arrOfStr&quot; string array will contain these two strings, &quot;eid&quot;, and &quot;Employee&quot;. 

To check that &quot;arrOfStr&quot; has the correct values, I printed the booleans resulting from the .equals() function comparing the values to the strings I want which, again, are &quot;eid&quot;, and &quot;Employee&quot;.

In the console the results are, not to my surprise:

    true
    true

Thus, in the next 3 lines, I set the String values &quot;eid&quot; and &quot;Employee&quot; into the PreparedStatements.

Finally, I execute the queries in the following 2 lines. Both of these query executions (I have tried switching them around) give me a very similar error. For the given code I get the error:

    SQL State: 42601
    ERROR: syntax error at or near &quot;$2&quot;
      Position: 16

This is stating that there is an issue with the syntax of the string &quot;Employee&quot;.

When I go directly to my postgresql database, and input:

    SELECT eid FROM Employee;

I get the output:

    1663
    1983
    1357
    ...

Could someone explain this syntax error?

</details>


# 答案1
**得分**: 1

如Elliott所提到的,你不能以这种方式绑定表名。

要实现你的目标,请尝试以下方法:

```java
String[] arrOfStr = arguments.split(" ", 0);

PreparedStatement preparedSelect = conn.prepareStatement("SELECT ? FROM " + arrOfStr[1]);
PreparedStatement preparedCount = conn.prepareStatement("SELECT COUNT(*) FROM " + arrOfStr[1]);

请注意,这是一段Java代码,用于执行SQL查询,不要进行翻译。

英文:

As mentioned by Elliott, you cannot bind the table name that way.

To achieve your goal, try this:

String[] arrOfStr = arguments.split(&quot; &quot;, 0);
    
PreparedStatement preparedSelect = conn.prepareStatement(&quot;SELECT ? FROM &quot; + arrOfStr[1]);
PreparedStatement preparedCount = conn.prepareStatement(&quot;SELECT COUNT(*) FROM &quot; + arrOfStr[1]);

huangapple
  • 本文由 发表于 2020年4月10日 11:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定