1. 程式人生 > >一個困擾我很長時間的錯誤--原來是jsp連結MySql資料庫的問題

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


作的一個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檔案,則系統就會執行正常