Intento hacer una aplicacion web muy simple, donde muestre el resultado de la ejecucion de un query de SQL. Pero resulta en un NPE. Aqui esta el JSP:
<%--
Document : consultas
Created on : 25-may-2017, 23:20:40
Author : Cliente
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="datos.Conexion"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.sql.Statement"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Consultas</title>
</head>
<body>
<h1>Consultas</h1>
<a href="../index.jsp">Inicio</a>
<p>Consultas Disponibles:</p>
1. Conocer el nombre de todos los chóferes que tengan mas de 25 años.
<%
Conexion c = new Conexion();
c.conectar();
ResultSet rs;
String query = "SELECT Chofer.nombre,Chofer.aPaterno, Chofer.aMaterno" +
"FROM Chofer" +
"WHERE FLOOR(DATEDIFF (dd, fecha_nac, GETDATE())/365.25)>25";
rs = c.ejecutarConsulta(query);
//TABLA HTML:
out.write("<table style=\"width:100%\">");
out.write("<tr>");
out.write("<th>Nombre</th>");
out.write("<th>Apellido Paterno</th>");
out.write("<th>Apellido Materno</th>");
out.write("</tr>");
while(rs.next()){
out.write("<tr>");
out.write("<td>"+rs.getString(1)+"</td>");
out.write("<td>"+rs.getString(2)+"</td>");
out.write("<td>"+rs.getString(3)+"</td>");
out.write("</tr>");
}
out.write("</table>");
rs.close();
%>
</body>
</html>
y aqui el codigo en Java de la clase Conexion
package datos;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Cliente
*/
public class Conexion {
private Connection con;
private Statement stmt;
private ResultSet rs;
public Conexion() {
stmt = null;
con = null;
rs = null;
}
public void conectar() throws Exception {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://DESKTOP-92RP7H8\\MSSQLSERVER:1433;"+
"databaseName=Pumer;"+
"user = sa;"+
"password = PerroLuka;";
con = DriverManager.getConnection(connectionUrl);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("SQLException: " + e.getMessage() + " conectar =(");
}
}
public void desconectar() throws SQLException {
try {
con.close();
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage() + " desconectar =(");
}
}
public ResultSet ejecutarConsulta(String consulta) throws Exception {
try {
stmt = con.createStatement();
rs = stmt.executeQuery(consulta+";");
return rs;
} catch (Exception ex) {
System.out.println("SQLException: " + ex.getMessage() + " getMultas");
}
return null;
}
}
Output de GlassFishServer
Información: SQLException: Sintaxis incorrecta cerca de 'FLOOR'. ejecutarConsultas
Advertencia: StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.Utilidades.consultas_jsp._jspService(consultas_jsp.java:91)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)
Si de algo sirve, estoy usando el IDE de NetBeans
Ya llevo mucho tiempo intentando entender el problema y necesito ayuda, gracias.