查询SQL * Plus而不是jdbc程序

时间:2014-05-16 16:35:33

标签: java sql jdbc exception-handling oracle11g

我在sqlplus(Oracle 11g)上执行了一个查询

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

我得到了这个输出:

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

但是当我对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();

我得到了这个例外:

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

现在为什么会这样?如果sqlplus正在执行这个没有任何问题,那么为什么会在程序中抛出异常?

1 个答案:

答案 0 :(得分:3)

在您连接的行的开头放置空格以在Java中创建查询,否则您连接的行将一起运行查询:

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

何时需要

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