我创建了一个简单的JSP,显示从数据库中获取的浏览器信息。与数据库的连接我创建了一个在jsp中调用的简单类。它们引发了两个异常:JasperException和NullPointerException。 我不明白我哪里出错了。我求你帮忙。
P.S .:对不起我的英语:) 这是一个index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>WORLD</title>
</head>
<body>
<%@ page language="java" import="java.sql.*" %>
<%@ page language="java" import="connessione.*" %>
<%
Connessione con = new Connessione();
Connection cnn = con.connetti();
Statement st = null;
ResultSet rs = null;
try
{
st = cnn.createStatement();
rs = st.executeQuery("SELECT code,name,continent FROM country");
int code=0;
String name="";
String continent="";
out.println("<h1>ELENCO STATI:</h1>");
while(rs.next())
{
code = rs.getInt(1);
name = rs.getString(2);
continent = rs.getString(3);
out.println("<br><br><b>ID:</b> "+ code + " <br> " + "<b>COUNTRY:</b> "+name+" <br> " + "<b>CONTINENT</b> "+continent+" <br> ");
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
rs.close();
st.close();
cnn.close();
}
catch(SQLException e)
{
out.println(e.getMessage());
}
}
%>
</body>
</html>
这是一个班级Connessione
package connessione;
import java.sql.*;
public class Connessione
{
protected String driver = "com.mysql.jdbc.Driver", url = "jdbc:mysql//localhost:3306/world ";
protected String user = "root", psw = "informatica";
public Connection connetti()
{
Connection dbConn = null;
try
{
Class.forName(driver);
dbConn = DriverManager.getConnection(url, user, psw);
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch(SQLException ex)
{
ex.getMessage();
}
return dbConn;
}
}
,这是一个错误页面
type Exception report
message An exception occurred processing JSP page /index.jsp at line 47
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 47
44: }
45: finally
46: {
47: rs.close();
48: st.close();
49: cnn.close();
50: }
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWra pper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 76)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:158)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:4 38)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.33 logs.
我使用eclipse jee mars,tomcat v8.0 .33和mySQL。从eclipse我使用Run on server。 在我将war文件导出保存在tomcat的webapps文件夹之前。我在tomcat的lib文件夹中添加了mysql连接器,在我的项目中使用了外部jar的eclipse。
答案 0 :(得分:1)
我已经解决了,正如他所说@Lawrence Tierney我检查了我的sql是否正常......我创建了一个简单的java应用程序,在控制台上显示查询的结果......仍然看到NullPointerException引发了..我简直不敢相信自己的眼睛....然后我说的是DB,也因为同一个DBMS上的其他java应用程序工作。但后来我认为数据库不正确我不可能看到原因...然后我试图删除Connessione类并在我的类的构造函数中建立与DB的连接....瞧它有效!!!!!我回到我的web项目,我删除了Connessione类,并直接在jsp中建立连接,它的工作原理!我甚至不知道为什么没有传达jsp和班级Connessione ......但是谢谢你的帮助!!!
新的jsp代码工作吧!!!
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>WORLD</title>
</head>
<body>
<%@ page language="java" import="java.sql.*" %>
<%
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/world";
String USER = "root";
String pass = "informatica";
Connection dbConn = null;
Statement st = null;
ResultSet rs = null;
try
{
//Registro il driver del database
Class.forName(JDBC_DRIVER);
// Connessione al db tramite il driver
dbConn = DriverManager.getConnection(DB_URL, USER, pass);
if(!dbConn.isClosed())
{
out.println("Connessione avvenuta con successo a mysql server. . .");
}
st = dbConn.createStatement();
rs = st.executeQuery("SELECT Code,name,continent from country where name like 'i%';");
String code="";
String name="";
String continent="";
out.println("<h1>ELENCO STATI CON LA I:</h1>");
while(rs.next())
{
code = rs.getString(1);
name = rs.getString(2);
continent = rs.getString(3);
out.println("<br><br><b>ID:</b> "+ code + " <br> " + "<b>COUNTRY: </b> "+name+" <br> " + "<b>CONTINENT</b> "+continent+" <br> ");
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
rs.close();
st.close();
dbConn.close();
}
catch(SQLException e)
{
out.println(e.getMessage());
}
}
%>
</body>
</html>
答案 1 :(得分:0)
当你试图“关闭”它们时,看起来rs,st或cnn都是null(可能是rs)
检查它们是否先不为空?
编辑:读完ResultSet的文档后,似乎executeQuery永远不会返回null。因此我想知道你的SQL是否正常?
就像上面提到的人一样,完整的日志会很好......