简体   繁体   English

与数据库连接的简单jsp

[英]simple jsp with connection on database

I have created a simple JSP that displays the browser information taken from a database . 我创建了一个简单的JSP,用于显示从数据库中获取的浏览器信息。 The connection to the database I created a simple class that call in the jsp . 与数据库的连接我创建了一个简单的类,该类在jsp中调用。 They raise two exceptions : JasperException and NullPointerException . 它们引发两个异常:JasperException和NullPointerException。 I do not understand where i went wrong. 我不明白我哪里做错了。 I ask your help . 请你帮忙。

PS .: sorry for my english :) this is a index.jsp PS。:对不起,我的英语:) 这是一个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>

this is a Class 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;
    }

}

and this is a error page 这是一个错误页面

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.

I use eclipse jee mars , tomcat v8.0 .33 and mySQL. 我使用eclipse jee mars,tomcat v8.0 .33和mySQL。 From eclipse i use Run on server. 从eclipse我使用在服务器上运行。 before I make the export of war file saved in webapps folder of tomcat . 在将war文件导出到保存在tomcat的webapps文件夹中之前。 I added the mysql connector is in the lib folder of tomcat with both the external jar 's in eclipse on my project. 我添加了mysql连接器在tomcat的lib文件夹中,并且在我的项目中两个外部jar都在eclipse中。

I HAVE SOLVED , as he said @Lawrence Tierney I checked if my sql was ok ... I made a simple java application that displayed on the console the result of a query ... and still see the NullPointerException raised ... I could not believe my eyes .... then I said is the DB , also because other java app on the same DBMS worked . 我已经解决了,就像他说的那样@Lawrence Tierney我检查了我的SQL是否正常...我做了一个简单的Java应用程序,该程序在控制台上显示了查询结果...并且仍然看到NullPointerException出现了问题...我可以不相信我的眼睛....那我说的是DB,也因为同一DBMS上的其他Java应用程序起作用了。 But then I thought it was not possible that the db was not right I could not see the reason ... then I have tried to delete the Connessione class and make the connection to the DB in the constructor of my class .... et voila it works!!!!! 但是后来我以为数据库不太可能是错误的,我看不到原因……然后我试图删除Connessione类,并在我的类的构造函数中建立与DB的连接。瞧,它有效!!!! I go back on my web project I delete the Connessione class and make the connection directly in the jsp and it works !!!! 我回到我的Web项目,删除Connessione类,并直接在jsp中建立连接,它可以正常工作! I do not even know why did not communicate the jsp and class Connessione ... but thank you for the help !!! 我什至不知道为什么没有交流jsp和Connessione类……但是谢谢您的帮助!

new jsp code work it!!! 新的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>

It looks like either rs, st or cnn are null when you try to "close" them (probably rs) 当您尝试“关闭”它们时,看起来rs,st或cnn都为空(可能是rs)

Check to see if they are not null first? 首先检查它们是否不为null?

Edit: having read the doc for ResultSet it appears as if executeQuery can never return null. 编辑:阅读了ResultSet的文档后,好像executeQuery永远不会返回null。 Therefore I wonder if your SQL is ok? 因此,我想知道您的SQL是否还行吗?

Like the guys said above, a full log would be nice... 就像上面说的,完整的日志将是不错的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM