我需要从数据库中填充选择框。与数据库的连接成功,我可以检索并插入数据库。我尝试了以下代码来填充选择框。但它显示错误“迭代器无法解析为类型”。用于从数据库中检索的 Java 代码是,
package servicescheduler.pack;
import java.text.*;
import java.util.*;
import java.sql.*;
public class listObject
{
static Connection currentCon = null;
String sql="select * from center_point_map where service_center='Radiology';";
public List getlist()
{
ArrayList<String> list=new ArrayList<String>();
try
{
currentCon = ConnectionManager.getconnection();
}
catch (Exception ex)
{
System.out.println(" An Exception has occurred! " + ex);
}
if(currentCon!=null)
{
System.out.println("You made it, take control your database now!");
try
{
PreparedStatement prest = currentCon.prepareStatement(sql);
ResultSet rs = prest.executeQuery();
while(rs.next()) {
list.add(rs.getString(1));
}
System.out.println(list.get(0));
prest.close();
rs.close();
return list;
}
catch (SQLException s)
{
System.out.println("SQL statement is not executed!"+s);
}
catch (Exception e)
{
e.printStackTrace();
}
}
return list;
}
}
jsp代码是
<%@page import="servicescheduler.pack.listObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4 /loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<select>
<%
Iterator it = new listObject().getlist();
while(it.hasNext()) {
out.write("<option value=\""+ it.getFieldA()+ "\">"+ it.getFieldB() +"\">");
}
%>
</select>
</body>
</html>