使用limit分页无效(所有代码不包括sql,jar包)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="<%=request.getContextPath() %>/addCourseServlet">
<table>
<thead>
<tr>
<th colspan="2">课程添加</th>
</tr>
</thead>
<tbody>
<tr>
<td>课程名</td>
<td><input type="test" name="courseName" /></td>
</tr>
<tr>
<td>所属方向</td>
<td><input type="test" name="category" /></td>
</tr>
<tr>
<td>课程描述</td>
<td><input type="test" name="desp" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="添加">
</td>
</tr>
</tbody>
<tfoot></tfoot>
</table>
</form>
</center>
</body>
</html>
-------------------------------------------
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<c:if test="${add!=null }">
<h1>添加成功!</h1>
</c:if>
<table border="1">
<tr>
<th>课程名称</th>
<th>所属方向</th>
<th>课程描述</th>
<th>创建时间</th>
</tr>
<c:if test="${list!=null || fn:length(list)!=0}">
<c:forEach var="course" items="${list }">
<tr>
<td>${course.name}</td>
<td>${course.category}</td>
<td>${course.desp}</td>
<td><fmt:formatDate value="${course.createTime}" pattern="yyyy-MM-dd"/></td>
</tr>
</c:forEach>
</c:if>
</table>
</center>
</body>
</html>
----------------------------------
package addCourse.model;
import java.sql.Date;
public class course {
private int id;
private String name;
private String category;
private String desp;
private Date createTime;
public course(){}
public course(int id, String name, String category, String desp, Date createTime) {
super();
this.id = id;
this.name = name;
this.category = category;
this.desp = desp;
this.createTime = createTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date date) {
this.createTime = date;
}
@Override
public String toString() {
return "course [id=" + id + ", name=" + name + ", category=" + category + ", desp=" + desp + ", createTime="
+ createTime + "]";
}
}
--------------------------------------
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()) {
// response.sendRedirect("/addCourse.jsp");
// }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");
}
List<course> list = add.FindAllCourse(0,5);
for(course cd : list) {
System.out.println("---------------------------------------------------");
System.out.println(cd.getName()+" "+cd.getCategory()+" "+cd.getDesp());
}
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 addCourseDao;
import java.util.List;
import addCourse.model.course;
public interface addCourseDao {
public boolean addCourse(course c);
public List<course> FindAllCourse(int index, int column);
}
--------------------------------------
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;
}
}
-------------------------------------------------
package addCourseJDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class jdbcUtils {
private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();
public static Connection getConn() throws SQLException {
Connection conn = dataSource.getConnection();
return conn;
}
public static void release(Connection conn,Statement st,ResultSet rs) {
closeConnection(conn);
closeStatement(st);
closeResultSet(rs);
}
public static void release(Connection conn,Statement st) {
closeConnection(conn);
closeStatement(st);
}
private static void closeConnection(Connection conn) {
try {
if(conn!=null) {
conn.close();
}
}catch(Exception ex) {ex.printStackTrace();}
conn = null;
}
private static void closeStatement(Statement st) {
try {
if(st!=null) {
st.close();
}
}catch(Exception ex) {ex.printStackTrace();}
st = null;
}
private static void closeResultSet(ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
}catch(Exception ex) {ex.printStackTrace();}
rs = null;
}
}
---------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///school</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
-------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///school</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">20</property>
</default-config>
</c3p0-config>
正在回答 回答被采纳积分+1
你好同学,我针对你的代码进行了一些修改,可以看出你的FindAllCourse方法中的index,是当前页面参数,column是每页显示的行数,当前页码-1,乘colum,也就是从第几条数据开始查,这样sql语句就完成了,同学只需要控制index和column参数就可以控制页面显示的数据了,如果有不明白的可以继续提问哦,祝学习愉快~
你好同学,你是说limit无法分页查询吗?如果是,我在你代码的基础上进行了修改,是可以进行修改的哦,譬如下图代码,同学可以参考一下哦,祝学习愉快~
建议同学,粘贴一下自己的sql语句。这样可以保证我们的运行环境和你是一样的。祝学习愉快~
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星