gpt4 book ai didi

java - 从mysql数据库填充选择框

转载 作者:行者123 更新时间:2023-11-29 00:33:30 26 4
gpt4 key购买 nike

我需要从数据库中填充选择框。与数据库的连接成功,我可以检索并插入到数据库中。我尝试了以下代码来填充选择框。但它显示错误“迭代器无法解析为类型”。用于从数据库中检索的 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>

最佳答案

错误是因为导入语句。你还需要在jsp中导入Iterator。默认包是java.lang。但是这里我们使用的是Iterator,它在java.util中可用.*。所以需要先导入java.util包。

<%@page import="java.util.*"%>

并使用这个

Iterator it = new listObject().getlist().iterator();

关于java - 从mysql数据库填充选择框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15446166/

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