Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从JSP站点选择条目,使用SQL和JBDC从数据库中删除条目_Java_Jsp_Jdbc - Fatal编程技术网

Java 从JSP站点选择条目,使用SQL和JBDC从数据库中删除条目

Java 从JSP站点选择条目,使用SQL和JBDC从数据库中删除条目,java,jsp,jdbc,Java,Jsp,Jdbc,我需要一些帮助来创建一个删除SQL数据库中的条目/行的方法。为此,我使用JBDC、JSP和java。我已经有了一个与我的JSP站点一起工作的add方法。问题是,我不确定是否需要创建一个对象来实现这一点。以下是迄今为止我的代码JSP removeMatch.JSP: <div class="content"> <% String databaseId = request.getParameter("id"); String mat

我需要一些帮助来创建一个删除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>

我可以得到正确的数据库ID,但是我的测试会打印到控制台,继续打印数据库ID为1的行的值。可能有一种方法可以传递正确的值,但我想不出来。另一个问题是removeMatch不会删除匹配项,事实上,它不会对数据库进行任何更改。

首先,您的删除sql看起来是错误的:

sql = "DELETE FROM matches WHERE ID = ?) " // Why is there a closing parenthesis?
          + "VALUES (?)";      // Why are you using VALUES in a DELETE?
我会用

sql = "DELETE FROM matches WHERE ID = ? ";
这将消除对以下线路的需要:

if (match.isInDatabase()) {
    prep.setLong(2, match.getDatabaseId());
}
最后,为了解决不更改数据库的问题,我将用以下命令结束您的try块:

con.commit();

如果已将“自动提交”设置为true,则可能不需要这样做。我不是要批评你,但这似乎是你第一次编写JDBC代码。如果是这样的话,我强烈建议你通过。它为您提供了JDBC原则的所有基本解释。

在JSP中使用Scriptlet是一种糟糕的做法。您可以考虑像JSTL这样的选择,为什么不使用MVC模式呢?看,我知道这被认为是不好的做法,但这是一项学校作业:/将来,如果你在学校作业上寻求帮助,你应该在作业上贴上标签。
con.commit();