Mysql / jsp更新查询

时间:2014-03-16 16:20:32

标签: mysql jsp netbeans sql-update

我想要做的是获取字段参数并运行mysql查询来从字段数量更新(减量)值,任何人都可以告诉我该怎么做?

<form method="post">

<table class="bordered">
    <thead>

    <tr>
        <th>No</th>        
        <th>Drug Name</th>
        <th>Strength</th>
        <th>Quantity</th>
        <th>Dis Qty</th>            
    </tr>
    </thead>

     <% try {

String query="select * from drugs";

Statement stmt=conn.createStatement(); 
ResultSet rs=stmt.executeQuery(query);  
while(rs.next()) {
%>    

<tr>     
<td><%=rs.getInt("drugid") %></td>
<td><%=rs.getString("drugname") %></td>     
<td><%=rs.getString("strength") %></td>    
<td><%=rs.getInt("drugquant") %></td>    
<td><INPUT TYPE="TEXT" NAME="getint" SIZE="7">
<INPUT TYPE="SUBMIT" VALUE="Dispense" NAME="B1"></td>    </tr>



        <%


} %> </table>
    <%
    rs.close();
    stmt.close();
    conn.close();
    } catch(Exception e) {
    e.printStackTrace();
    }


![enter image description here][1]

%>

</form>

我尝试使用与此类似的代码,但它对我不起作用:(

<%
String getint = request.getParameter("getint");
String getsize = "";
// check if Update button is clicked
if(request.getParameter("B1") != null) {
    // get what user enterd in intbox
    getint = request.getParameter("getint");
    // DEBUG
    PreparedStatement pstmt4;
    pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=drugquant - "+getint+" WHERE drugid=1");

    pstmt4.executeUpdate();
}

%>

1 个答案:

答案 0 :(得分:0)

你应该添加和更改以下一行:

pstmt4 = conn.prepareStatement("UPDATE drugs SET drugquant=?"+"WHERE drugid=?");
INT drugquant = request.getParameter("drugquant"); 
INT drugid = request.getParameter("drugid"); 
pstmt4.setString(1, drugquant);
pstmt4.setString(2, drugid);
pstmt4.executeUpdate();

如果您有任何疑问,请遵循此link并给我发表评论。