2012-04-30 15 views
-2

私のSQLデータベース内のエントリ/行を削除する方法の作成には助けが必要です。私はこれにJBDC、JSP、javaを使用します。私は既にJSPサイトで動作するaddメソッドを持っています。問題は、私がこの作業をするためにオブジェクトを作成する必要があるかどうかわからないことです。ここに私のコードJSPは、これまでのところ(removeMatch.jsp)です:(JSPサイトからエントリを選択し、SQLとJBDCを使用してデータベースからエントリを削除します

<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ファイルからの方法は、データベース内の行を削除しますMatchMapper.java @ removeMatch()):

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」を持つ行の値を印刷し続けています。おそらく、正しい値を渡す方法がありますが、私はそれを理解できません。もう1つの問題は、removeMatchがマッチを削除しないということです。実際には、データベースに何も変更を加えません。

+3

JSPでスクリプトレットを持つのは悪いことです。あなたはJSTLのような選択肢を検討するかもしれません。 – kosa

+0

MVCパターンを使用してみませんか? http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller –

+0

を参照してください。私は悪い習慣と考えていますが、これは学校の課題です:/ – decon

答えて

0

まず第一に、あなたのDELETE 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ブロックで終了します:あなたはtrueに設定する自動コミットしている場合

con.commit(); 

、これは必要ないかもしれません。私はあなたを批判するつもりはありませんが、JDBCコードを書くのは初めてのようです。その場合は、this tutorialに行くことを強くお勧めします。これは、JDBCの原則のすべての基本的な説明を提供します。