当类有另一个类字段时,如何在java中列出类元素?它看起来像这样:
public class Grad {
private int grad_id;
private **Drzava drzava**;
private String naziv;
private boolean aktivan;
private Set<Korisnik> korisnici = new HashSet<Korisnik>(0);
public Drzava getDrzava() {
return drzava;
}
public void setDrzava(Drzava drzava) {
this.drzava = drzava;
}
public List<Grad> getAllGrad() {
List<Grad> gradovi = new ArrayList<Grad>();
try {
Statement statement = DBConnection.getConnection().createStatement();
ResultSet rs = statement.executeQuery("select * from grad;");
while (rs.next()) {
Grad grad = new Grad();
grad.setGrad_id(rs.getInt("grad_id"));
**grad.setDrzava( (Drzava)rs.getObject("drzava"));**/**(HERE IS A PROBLEM!!)**/
grad.setNaziv(rs.getString("naziv"));
grad.setAktivan(rs.getBoolean("aktivan"));
gradovi.add(grad);
}
else if (action.equalsIgnoreCase("listGrad")){
forward = LIST_GRAD;
request.setAttribute("gradovi", dao.getAllGrad());
这是在 jsp 页面中,我认为错误在 grad.drzava.naziv:
<c:forEach items="${gradovi}" var="grad">
<tr>
<td><c:out value="${grad.grad_id}"/></td>
<td><c:out value="${grad.drzava.naziv}"/></td>
<td><c:out value="${grad.naziv}" /></td>
<td><c:out value="${grad.aktivan}" /></td>
例外是:找不到 java sql SQLException 列“drzava”。
非常感谢您的回答:))