我正在尝试使用访问数据库查看此页面的示例(Pagination with JSP),但是我收到以下错误:
org.hsqldb.HsqlException:游标状态不正确:指示游标没有位于语句UPDATE,DELETE,SET或GET的行中;查询结果的当前位置在第一个记录之前
我的代码是:
水果类
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Daniel
*/
public class Fruta {
private String fruta, color, sabor;
public Fruta() throws ClassNotFoundException, SQLException{
fruta = "";
color = "";
sabor = "";
}
/**
* @return the fruta
*/
public String getFruta() {
return fruta;
}
/**
* @param fruta the fruta to set
*/
public void setFruta(String fruta) {
this.fruta = fruta;
}
/**
* @return the color
*/
public String getColor() {
return color;
}
/**
* @param color the color to set
*/
public void setColor(String color) {
this.color = color;
}
/**
* @return the sabor
*/
public String getSabor() {
return sabor;
}
/**
* @param sabor the sabor to set
*/
public void setSabor(String sabor) {
this.sabor = sabor;
}
}
FruitDAO
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Daniel
*/
public class FrutaDAO {
Connection connection;
Statement stmt;
private int noRecords;
public FrutaDAO(){}
private static Connection getConnection()
throws SQLException,
ClassNotFoundException
{
Connection con = Conexion.getConnection();
return con;
}
public List<Fruta> verFrutas(
int offset,
int noOfRecords)
{
String query = "SELECT * FROM Frutas LIMIT "
+ offset + ", " + noOfRecords;
List<Fruta> list = new ArrayList<Fruta>();
Fruta fruta = null;
try {
connection = getConnection();
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
if(rs.next()){
while (rs.next()) {
fruta = new Fruta();
fruta.setFruta(rs.getString("Fruta"));
fruta.setFruta(rs.getString("Color"));
fruta.setFruta(rs.getString("Sabor"));
list.add(fruta);
}
}
rs.close();
rs = stmt.executeQuery("SELECT COUNT(*) FROM Frutas");
if(rs.next())
this.noRecords = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally
{
try {
if(stmt != null)
stmt.close();
if(connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public int getNoOfRecords() {
return noRecords;
}
}
FruitServlet
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Daniel
*/
@WebServlet(name = "FrutaServlet", urlPatterns = {"/FrutaServlet"})
public class FrutaServlet extends HttpServlet {
public FrutaServlet() {super();}
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
int page = 1;
int recordsPerPage = 5;
if(request.getParameter("page") != null)
page = Integer.parseInt(request.getParameter("page"));
FrutaDAO dao = new FrutaDAO();
List<Fruta> list = dao.verFrutas((page-1)*recordsPerPage,
recordsPerPage);
int noOfRecords = dao.getNoOfRecords();
int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
request.setAttribute("listaFruta", list);
request.setAttribute("noOfPages", noOfPages);
request.setAttribute("currentPage", page);
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request, response);
}
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet FrutaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet FrutaServlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我已经在使用 rs.next()了,这里发生了什么?
答案 0 :(得分:0)
我不确定这是否可以解决,但您不应该对同一查询使用while(rs.next())
和//if(rs.next()){ //remove this check
while (rs.next()) {
fruta = new Fruta();
fruta.setFruta(rs.getString("Fruta"));
fruta.setFruta(rs.getString("Color"));
fruta.setFruta(rs.getString("Sabor"));
list.add(fruta);
}
//}
rs.close();
,请尝试修改 FruitDAO 的代码如下所示:
#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device rd; //seed generator
mt19937_64 generator{rd()}; //generator initialized with seed from rd
uniform_int_distribution<> dist{1, 6};
for(int i = 0; i < 15; i++)
{
int random = dist(generator);
cout << random << endl;
}
}