📄 listaction.java
字号:
package news.action;
import news.db.*;
import news.form.*;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class ListAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception
{
String target = "list";
PageForm pageBean = (PageForm)form;
Connection con = null;
try{
con=DatabaseConnection.getCon();
Statement st = con.createStatement();
String q1 = "SELECT COUNT(*) AS total FROM news";
ResultSet rs1 = st.executeQuery(q1);
int total = 0;
if(rs1.next()){
total = rs1.getInt("total");
}
pageBean.setMaxRowCount(total);
pageBean.setMaxPage();
int num = pageBean.getRowsPerPage();
int start = (pageBean.getCurPage()-1)*num;
String q2="SELECT * FROM news ORDER BY id DESC LIMIT "+start+","+num;
ResultSet rs2 = st.executeQuery(q2);
Vector v = new Vector();
News news;
while(rs2.next()){
news = new News();
news.setTitle(rs2.getString("title"));
news.setAuthor(rs2.getString("author"));
news.setContent(rs2.getString("content"));
news.setPubtime(rs2.getDate("pubtime"));
v.add(news);
}
pageBean.setData(v);
} catch(Exception e){
e.printStackTrace();
throw e;
} finally{
try{
if(con!=null){
con.close();
}
}catch(Exception e2){}
}
return (mapping.findForward("list"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -