2014-04-03 74 views
2

我不斷收到以下異常:org.apache.jasper.JasperException:java.util.NoSuchElementExceptionNoSuchElementException異常在JSP與MYSQL

你能幫我與我的代碼,因爲我清楚地失去了一些東西。提前致謝!

的JSP文件:

<% 
    Enumeration names = request.getParameterNames(); 
    while (names.hasMoreElements()) { 
    String name = (String) names.nextElement(); 
    StringBuffer sb = new StringBuffer(name); 
    sb.deleteCharAt(0); 
    VoCo.Vogel.Delete(sb.toString()); 
} 

%> 
<br> 

<div class="navigator"> 
<a href="index.jsp">Add</a> 
<a id="currenttab" href="waarnemingen.jsp">Delete</a> 
</div> 

<br> <br> <br> 

<form action="waarnemingen.jsp" method="post"> 
<table> 
<tr> 
<th>Datum</th> 
<th>Tijd</th> 
<th>Plaats</th> 
<th>Spotternaam</th> 
<th>Vogelsoort</th> 
</tr> 
<% 

List list = VoCo.Vogel.GetWaarnemingen(); 
int id = 0; 
String box = null; 

Iterator<String> it = list.iterator(); 

while (it.hasNext()) { 
    id = Integer.parseInt(it.next()); 
    out.print("<tr>"); 
    for (int i = 0; i < 5; i++) { 
     out.print("<td>"); 
     out.print(it.next()); 
     out.print("</td>"); 
} 
out.print("<td>"); 
box = "<input name=r" + id + " type='checkbox'>"; 
out.print(box); 
out.print("</td>"); 
out.print("</tr>"); 
} 
%> 

</table> 

<br> 
<input type="submit" value="Delete"> 

</form> 

Java文件:

public class Vogel { 
static final String url = "jdbc:mysql://localhost:3306/turving"; 

public static void Insert(String datum, String tijd, String plaats, String spotternaam, String vogelsoort) { 
    try { 

     String insert = "INSERT INTO turving(datum, tijd, plaats, spotternaam, vogelsoort)" + "VALUES (?, ?, ?, ?, ?)"; 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection(url, "root", ""); 

     PreparedStatement ps = con.prepareStatement(insert); 

     ps.setString(1, datum); 
     ps.setString(2, tijd); 
     ps.setString(3, plaats); 
     ps.setString(4, spotternaam); 
     ps.setString(5, vogelsoort); 
     ps.executeUpdate(); 
     con.close(); 

    } catch (Exception ex) { 
     Logger.getLogger(Vogel.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

public static List GetWaarnemingen() { 

    List<String> list = new ArrayList<String>(); 

    try { 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection(url, "root", ""); 

     Statement stmt = con.createStatement(); 

     ResultSet result = stmt.executeQuery("SELECT * FROM turving"); 

     while(result.next()) 
     { 
     list.add(result.getString("id")); 
     list.add(result.getString("datum")); 
     list.add(result.getString("tijd")); 
     list.add(result.getString("plaats")); 
     list.add(result.getString("spotternaam")); 
     list.add(result.getString("vogelsoort")); 
     } 

     con.close(); 

    } catch (Exception ex) { 
     Logger.getLogger(Vogel.class.getName()).log(Level.SEVERE, null, ex); 
    } 
     return list; 
    } 

public static void Delete(String id) { 
    try { 
     String delete = "DELETE from turving WHERE id = ?"; 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con = DriverManager.getConnection(url, "root", ""); 
     PreparedStatement ps = con.prepareStatement(delete); 

     ps.setString(1, id); 
     ps.executeUpdate(); 
     con.close(); 

    } catch (Exception ex) { 
     Logger.getLogger(Vogel.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 
} 
+0

你應該張貼堆棧跟蹤您的例外 – Drunix

回答

2

的問題是在這裏:

Iterator<String> it = list.iterator(); 

while (it.hasNext()) { 
    id = Integer.parseInt(it.next()); 
    out.print("<tr>"); 
    for (int i = 0; i < 5; i++) { 
     out.print("<td>"); 
     out.print(it.next()); 
     out.print("</td>"); 
} 

你打電話it.next()太多次了。第一次調用應保存在一個變量中,以便在子循環中不再調用它。

應該是這樣:

Iterator<String> it = list.iterator(); 

while (it.hasNext()) { 
    String item = (String)it.next(); 
    id = Integer.parseInt(item); 
    out.print("<tr>"); 
    for (int i = 0; i < 5; i++) { 
     out.print("<td>"); 
     out.print(item); 
     out.print("</td>"); 
} 
相關問題