我需要從數據庫填充選擇框。與數據庫的連接是成功的,我可以檢索並插入到數據庫。我嘗試了下面的代碼填充選擇框。但它顯示錯誤「迭代器不能被解析爲類型「。從數據庫retreiving 的Java代碼,從mysqldatabase填充選擇框
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>
我想你可以從這裏http://stackoverflow.com/questions搭車/ 8643096/JSP-AJAX填入-下拉列表基於 - 上的選擇的值 – OmniPotens 2013-03-16 06:10:12