简体   繁体   English

查询适用于SQL * Plus,但不适用于jdbc程序

[英]Query working on SQL*Plus but not in jdbc program

I executed a query on sqlplus (Oracle 11g) 我在sqlplus(Oracle 11g)上执行了查询

select Bookdetails.TITLE,Bookdetails.PRICE
from bookdetails
inner join orderdetails
ON BOOKDETAILS.BOOKID=ORDERDETAILS.BOOKID
Where Orderdetails.username ='Divya.grg';

I got this output: 我得到以下输出:

TITLE                                                             PRICE
------------------------------------------------------------ ----------
mastering c++                                                     876.2
construction material reference book 2nd edition                    332
let us c++                                                          793

But when I did the same query on a JDBC program: 但是,当我在JDBC程序上执行相同的查询时:

PreparedStatement ps=conn.prepareStatement("select Bookdetails.TITLE,Bookdetails.PRICE"
  +"from bookdetails"
  +"inner join orderdetails"
  +"ON BOOKDETAILS.BOOKID=ORDERDETAILS.BOOKID"
  +"Where Orderdetails.username ='"+username+"'");
ResultSet rs=ps.executeQuery();

I am getting this exception: 我收到此异常:

>java.sql.SQLSyntaxErrorException: ORA-00923: FROM keyword not found where expected

Now why would this happen? 现在为什么会发生这种情况? If sqlplus is executing this without any problem then why would the exception be thrown in program? 如果sqlplus执行此操作没有任何问题,那么为什么在程序中引发异常?

Place spaces at the beginnings of your lines you're concatenating to create your query in Java, or else the lines you're concatenating will run the query together as: 在要连接的行的开头放置空格,以使用Java创建查询,否则,要连接的行将按以下方式一起运行查询:

select Bookdetails.TITLE,Bookdetails.PRICEfrom bookdetailsinner join orderdetails...

when you want 当你想要的时候

select Bookdetails.TITLE,Bookdetails.PRICE from bookdetails inner join orderdetails...

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

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