一個困擾我很長時間的錯誤–原來是jsp連結MySql資料庫的問題

來源:互聯網
上載者:User

作的一個jsp系統,開運行時總要報這樣的錯誤:
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:395)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.NullPointerException
org.apache.jsp.first_jsp._jspService(first_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.

然而在重新整理幾遍後,或者過一段時間系統就運行正常,找了很多資料,終於有了結果,解決得益於以下提示:
1.

QUOTE:java.lang.NullPointerException
note The full stack trace of the root cause is available in the Tomcat logs.

查看log
檢查你的jsp頁面
2. java.lang.NullPointerException 是一個運行期異常,說明你代碼在運行是訪問了一個值為null的對象
org.apache.jsp.pass_jsp._jspService(pass_jsp.java:66) 這句說明了出錯語句的位置
在tomcat的安裝目錄下find這個檔案pass_jsp.java,看看第66行是什麼
3.看了代碼,覺得是運行期間con是null,所以Statement  stmt= con.createStatement就會出那個異常,con是null的原因估計是Connection con=yy.getConn(); 這裡。也就是說yy的getConn方法返回了null值 .解決方案: 在用con之前判斷con是什麼,或者用try catch捕捉異常

後來經過查看Tomcat的logs,在logs 裡面有一個stdout_20070104.log檔案,報當天的日誌,裡面有如下資訊:
java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
unable to load driver
java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
unable to load driver
java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
unable to load driver
java.sql.SQLException: Communication failure during handshake. Is there a server running on localhost:3306?
unable to load driver

經思索是資料庫連接檔案OpenDB.java的問題,後重寫該類,改代碼為標準寫法如下
import java.sql.*;
import java.io.*;

public class OpenDB
{
    String host="localhost";
    String database="netshop";
    String userName="root";
    String userPassword="****";//此處的密碼使用者名稱一定要與所用資料庫的密碼及使用者名稱一致
    private String driverName = "org.gjt.mm.mysql.Driver";
  private String url= "jdbc:mysql://"+host+"/"+database+"?useUnicode=true&characterEncoding=gb2312";

    Connection dbConn;
    public OpenDB()
    {

    }
    public Connection getConnection()
    {
        try
        {
            Class.forName(driverName).newInstance();
            dbConn=DriverManager.getConnection(url,userName,userPassword);
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
            dbConn = null;
            System.out.println("unable to load driver");
        }
      
        return dbConn;
    }

}

後經摸索,發現每次開機或者說啟動tomcat以後只要重新編譯一下OpenDB.java檔案,則系統就會運行正常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.