【问题标题】:How to get row id on button click?如何在按钮单击时获取行 ID?
【发布时间】:2019-10-09 08:39:50
【问题描述】:

我正在尝试使用 jsp 制作购物车。我需要从一行中获取 id 以使用该 id 添加另一个表。我创建了表格并放置了按钮。我应该创建什么样的方法或者我应该做什么?谢谢...

我想运行这个 sql 命令来更新购物车:“insert into users.cart (Name, Price) select Name, Price from users.product where id = (...);

这是我的 jsp 文件的一部分,我需要使用该按钮来获取 id

<tr>

</tr>
<tr>
<td><b>ProductName</b></td>
<td><b>Description</b></td>
<td><b>Price</b></td>
</tr>
<%

try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","");
    Statement st = con.createStatement(); 
    ResultSet rs =st.executeQuery("SELECT * FROM users.product"); 

    out.println("<h1 align = \"center\">Start Shopping:</h1><br>");

    while(rs.next()){
%>
<tr>

<td><%=rs.getString("Name") %></td>
<td><%=rs.getString("Description") %></td>
<td><%=rs.getInt("Price") %></td>
<td><input type = "button" value = "Add to cart"></td>

</tr>

<% 
}

} catch (Exception e) {
e.printStackTrace();
}
%>
</table>

【问题讨论】:

    标签: java html mysql jsp


    【解决方案1】:

    您可以使用&lt;a&gt; 标签将id 传递到您的jsp 页面,您对insert 的查询如下所示:

     <td>
     <!-- here you are passing your id=somevalue to yourjsp page-->
       <a href="yourjsp.page?id=<%=rs.getString("id") %>">
        <input type = "button" value = "Add to cart">
       </a>
     </td> 
    

    要超过id,您可以执行以下操作:

       String id=request.getParameter("id");//getting id passed in url also you can convert this id to int
    

    在您的查询中传递id,然后将redirect 传递到您的jsp 页面。

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多