1
I am developing an application using JSP and servelt in which I want to get the total amount of records from my tb_driver table,created a servelt in which I passed the method that makes the query and created a session but when taking the session created in servelet and put in the page listMotorista2 it appears the following error.An error occurred at line: 47 in the jsp file: /listaMotoristas2.jsp Cannot cast from Object to int
public int totalRegistros(){
try {
con = Conecta.conexao();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Conecta.class.getName()).log(Level.SEVERE, null, ex);
}
try {
String sql="Select count(*) as contaRegistros from tb_motorista";
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);
rs.next();
int totalRegistros=Integer.parseInt(rs.getString("contaRegistros"));
return totalRegistros;
} catch (Exception e) {
JOptionPane.showConfirmDialog(null,e);
}
return 0;
}
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
MotoristasDAO dao= new MotoristasDAO();
try {
int totalMotorista=dao.totalRegistros();
request.setAttribute("totalMotoristas", totalMotorista);
RequestDispatcher rd=
request.getRequestDispatcher("/listaMotoristas2.jsp");
rd.forward(request, response);
} catch (Exception e) {
}
}
<%
int totalRegistros= (int) request.getAttribute("totalMotorista");//Cannot cast from Object to int
%>
If you insert a
System.out.println(rs.getString("contaRegistros"));
, what is the result? This output could be right after thers.next();
– Weslley Tavares
Weslley Tavares returns the number of records 5
– User1999