Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7991619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:20:34+00:00 2026-06-04T13:20:34+00:00

My main problem is I have a class, Database.java, that access and updates the

  • 0

My main problem is I have a class, Database.java, that access and updates the JDBC Derby database. Using unit test class, Database.java passes but using a servlet it throws exception. What can be the reason?
Below the codes are available.
I have a program Database.java which is

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;
    }

    ....
}

I wrote a unit test for class DatabaseTest which successfully tests and verifies the Database class

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);


    }

}

But when call the Database instance from the servlet Voter, it gives the 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

The code of the 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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:20:35+00:00Added an answer on June 4, 2026 at 1:20 pm

    You need to make derby.jar accessible to your servlet. Depending on your app server, there are a variety of ways to do this. If you are building a .war file, the simplest thing to do is to include derby.jar in the WEB-INF/lib subdirectory of your war file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a java SE application that uses the mysql database using mysql
I have always felt that in general the main work of a class should
I currently have a custom log manager that internally uses java.util.Formatter . The main
I have a database class that I made that uses PDO to connect to
Basically I have two main questions: What exactly should you unit test? How do
I have new problem. My code: .method public static void Main() cil managed {
Can't understand what is a problem here: I have got main.cpp file where I
I have a reasonably complex layout problem: I would like to have a main
Short problem: #include <iostream> using namespace std; int main() { double **T; long int
I have got a problem with my codeigniter library. To transform database data into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.