enter code here
这是将数据从数据库加载到jsp的java代码
包道;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Category;
import utils.DBConnect;
public class CategoryDAO {
public ArrayList<Category> getListCategory() throws SQLException {
Connection connect = DBConnect.getConnection();
ArrayList<Category> listCategory = new ArrayList<>();
String sql = "SELECT * FROM category";
PreparedStatement ps = connect.prepareCall(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Category category = new Category();
category.setCategoryID(rs.getInt("CategoryID"));
category.setCategoryName(rs.getString("CategoryName"));
listCategory.add(category);
}
return listCategory;
};
}
这是html文件显示数据,我是加载类
<ul class="drop">
<%
for (Category c : cateDao.getListCategory()) {
%>
<li><a href="product.jsp?category=<%=c.getCategoryID()%>"><%= c.getCategoryName() %></a></li>
<%
}
%>
</ul>
错误部分将发布在评论中
1条答案
按热度按时间ohtdti5x1#
我不确定我的答案,但是。。。方法connect.preparecall从数据库中调用存储过程,因此,可能您应该改用connect.preparestatement。