我需要一些帮助来创建一个删除我的 SQL 数据库中的条目/行的方法。为此,我使用 JBDC、JSP 和 java。我已经有了一个适用于我的 JSP 站点的 add 方法。问题是我不确定是否需要创建一个对象来完成这项工作。到目前为止,这是我的代码 JSP(removeMatch.jsp):
<div class="content">
<%
String databaseId = request.getParameter("id");
String matchDate = request.getParameter("matchDate");
String matchTime = request.getParameter("matchTime");
String meetingTime = request.getParameter("meetingTime");
String series = request.getParameter("series");
String opponent = request.getParameter("opponent");
String matchLocation = request.getParameter("matchLocation");
if (matchDate != null && matchTime != null && meetingTime != null && series != null && opponent != null && matchLocation != null) {
int intSeries = Integer.parseInt(series);
Match match = new Match(matchDate, matchTime, meetingTime, intSeries, opponent, matchLocation);
//cast string to long value
long longDatabaseId = Long.parseLong(databaseId);
match.setDatabaseId(longDatabaseId);
//remove the match
MatchMapper.removeMatch(match);
//Test in console, they don't print the same values that are connected to the databaseId:
System.out.println(databaseId);
System.out.println(matchDate);
System.out.println(matchTime);
System.out.println(meetingTime);
System.out.println(series);
System.out.println(opponent);
System.out.println(matchLocation);
}
%>
<h3>Vælg de kampe som du vil slette og dermed fjerne fra
databasen:</h3>
<form action="removeMatch.jsp" method="post">
<fieldset>
<table border="1">
<tr>
<!-- TABEL PASSER IKKE OVER ENS!!!! -->
<th></th>
<th>Dato</th>
<th>Modstander</th>
<th>Spilletid</th>
<th>Mødetid</th>
<th>Spillested</th>
<th>Serie</th>
</tr>
<%
ArrayList<Match> matches = MatchMapper.getAllMatches();
for (Match m : matches) {
%>
<tr>
<td><input type="checkbox" name="id" value=<%=m.getDatabaseId()%>></td>
<td><input type="hidden" name="matchDate" value=<%=m.getDate()%>><%=m.getDate()%></td>
<td><input type="hidden" name="matchTime" value=<%=m.getMatchStart()%>><%=m.getMatchStart()%></td>
<td><input type="hidden" name="meetingTime" value=<%=m.getMeetingTime()%>><%=m.getMeetingTime()%></td>
<td><input type="hidden" name="series" value=<%=m.getSeries()%>><%=m.getSeries()%></td>
<td><input type="hidden" name="opponent" value=<%=m.getOpponent()%>><%=m.getOpponent()%></td>
<td><input type="hidden" name="matchLocation" value=<%=m.getLocation()%>><%=m.getLocation()%></td>
</tr>
<%
}
%>
</table>
<a href="matchTable.jsp"><input type="submit" value="Slet Kampe" /></a>
</fieldset>
</form>
</div>
这个想法是用户检查他想要删除的行,然后另一个java文件中的一个方法删除数据库中的行(removeMatch()@MatchMapper.java):
public static void removeMatch(Match match) {
Connection con = null;
try {
con = JDBCConnectionFactory.getInstance().getNewConnection();
// creates empty string
String sql = "";
// if Match exists in database, then prepare to call
// a DELETE FROM table_name WHERE some_column=some_value statement (matchDate and opponent)
if (match.isInDatabase()) {
sql = "DELETE FROM matches WHERE ID = ?) "
+ "VALUES (?)";
}
// if Match doesn't exist in database, then prepare to make an
// return
else {
System.out.println("Kamp findes ikke i databasen");
}
PreparedStatement prep = con.prepareStatement(sql);
prep.setLong(1, match.getDatabaseId());
// if the match already exists in database, then get the id (so it
// won't create a new id)
if (match.isInDatabase()) {
prep.setLong(2, match.getDatabaseId());
}
// execute insert/update
prep.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCConnectionFactory.getInstance().closeConnection(con);
}
}
我可以获得正确的databaseId,但我的测试打印到控制台继续打印databaseId为“1”的行的值。可能有一种方法可以传递正确的值,但我无法弄清楚。另一个问题是 removeMatch 不会删除匹配项,事实上,它不会对数据库进行任何更改。