请帮我解决同样的问题:我不知道如何通过在mysql中自动生成的id从表中删除数据,我认为这个问题可以在JBDC中使用GENERATED_KEY来解决,总而言之我不是知道,帮我提供如何按表ID删除的信息。
表'注意':
CREATE TABLE `note` (
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`noteName` VARCHAR(100) NOT NULL ,
`note` VARCHAR(1000) NOT NULL ,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
现在我正在获得价值 - ' noteName'但它是不正确的我需要为表中的每一行获取id
Statement st = con.createStatement();
ResultSet rs;
ResultSet resultSet;
rs = st.executeQuery("SELECT * FROM note");
while (rs.next()) {
%>
<tr>
<td><input type="checkbox"></td>
<td id="noteName"><%=rs.getString(1) %></td>
<td><%=rs.getString(2) %></td>
<td>time</td>
<td id="close">
<a href=deleteData?id=<%=rs.getString(1) %>><button onclick="deleteRow()" type="button" class="close" data-dismiss="modal">×</button></a>
</td>
</tr>
<% }
rs.close();
st.close();
con.close();
%>
我在这里删除数据:
public class deleteData extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int value = Integer.parseInt(request.getParameter("id"));
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/evernoteDB",
"evernoteDB", "0633739768z");
String query = "DELETE FROM note WHERE noteName = ?";
PreparedStatement statement = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
statement.setInt(1, value);
statement.executeUpdate();
response.sendRedirect("/userNotes.jsp");
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
您将能够从表中获取所有数据。
<tr>
<td><%= rs.getInt("id") %></td> returns id
<td><%= rs.getString("notename") %></td> returns noteName
<td><%= rs.getString("note") %></td> returns note
</tr>
并改变
<a href=deleteData?id=<%=rs.getString(1) %>>...</a>
到
<a href=deleteData?id=<%= rs.getInt("id") %>>...</a>
并在servlet中将查询更改为
"DELETE FROM note WHERE id = ?"