使用limit分页查询无效(显示所有结果)
package addCourse.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import addCourse.model.course;
import addCourseDaoImpl.courseDaoImpl;
@WebServlet("/addCourseServlet")
public class addCourseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public addCourseServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
course c = new course();
String courseName = request.getParameter("courseName");
String category =request.getParameter("category");
String desp =request.getParameter("desp");
if(courseName.isEmpty()||category.isEmpty()||desp.isEmpty()) {
request.getRequestDispatcher("/addCourse.jsp").forward(request, response);
}else {
c.setName(courseName);
c.setCategory(category);
c.setDesp(desp);
courseDaoImpl add= new courseDaoImpl();
boolean flag = add.addCourse(c);
if(flag) {
request.getSession().setAttribute("add", "1");
}
System.out.println(flag+"///");
List<course> list = add.FindAllCourse(0,5);
request.getSession().setAttribute("list",list);
request.getRequestDispatcher("/showCourse.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
---------------------------------------------------
package addCourseDaoImpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import addCourse.model.course;
import addCourseDao.addCourseDao;
import addCourseJDBCUtils.jdbcUtils;
public class courseDaoImpl implements addCourseDao {
@Override
public boolean addCourse(course c) {
Connection conn = null;
PreparedStatement pstmt = null;
boolean flag = false;
try {
conn = jdbcUtils.getConn();
String sql = "insert course values(null,?,?,?,now())";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, c.getName());
pstmt.setString(2, c.getCategory());
pstmt.setString(3,c.getDesp());
int x = pstmt.executeUpdate();
if(x>0) {
flag = true;
}
}catch(Exception e) {
e.printStackTrace();
}finally {
jdbcUtils.release(conn, pstmt);
}
return flag;
}
@Override
public List<course> FindAllCourse(int index,int column) {
Connection conn = null;
PreparedStatement pstmt = null;
List<course> list = new ArrayList();
ResultSet rs = null;
try {
conn = jdbcUtils.getConn();
String sql = "select * from course limit ?,?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, index);
pstmt.setInt(2, column);
rs = pstmt.executeQuery();
while(rs.next()) {
course c = new course();
c.setId(rs.getInt("id"));
c.setName(rs.getString("name"));
c.setCategory(rs.getString("category"));
c.setDesp(rs.getString("desp"));
c.setCreateTime(rs.getDate("createTime"));
list.add(c);
}
}catch(Exception e) {
e.printStackTrace();
}finally {
jdbcUtils.release(conn, pstmt,rs);
}
return list;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星