简体   繁体   English

查询在DB上运行但在java中引发异常

[英]Query runs on DB but throws exception in java

I'm having some trouble executing a nested sql query. 我在执行嵌套的sql查询时遇到了一些麻烦。 It works fine when i run it on TOAD (for Oracle DB) but when I try and execute the same query from my java program it gives me an error : ORA-00923: FROM keyword not found where expected: Here is the query: 它在TOAD(对于Oracle DB)上运行时工作正常,但是当我尝试从我的java程序执行相同的查询时,它给出了一个错误:ORA-00923:找不到FROM关键字:这是查询:

Select *
from(select tb1.Number, tb1.Desc, tb1.Local, tb2.Division
     from tb1
     inner join tb2
     on tb1.Number = tb2.Number
     and Local in ('ENG_USA', 'ENG_CHINA', 'FR_FRENCH'))
where Number in ('7512','4706')

Like I mentioned, it executes fine on Toad and is able to retrieve data but the same query throws the ORA-00923 exception. 就像我提到的,它在Toad上执行正常,并且能够检索数据,但是相同的查询会抛出ORA-00923异常。

rset = stmt.executeQuery(Select *
from(select tb1.Number, tb1.Desc, tb1.Local, tb2.Division
from tb1
inner join tb2
on tb1.Number = tb2.Number
and Local in ("+loc+"))
where Number in ("+s+")

Reply With Quote 引用回复

I had a similar issue with mysql. 我和mysql有类似的问题。 My problem was that i splitted the query in multiple rows without leaving a space between 2 rows. 我的问题是我在多行中分割查询而不在两行之间留下空格。 So your query should be: 所以你的查询应该是:

rset = stmt.executeQuery("Select * from(select tb1.Number, tb1.Desc, tb1.Local, tb2.Division from tb1 inner join tb2 on tb1.Number = tb2.Number and Local in ("+loc+")) where Number in ("+s+")"

PS: I would like to write this as a comment but for some reason i can't in this question. PS:我想把它写成评论,但出于某种原因,我不能在这个问题上。 Sorry! 抱歉!

First off, I don't see the beginning or ending double-quotes in your Java code. 首先,我没有在Java代码中看到开头或结尾的双引号。 Since your Java code must compile in order to get far enough to receive an Oracle error, I'm assuming that there is a double quote prior to the SELECT , a double-quote after the final close paren, and then another close parent to match to the open paren on the executeQuery . 由于你的Java代码必须编译才能足够接收Oracle错误,我假设在SELECT之前有一个双引号,在最后的close paren之后有一个双引号,然后是另一个匹配的父代匹配在executeQuery上打开paren。 So I'm assuming that your actual code is something like 所以我假设你的实际代码是这样的

rset = stmt.executeQuery(
        "Select *
           from(select tb1.Number, tb1.Desc, tb1.Local, tb2.Division
                  from tb1
                       inner join tb2
                          on tb1.Number = tb2.Number
                             and Local in ("+loc+"))
                 where Number in ("+s+")");

Is there a reason that you're building the SQL statement dynamically rather than using bind variables? 您是否有动态构建SQL语句而不是使用绑定变量? Using bind variables is more secure since you don't need to worry about SQL injection attacks. 使用绑定变量更安全,因为您不必担心SQL注入攻击。 It is more efficient since you're not forcing the database to hard parse the query every time. 它更高效,因为您不是每次都强制数据库硬解析查询。 And it saves you from many sorts of errors around quoting and escaping. 它可以避免引发和转义中出现的各种错误。 In your case, assuming that the local variable loc does not have the leading and trailing single quotes, you'd need to include those in your query 在您的情况下,假设局部变量loc没有前导和尾随单引号,您需要在查询中包含那些

rset = stmt.executeQuery(
        "Select *
           from(select tb1.Number, tb1.Desc, tb1.Local, tb2.Division
                  from tb1
                       inner join tb2
                          on tb1.Number = tb2.Number
                             and Local in ('"+loc+"'))
                 where Number in ("+s+")");

Had you used bind variables and just bound loc to the first bind variable, on the other hand, you wouldn't need to worry about adding the single quotes (or escaping any single quotes in loc . 如果您使用绑定变量并将loc绑定到第一个绑定变量,另一方面,您不必担心添加单引号(或转义loc任何单引号)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM