我有这个问题:我会打印一个数组。它的元素存在于数据库中。使用eclipse,我定义了这个类:
package provaprovincie;
public class Provincie {
public String nome = "";
public String sigla = "";
public Provincie (String nome, String sigla)
{
this.nome =nome;
this.sigla =sigla;
}
}
通过这种方式,我从数据库中捕获了值:
package provaprovincie;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
public class DBQuery {
public static ArrayList<Provincie> DB_Select_Provincie()
{
ArrayList<Provincie> arrpr = new ArrayList<Provincie>();
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://" + "127.0.0.1" + "/" + "test" + "?" +
"user=" + "root" + "&password=" + "");
PreparedStatement pstmt = con.prepareStatement("SELECT * FROM provincie");
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
String nome = rs.getString("nome");
String sigla = rs.getString("sigla");
Provincie p = new Provincie (nome, sigla);
arrpr.add(p);
}
con.close();
}
catch (Exception e) {
System.out.println("Errore con DB o Query errata");
e.printStackTrace();
}
return arrpr;
}
}
在页面&#34; .jsp&#34;中,我写道:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="provaprovincie.*" %>
<%@ page import="java.util.ArrayList" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList<Provincie> prov = DBQuery.DB_Select_Provincie();
for (int i = 0; i < prov.size(); i++)
{
out.print("ciao: " +prov.get(i).nome + "," +prov.get(i).sigla+".");
}
%>
</body>
</html>
通过这种方式,日食不打印任何东西......请帮助我......