user3389732
user3389732

Reputation: 33

JAVA servlet get result from database return to client

So as my title says i am having problems with my server side application,

I have a class with the getter and setter methods called LocationBean

public class LocationBean {

    private String location;
    private String ReaderID;

    public String getLocation () {
        return location;
    }

    public String getReaderID () {
        return ReaderID;
    }

    public void setLocation (String newlocation) {
        location = newlocation;
    }

    public void setReaderID (String newReaderID) {
        ReaderID = newReaderID;
    }
}

then i have my main servlet class called getlocation

    package location;

import java.io.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

public class getlocation extends HttpServlet {

    String ReaderID;
    String locs;


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {

        ReaderID = request.getParameter("readerid");
        locs = LocationDAO.getloc(ReaderID);

        JSONObject loc = new JSONObject();

        loc.put("Location", locs);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        out.print(loc);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
    {
    }
}

Last is my DAO class called LocationDAO

package location;
import java.sql.*;

public class LocationDAO {
    public static String getloc(String ReaderID) {
        // TODO Auto-generated method stub
        Connection conn =null; // Create connection object
        Statement stmt = null;
        ResultSet rs1 = null;
        String database = ""; // Name of database
        String user = ""; // 
        String password = "";
        String url = "jdbc:mysql://:3306/" + database;
        String Location = null;
        String Rid = ReaderID;    

        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch(Exception e) {
            System.err.println(e);
        }

        // connecting to database
        try {
            conn = DriverManager.getConnection(url, user, password);

        } catch(SQLException se) {
            System.err.println(se);
        }
        // Create select statement and execute it

        try {
            String selectSQL = "select Location from `LocationTable` where ReaderID ='"+Rid + "'";
            stmt = conn.createStatement();

            rs1 = stmt.executeQuery(selectSQL);
            // Retrieve the results

            if(rs1.next()) {                              
                Location =  rs1.getString("Location");    
            }

            conn.close();
        } catch(SQLException se) {
            System.err.println(se);
        }
        return Location;
    }
}

I have removed the database details such as the url password etc as this is hosted on my own server and would not like it to be posted on the internet. So they problem when data is sent to this is that i get this error message

HTTP Status 500 -

type Exception report

message

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

exception

java.lang.NullPointerException
location.LocationDAO.getloc(LocationDAO.java:39)
location.getlocation.doGet(getlocation.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

if anyone could help in anyway it would be appreciated

Upvotes: 3

Views: 1889

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96385

When this code connects to the database, if there is a problem (and there are many possible problems, like an incorrect url, or a missing driver jar, or the database is down or not reachable) it throws an exception. The exception gets trapped in the catch block surrounding the connection code and is written to System.err. Then the program proceeds on its way and uses the connection, calling createStatement on it. Since the connection never got initialized it is still null, causing a NullPointerException.

The exception-handling here is counterproductive, it would be much better to let the exception get thrown than to catch it and keep blundering on. Because now you're looking at some stacktrace that is unlikely to point to the original problem, it's just a side-effect of the real issue.

There's some kind of problem with how you're connecting to the database, exactly what is not knowable from what's posted here. Look at the Tomcat console output to see what gets written to System.err, that should give you an indication of what is wrong.

--

Here is the line from the console that the OP found (copied from the comments):

Mar 06, 2014 2:47:01 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive /home/pi/apache-tomcat-7.0.50/webapps/l$ java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.sql.SQLException: No suitable driver found for jdbc:mysql://softwareprojec$ java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

See this answer for how to deal with this.

Upvotes: 1

Related Questions