0

我的主要问题是我有一个类 Database.java,它可以访问和更新 JDBC Derby 数据库。使用单元测试类,Database.java 通过但使用 servlet 会引发异常。可能是什么原因?下面的代码可用。我有一个程序 Database.java 这是

package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class Database {

    Connection connection = null;

    String URL;
    String DRIVER;
    PreparedStatement updateVotes, totalVotes, results; 

    private static Database unique;

    private Database()
    {
        URL = "jdbc:derby:C:\\Documents and Settings\\tr1b4361\\Voting";
        DRIVER =  "org.apache.derby.jdbc.EmbeddedDriver";

        try {   

            Class.forName("DRIVER").newInstance();
            connection = DriverManager.getConnection(URL);          

        } catch (SQLException e) {

            e.printStackTrace();
        }
        catch (InstantiationException e) {

            e.printStackTrace();
        } catch (IllegalAccessException e) {

            e.printStackTrace();
        }
        catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
    }

    public static Database getInstance()
    {
        if(unique == null)
        {
            unique = new Database();
        }

        return unique;
    }

    ....
}

我为类 DatabaseTest 编写了一个单元测试,它成功测试并验证了 Database 类

package test;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;

import com.Database;
import com.Vote;
import com.voting.Voter;


public class DatabaseTest {


//  @Test
//  public void testUpdateVotes() {
//      
//      Database database = new Database();
//      
//      assertEquals("equal", 1, database.updateVotes("BIRD"));
//      assertEquals("eqaul", 1, database.updateVotes("BIRD"));
//  }

    @Test
    public void testSumVotes() {
        Database database = Database.getInstance();

        assertEquals("equal", 9, database.sumVotes());

    }

    @Test
    public void testGetTable() {
        Database database = Database.getInstance();
        List<Vote> l = database.getTable(); 

        Iterator<Vote> i = l.iterator();


        while(i.hasNext())
        {
            Vote v = i.next();
            System.out.println(v.animal + ": " + v.vote);
        }

        List<Vote> l2 = new ArrayList<Vote>();
        l2.add(new Vote("BIRD", 4));
        l2.add(new Vote("CAT", 5));
        l2.add(new Vote("DOG", 0));
        l2.add(new Vote("NONE", 0));
        l2.add(new Vote("SNAKE", 0));

        assertEquals("equal", l.get(0).animal, l2.get(0).animal);
        assertEquals("equal", l.get(1).animal, l2.get(1).animal);
        assertEquals("equal", l.get(2).animal, l2.get(2).animal);
        assertEquals("equal", l.get(3).animal, l2.get(3).animal);
        assertEquals("equal", l.get(4).animal, l2.get(4).animal);


    }

}

但是当从 servlet Voter 调用数据库实例时,它会给出 ClassNotFoundException

java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.Database.<init>(Database.java:31)
    at com.Database.getInstance(Database.java:55)
    at com.voting.Voter.doPost(Voter.java:77)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
May 25, 2012 11:08:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.voting.Voter] in context with path [/Ch9_Fig9_27] threw exception
java.lang.NullPointerException

servlet Voter.java 的代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        database = Database.getInstance();
//      Database2 database= new Database2();
        String animalName = (String) request.getAttribute("animal");
        database.updateVotes(animalName);

//      RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
//      rd.forward(request, response);
4

1 回答 1

0

您需要使您的 servlet 可以访问 derby.jar。根据您的应用服务器,有多种方法可以做到这一点。如果您正在构建 .war 文件,最简单的做法是将 derby.jar 包含在您的 war 文件的 WEB-INF/lib 子目录中。

于 2012-05-25T23:48:42.013 回答