public class NoticeDaoImpl implements NoticeDao {
@Override
public List<Notice> show(int pagenow) {
// TODO Auto-generated method stub
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
List<Notice> list=new ArrayList<Notice>();
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/ff?useUnicode=true&characterEncoding=utf8","root","1234");
//创建SQL语句
String sql="select * from notice order by id asc limit ?,?";
//预处理
ps=conn.prepareStatement(sql);
ps.setInt(1, (pagenow-1)*Notice.pageSize);
ps.setInt(2, Notice.pageSize);
rs=ps.executeQuery();
//遍历
while(rs.next()){
list.add(new Notice(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4)));
// Book book=new Book();
// book.setId(rs.getInt("id"));
// book.setName(rs.getString("name"));
// book.setPrice(rs.getDouble("price"));
// book.setAuthor(rs.getString("author"));
// book.setPublisher(rs.getString("publisher"));
// list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
有需要javaweb项目的可点以下链接javaweb资源_随手乱来的博客-CSDN博客https://blog.csdn.net/m0_51172271/article/details/124672631