-1

This is of course parts of a larger code. It will compile with no problem, but when I call this method I get the error

"syntax error near or at "."" at the position of stmt.executeQuery(SQL).

I would really appreciate the help!

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());
    }
}
mnille
  • 1,328
  • 4
  • 16
  • 20
gheithen
  • 11
  • 4

2 Answers2

1

Typo, missing a comma in the query between component.price and 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

Edit: To read the whole result set, put this cycle instead of 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: To print the header, you have to do it manually by putting a System.out.println(" id amount_of_component name price component_type "); before the while.

Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
  • thank you!! now another problem has come up! I only get one row of my table printed and there is no header? – gheithen Apr 06 '16 at 14:06
0

You missed a comma between 'component.price' and 'component.component_type'