快速Java SQL问题

时间:2011-03-30 21:24:25

标签: java sql

我的代码:

String sql = "SELECT Publisher.Name, Book.Title, ShopOrder.OrderDate, SUM(OrderLine.Quantity) AS No_Books, "
            + "SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Price"
            + "FROM Publisher, Book, OrderLine, ShopOrder"
            + "WHERE OrderLine.BookID = Book.BookID AND ShopOrder.ShopOrderID = OrderLine.ShopOrderID AND Publisher.PublisherID = Book.PublisherID AND Publisher.PublisherID = " + id
            + "GROUP BY book.title, publisher.name, ShopOrder.OrderDate"
            + "ORDER BY ShopOrder.OrderDate, Book.Title";

结果错误:

  

位于char位置166(就在FROM子句之后)的“Publisher”处或附近的语法错误

3 个答案:

答案 0 :(得分:5)

缺少Theres空间

你的字符串是 ... S Total_PriceFROM Publisher,Book,OrderLine,ShopOrderWHERE O ...

您应该使用:

String sql = "SELECT Publisher.Name, Book.Title, ShopOrder.OrderDate, SUM(OrderLine.Quantity) AS No_Books, "
        + " SUM(OrderLine.UnitSellingPrice * Orderline.Quantity) AS Total_Price"
        + " FROM Publisher, Book, OrderLine, ShopOrder"
        + " WHERE OrderLine.BookID = Book.BookID AND ShopOrder.ShopOrderID = OrderLine.ShopOrderID AND Publisher.PublisherID = Book.PublisherID AND Publisher.PublisherID = " + id
        + " GROUP BY book.title, publisher.name, ShopOrder.OrderDate"
            + " ORDER BY ShopOrder.OrderDate, Book.Title";

答案 1 :(得分:0)

当您连接这些字符串时,Total_Price和FROM之间没有空格。 而在其他方面同样如此。我总是以一个空格结束并启动带引号的SQL片段。

答案 2 :(得分:0)

您需要在行尾或开头处使用空格。结果字符串如下所示:...AS Total_PriceFROM Publisher...