1,105,417 Community Members

Tomcat 7 Connection Pool Problem

Member Avatar
jakubee
Newbie Poster
16 posts since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everyone,
I dont know if im placing this in a good place.
I have a problem with my application. Its a simple application with connecting to database, retriving info and populating table.
I changed the apache tomcats context.xml to looks like this:

<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"

maxActive="100" maxIdle="30" maxWait="10000"

username="root" password="MY_PASSWORD" driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/test"/>

****My jsp look like this: ****

<%@ 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">

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.*" %>
<%@ page import="business.Module" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body> 


<table border=1 bgcolor="lightgrey">
<tr> <th>ID</th><th>Code</th><th>Description</th><th>Lecturer</th>
<c:forEach var="module" items="${sessionScope.modules}">
    <tr>
      <td><c:out value="${module.id}" /></td>
      <td><c:out value="${module.code}" /></td>
      <td><c:out value="${module.description}" /></td>   
      <td><c:out value="${module.lecturer}" /></td>      
    </tr>  
</c:forEach>
</table>
<form action="TestConnectionServlet" method="post">
        <input type="hidden" name="action" value="populate" />
        <input type="submit" value="populate table" />

</form>

</body>
</html>

After i run this i get the errors:

SEVERE: Servlet.service() for servlet [servlet.TestConnectionServlet] in context with path [/Database_Connection_Pooling_-_Exercise1] threw exception
java.lang.NullPointerException**
at dao.ModuleDao.getAllModules(ModuleDao.java:28)
at servlet.TestConnectionServlet.doPost(TestConnectionServlet.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Im using the dao class, moduleDao and testConnectionServlet.

The code that error refers to
ModuleDao:

public class ModuleDao extends Dao {

public List<Module> getAllModules() throws DaoException {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Module> modules = new ArrayList<Module>();

    try {
        //Get connection object using the methods in the super class (Dao.java)...
        con = this.getConnection();

        String query = "SELECT * FROM module";
        ps = con.prepareStatement(query);            <<<<<<<<<<-------- this is line 28

        //Using a PreparedStatement to execute SQL...
        rs = ps.executeQuery();
        while (rs.next()) {
            int id = rs.getInt("id");
            String code = rs.getString("code");
            String description = rs.getString("description");
            String lecturer = rs.getString("lecturer");

            Module m = new Module(id,code,description,lecturer);
            modules.add(m);
        }
    } catch (SQLException e) {
        throw new DaoException("getAllModules()" + e.getMessage());
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                freeConnection(con);
            }
        } catch (SQLException e) {
            throw new DaoException(e.getMessage());
        }
    }

    return modules;
    }

And TestConnectionServlet:

/**
 * Servlet implementation class TestConnectionServlet
 */
@WebServlet("/TestConnectionServlet")
public class TestConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public TestConnectionServlet() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if ( request.getParameter("action").equalsIgnoreCase("populate") ){
        String forwardToJsp = "";
        HttpSession session = request.getSession();
        ModuleDao mDao = new ModuleDao();

            List<Module> modules = new ArrayList<Module>();
            try {
                modules = mDao.getAllModules();     <<<---- this is line 53
            } catch (DaoException e) {
                e.printStackTrace();
            }

            session.setAttribute("modules", modules);
            forwardToJsp = "/TestConnection.jsp";   


        forwardToPage(request, response, forwardToJsp);
    }
}

Im struggling with this whole day now and cant get it working. Any ideas what might be wrong?

Question Self-Answered as of 2 Years Ago
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: