是否有可能通过jsp和Servlet从数据库中拉出选定的行?

时间:2018-08-07 06:45:24

标签: jsp servlets java-ee

我正在使用以下查询提取所选数据

String fetch_head = "select * from head_category where id = (?)";

但是我不知道如何同时使用ResultSet和使用PreparedStatement提供参数。

public ArrayList fetch_selected() {
        ArrayList arrayList = new ArrayList();
        try {
            connectionFactory cf = new connectionFactory();
            con = cf.getcon();
            st = con.createStatement();
            rs = st.executeQuery(fetch_head);

            while (rs.next()) {
                // it only fetches all              
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }

现在我应该在哪里放置PreparedStatement ...?

1 个答案:

答案 0 :(得分:0)

st未在您的代码中声明,应该为PreparedStatement

PreparedStatement  st = con.createStatement();

请注意,您必须在代码中release all resources as PreparedStatement