0

所以我有这段代码,它应该为我提供一行的 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

可能是什么问题呢?

4

3 回答 3

4

不做st.executeQuery(query),只是st.executeQuery()

于 2012-05-19T17:47:49.377 回答
1

不要两次传递查询。

   ResultSet rs = st.executeQuery(); 
于 2012-05-19T17:48:29.693 回答
0

只需使用st.executeQuery();而不是st.executeQuery(query)

这是示例

于 2012-05-19T17:52:01.860 回答