public class DBBean {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
private String dbName ="test";
private String dbuser = "root";
private String dbpass ="******";
static{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println(e);
}
}
public void prepareConnection(){
try{
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbName,dbuser,dbpass);
}catch(SQLException e){
System.out.println(e);
}
}
//关闭连接
public void close(){
try {
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
try {
if(pstmt!=null)
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pstmt = null;
}
//设置参数
private void setParems(String[] parems){
if(parems!=null){
for(int i=0;i<parems.length;i++){
try {
pstmt.setString(i+1, parems[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public ResultSet executeQuery(String sql,String[] parems){
ResultSet res = null;
prepareConnection();
try {
pstmt = con.prepareStatement(sql);
setParems(parems);
res = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return res;
}
}
public class StudentBean {
private long id;
private String name;
private String phone;
private int age;
private int score;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
public class StudentDao implements StudentDaoIn {
@Override
public ArrayList<StudentBean> findByPage(int page){
DBBean db = new DBBean();
int begin = (page-1) * 5;
String sql = "select * from t_student limit "+begin+",5";
ResultSet rs = db.executeQuery(sql,null);
ArrayList<StudentBean> list = new ArrayList<StudentBean>();
try {
while(rs.next()){
StudentBean st = new StudentBean();
st.setName(rs.getString("name"));
st.setAge(rs.getInt("age"));
st.setId(rs.getInt("id"));
st.setPhone(rs.getString("phnoe"));
st.setScore(rs.getInt("score"));
list.add(st);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
public int userCount(){
DBBean db = new DBBean();
String sql = "select count(*) from t_student";
ResultSet rs = db.executeQuery(sql, null);
int count = 0;
try {
rs.next();
count = rs.getInt(1);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String page = null;
page = request.getParameter("page");
if(page == null || page=="")
page = "1";
StudentDao studao = new StudentDao();
request.setAttribute("student",studao.findByPage(Integer.parseInt(page)));
request.setAttribute("pagenum",studao.userCount()/5+1);//总页数
request.setAttribute("page", page);//当前页
request.getRequestDispatcher("student.jsp").forward(request, response);
}
<table id="t_stu" border="1" cellpadding="2" cellspacing="0">
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>电话</th>
<th>成绩</th>
</tr>
</thead>
<c:forEach items="${student}" var="st">
<tr>
<td>${st.getId()}</td>
<td>${st.getName()}</td>
<td>${st.getAge()}</td>
<td>${st.getPhone()}</td>
<td>${st.getScore()}</td>
</tr>
</c:forEach>
</table>
<br>
共 ${pagenum}页 当前 第${page}页
<c:choose>
<c:when test="${page>1}">
<a href="getSutent?page=${page-1}" rel="external nofollow" ><input type="button" value="上一页" ></a>
</c:when>
<c:otherwise>
<input type="button" value="上一页" disabled="disabled" />
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${page!=pagenum}">
<a href="getSutent?page=${page+1}" rel="external nofollow" ><input type="button" value="下一页"></a>
</c:when>
<c:otherwise>
<input type="button" value="下一页" disabled="disabled" />
</c:otherwise>
</c:choose>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有