java sql语法错误选择

时间:2012-05-19 17:44:57

标签: java sql select

所以我有这个代码应该为我提供行的id。

public int getIdSala(Connection conn, String t) {
    try {
        String query = "Select id_sala from sala where nume = ?";

        PreparedStatement st = conn.prepareStatement(query);
        st.setString(1, t);
        ResultSet rs = st.executeQuery(query);

        id = rs.getInt("Id_sala");

    } catch (Exception e) {
        System.err.println("Got an exception!");
        System.err.println(e.getMessage());
    }
    return id;
}

我收到以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQLserver version for the right syntax to use near '?' at line 1

可能是什么问题?

3 个答案:

答案 0 :(得分:4)

不要st.executeQuery(query),而只是st.executeQuery()

答案 1 :(得分:1)

不要两次传递查询。

   ResultSet rs = st.executeQuery(); 

答案 2 :(得分:0)

只需使用st.executeQuery();代替st.executeQuery(query)

这里是example