【问题标题】:Is it possible to LIMIT query result depending on user input? [duplicate]是否可以根据用户输入限制查询结果? [复制]
【发布时间】:2019-06-28 11:28:38
【问题描述】:

这是我的索引页。

<form action="worker.jsp">
  <input type="text" name="count"></input>
</form>

这是我的 worker.jsp。

<jsp:useBean id="ff" class="my.dbController" scope="page"/>
String count = (request.getParameter("count"));
ff.queryTest(count)

这是 java dbController 文件。

String query = "Select * from table limit ?"
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, x);
ps.executeUpdate();

这可能吗?我一直在尝试这种方式,但它一直返回 0 数据。

编辑:我能够修复它,我的 .jsp 文件中有一个错误。还是谢谢

【问题讨论】:

  • 如果是查询,你应该使用executeQuery,而不是executeUpdate
  • 没关系我能够修复它,我的一个 .jsp 文件中有错误。还是谢谢

标签: java jsp


【解决方案1】:

当你应该做executeQuery时,你正在做executeUpdate

【讨论】:

    【解决方案2】:

    1)您已经准备好语句,因此 executeUpdate() 将不起作用。 2)limit参数接受int。无需将字符串传递给它。

    ps.setInt(1,limit);
    ps.executeUpdate(query);
    

    总是有一个正确的命名约定,而不是像 x,y 这样的命名变量:)

    【讨论】:

    • *ps.executeQuery(query);
    猜你喜欢
    • 2011-01-28
    • 2015-03-15
    • 2021-09-28
    • 2013-09-29
    • 2017-10-03
    • 2010-10-11
    • 2017-04-22
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多