-1

这当然是更大代码的一部分。它可以毫无问题地编译,但是当我调用此方法时出现错误

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());
    }
}
4

2 回答 2

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);
}

Edit2:要打印标题,您必须在一段System.out.println(" id amount_of_component name price component_type ");时间之前手动执行此操作。

于 2016-04-06T13:58:04.133 回答
0

您错过了“component.price”和“component.component_type”之间的逗号

于 2016-04-06T14:03:30.153 回答