public class OracleFenye extends Action {
private int pageNow = 1;
private int totalPage = 0;
private int pageSize = 3;
@SuppressWarnings("unchecked")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcldb", "scott", "tiger");
Statement st = conn.createStatement();
// 分页
String pageNo = request.getParameter("pageNow");
if (pageNo != null) {
pageNow = Integer.parseInt(request.getParameter("pageNow"));
}
ResultSet rs = st
.executeQuery("select * from (select a1.*,rownum rn from (select * from emp) a1 where rownum <="
+ pageSize
* pageNow
+ ") where rn >="
+ ((pageNow - 1) * pageSize + 1)
+ " ");
List list = new ArrayList();
while (rs.next()) {
list.add(rs.getString(2));
}
// 计算
totalPage rs = st.executeQuery("select count(*) from emp");
List pageList = new ArrayList();
while (rs.next()) {
int rsno = rs.getInt(1);
if (rsno % 2 == 0) {
totalPage = rsno / pageSize + 1;
} else {
totalPage = rsno / pageSize;
}
}
for (int i = 1; i <= totalPage; i++) {
pageList.add(i);
}
request.setAttribute("rsList", list);
request.setAttribute("pageList", pageList);
rs.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return mapping.findForward("fenye");
}
}