使用limit分页无效(所有代码不包括sql,jar包)

使用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

登陆购买课程后可参与讨论,去登陆

3回答
chrismorgen 2019-03-01 13:56:27

你好同学,我针对你的代码进行了一些修改,可以看出你的FindAllCourse方法中的index,是当前页面参数,column是每页显示的行数,当前页码-1,乘colum,也就是从第几条数据开始查,这样sql语句就完成了,同学只需要控制index和column参数就可以控制页面显示的数据了,如果有不明白的可以继续提问哦,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5c78c8c800013a9909480604.jpg

chrismorgen 2019-02-28 11:35:28

你好同学,你是说limit无法分页查询吗?如果是,我在你代码的基础上进行了修改,是可以进行修改的哦,譬如下图代码,同学可以参考一下哦,祝学习愉快~

http://img1.sycdn.imooc.com//climg/5c7756d0000146f907870571.jpg

  • 提问者 慕粉4191198 #1
    这个好像只能查询前几 填吧,但是不能够实现分页功能哈
    2019-02-28 11:48:40
  • chrismorgen 回复 提问者 慕粉4191198 #2
    请问同学是说分页无效?还是想让老师帮你完成分页功能呢?因为本题中并没有分页的要求,同学想实现的效果是点击上翻页和下翻页来展示不同数据的功能吗?如果是,你的jsp页面中没有传递对应的页数值呀,建议同学具体的描述一下你的需求哦,方便我准确的回答你的问题,祝学习愉快~
    2019-02-28 13:31:30
  • 提问者 慕粉4191198 回复 chrismorgen #3
    老师,我只是想在这里尝试用一下分页功能,不是因为题目没有要求,也不是要老师帮我完成,只是在这里碰到的问题不知道该怎么解决。
    2019-02-28 22:10:11
chrismorgen 2019-02-27 10:15:59

建议同学,粘贴一下自己的sql语句。这样可以保证我们的运行环境和你是一样的。祝学习愉快~

  • 提问者 慕粉4191198 #1
    create table `course` ( `id` int (11), `name` varchar (60), `category` varchar (60), `desp` varchar (90), `createTime` datetime ); insert into `course` (`id`, `name`, `category`, `desp`, `createTime`) values('2','Java Web','java','JSP和servlet','2019-02-21 23:43:38'); insert into `course` (`id`, `name`, `category`, `desp`, `createTime`) values('3','前端小白','前端','HTML/CSS/JS','2019-02-21 23:43:38'); insert into `course` (`id`, `name`, `category`, `desp`, `createTime`) values('4','ajax','ajax','ajax','2019-02-23 21:26:43'); 我那个是随便添加(添加可用)的,只是不能分页显示而已
    2019-02-27 23:12:22
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师