gpt4 book ai didi

java - 带有 MYSQL 的 JSP 中的 NoSuchElementException

转载 作者:行者123 更新时间:2023-11-29 02:25:32 27 4
gpt4 key购买 nike

我不断收到以下异常:org.apache.jasper.JasperException: java.util.NoSuchElementException

你能帮我处理我的代码吗,因为我显然遗漏了一些东西。非常感谢!

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);
}
}
}

最佳答案

问题出在这里:

 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>");
}

关于java - 带有 MYSQL 的 JSP 中的 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847880/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com