求解答,我自己糾結(jié)兩天了
我自己跟著視頻寫的代碼。調(diào)用的是mysql自帶的數(shù)據(jù)庫(kù)world。
這個(gè)是CityDAo里面的
public ArrayList<City>getAllCities(){
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
ArrayList<City> list=new ArrayList<City>();//城市集合
try{
conn=DBHelper.getConnection();
String sql="select *from city where Name='Shanghai';"; ? //SQL
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
//Statement stmt = conn.createStatement(); ?
//ResultSet rs = stmt.executeQuery(sql); ?
while(rs.next()){
City city=new City();
city.setId(rs.getInt("ID"));
city.setName(rs.getString("Name"));
city.setCountryCode(rs.getString("CountryCode"));
city.setDistrict(rs.getString("District"));
city.setPopulation(rs.getInt("Population"));
list.add(city); ?//每次遍歷加一個(gè)城市
}
return list; ? //返回
index.jsp文件
? <h1>數(shù)據(jù)庫(kù)city展示</h1>
? <hr>
<center>
<table>
<thead>
<tr><th>ID</th><th>名字</th><th>國(guó)家</th><th>地區(qū)</th><th>人口數(shù)量</th></tr>
</thead>
<tbody>
<%
CityDAO cityDao=new CityDAO();
ArrayList<City>list=cityDao.getAllCities();
if(list!=null&&list.size()>0){
for(int i=0;i<list.size();i++){
City city=list.get(i);
%>
?
<tr><td><%=city.getId() %></td><td><%=city.getName() %></td><td><%=city.getCountryCode() %></td><td><%=city.getDistrict() %></td><td><%=city.getPopulation() %></td></tr>
<%
}
}
%>
</tbody>
</table>
</center>
數(shù)據(jù)庫(kù)連接正常,網(wǎng)頁(yè)顯示只有第一行的表頭,沒有數(shù)據(jù)。求解答
2017-08-12
String sql="select *from city where Name='Shanghai'"; ? //SQL
這里面多了一個(gè)分號(hào)