JDBC在#34;或附近的语法不正确。"的executeQuery

时间:2016-04-06 13:55:42

标签: java jdbc executequery

这当然是更大代码的一部分。它会编译没有问题,但是当我调用这个方法时,我得到了错误

  

"#34;附近或之后的语法错误。""在stmt.executeQuery(SQL)的位置。

我真的很感激帮助!

private void Component() {
    try {
        Statement stmt = con.createStatement();
        String SQL = "SELECT component.*, stock.amount_of_component, component.price component.component_type "
                    + "FROM component JOIN stock "
                    + "ON component.id = stock.component_id "
                    + "ORDER BY component.component_type";
        ResultSet rs = stmt.executeQuery(SQL);

        rs.next();
        int id = rs.getInt("ID");
        int amount_of_component = rs.getInt("Amount");
        String name = rs.getString("Name");
        double price = rs.getDouble("Price");
        String component_type = rs.getString("Type");


       System.out.println(" " + id + amount_of_component + " " + name + " " + price + " " + component_type);

    } catch (SQLException err)
    {
        System.out.println(err.getMessage());
    }
}

2 个答案:

答案 0 :(得分:1)

错误,在component.price和component.component_type:

之间的查询中缺少逗号
SELECT component.*, stock.amount_of_component, component.price, component.component_type 
FROM component JOIN stock 
ON component.id = stock.component_id 
ORDER BY component.component_type

编辑:要读取整个结果集,请将此循环设置为rs.next()

while(result.next()) {
    int id = rs.getInt("ID");
    int amount_of_component = rs.getInt("Amount");
    String name = rs.getString("Name");
    double price = rs.getDouble("Price");
    String component_type = rs.getString("Type");
    System.out.println(" " + id + amount_of_component + " " + name + " " + price + " " + component_type);
}

编辑2:要打印标题,您必须先在System.out.println(" id amount_of_component name price component_type ");之前手动执行此操作。

答案 1 :(得分:0)

你错过了' component.price'之间的逗号。和' component.component_type'