Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   createStatement() not recognized (http://www.programmingforums.org/showthread.php?t=12727)

paulchwd Mar 5th, 2007 10:13 PM

createStatement() not recognized
 
Hello All,

I have 2 classes EstablishDBConnection.java which reads the database connection details from a txt file and then my servlet CustomQuery.java which used the other class to establish the connection and do its servlet stuff

But i the second (general) exception in doPost fires


Servlet code:

:

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletResponse;

public class CustomQuery extends HttpServlet
{

        Statement stmt;
        ResultSet rs;
        PrintWriter printer;
        EstablishDBConnetion edbc;

        public void init() throws ServletException
        {
                edbc = new EstablishDBConnetion();
                edbc.establishConnection();

        }

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

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
               
                response.setContentType("text/html");
                printer = response.getWriter();

                String title = "Result from custom query";

                try
                {

                        stmt = edbc.createStatementObj();
                        printer.println("<html><body><font color=black> test </font></body></html>");

                        stmt.createStatement();

                        rs = stmt.executeQuery("SELECT customerID" +
                                                    "  FROM customer");


                        while (rs.next())
                        {

                                printer.println("<html> <head><title>" + title + "</title><br><br><table border=\"0\"> <tr><td>"
                                                    + rs.getString(1));

                        }

                }

                catch (SQLException sqlx)
                {
                        printer.println("SQL Exception");
                        sqlx.printStackTrace();
                }

                /*catch (Exception e)
                {
                        printer.println("General Exception");
                        e.printStackTrace();
                }*/

        }

        public void destroy()
        {
                try
                {
                        rs.close();
                        stmt.close();
                        edbc.con.close();
                }
                catch (SQLException e)
                {
                        printer.println("SQL Exception in destroy()");
                        e.printStackTrace();
                }
                catch (Exception e)
                {
                        printer.println("Exception in destroy()");
                        e.printStackTrace();
                }

        }
}


establishConnection code

:

import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;

class EstablishDBConnetion
{
        Connection con;

        public void establishConnection()
        {

                try
                {
                        String dbDriver = "";
                        String dbUrl = "";
                        String dbName = "";
                        String dbUser = "";
                        String dbPassword = "";
                        String newLine = "";

                        BufferedReader fileRead = new BufferedReader(new FileReader("connection.txt"));

                        boolean readerStatus = fileRead.ready();

                        newLine = fileRead.readLine();

                        while (readerStatus)
                        {
                                StringTokenizer st = new StringTokenizer(newLine, ",");

                                while (st.hasMoreTokens())
                                {
                                        dbDriver = st.nextToken();
                                        dbUrl = st.nextToken();
                                        dbName = st.nextToken();
                                        dbUser = st.nextToken();
                                        dbPassword = st.nextToken();
                                }

                                readerStatus = fileRead.ready();

                        }

                        Class.forName(dbDriver);
                        String url = dbUrl + dbName;
                        System.out.print(url);
                        con = DriverManager.getConnection(url, dbUser, dbPassword);



                }

                catch (SQLException sqlx)
                {
                        sqlx.printStackTrace();
                }

                catch (IOException e)
                {
                        System.out.println("IO exception in establishConnection()");

                        e.printStackTrace();
                }

                catch (Exception ex)
                {
                        System.out.println("General exception in establishConnection()");
                        ex.printStackTrace();
                }
        }

        public Statement createStatementObj() throws SQLException
        {
                Statement stmt = con.createStatement();
               
                return (stmt);
        }

        public static void main(String[] args)
        {
                EstablishDBConnetion edbc = new EstablishDBConnetion();

                edbc.establishConnection();
        }
       
}



Output:




Output:

java.lang.NullPointerException
at EstablishDBConnetion.createStatementObj(EstablishDBConnetion.java:74)
at CustomQuery.doPost(CustomQuery.java:66)
at CustomQuery.doGet(CustomQuery.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:417)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:131)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)

Kaja Fumei Mar 6th, 2007 5:59 PM

Try rebuilding your code. I don't see how it even compiles this line in your servlet's doPost():

:

stmt.createStatement();

Because java.sql.Statement has no createStatement() method.

You already created a Statement and even if you changed that line to "edbc.createStatementObj()", the return value still is ignored and you dont need another Statement so I don't know why its there.

paulchwd Mar 6th, 2007 7:58 PM

thanks, i got rid of that line in my servlet class

and in my other class i changed it to

:

Statement stmt = new con.createStatement();
(i declared it as a global variable) and i killed the createStatementObj() method

so in my servlet i have

:

rs = edbc.stmt.executeQuery("SELECT customerID" +
                                                    "  FROM customer");


and i get this error from apache:

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
at CustomQuery.doPost(CustomQuery.java:70)
at CustomQuery.doGet(CustomQuery.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:417)
at org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:131)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)


and java.Sql does have a createStatement() method, its used to create a statement obj.

Arevos Mar 7th, 2007 5:02 AM

Quote:

Originally Posted by paulchwd (Post 124893)
java.lang.NullPointerException

That simply means one of your variables is null when it shouldn't be. Without seeing the whole code, however, the cause of it is impossible to figure out. It shouldn't be too hard to track down, however.

Quote:

Originally Posted by paulchwd (Post 124893)
and java.Sql does have a createStatement() method, its used to create a statement obj.

Kaja said that java.sql.Statement has no createStatement method. And Kaja is right, because it doesn't - that method belongs in the java.sql.Connection class.

Quote:

Originally Posted by paulchwd (Post 124893)
:

Statement stmt = new con.createStatement();

createStatement is a method, not a class, so you don't need the "new" operator preceding it.

paulchwd Mar 7th, 2007 10:04 PM

I put all the connection code in init() of the servlet, but still get a null pointer exceptionat run time (when i run it in apache 4.1) at the line:

stmt = con.createStatement();

I dont see why

Code:

:

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletResponse;

public class CustomQuery extends HttpServlet
{
        Connection con;
        Statement stmt; //this is where stmt is declared
        ResultSet rs;
               
        public void init() throws ServletException
        {

                try
                {
                       
                        String dbDriver = "";
                        String dbUrl = "";
                        String dbName = "";
                        String dbUser = "";
                        String dbPassword = "";
                        String newLine = "";

                        BufferedReader fileRead = new BufferedReader(new FileReader("connection.txt"));

                        boolean readerStatus = fileRead.ready();

                        newLine = fileRead.readLine();

                        while (readerStatus)
                        {
                                StringTokenizer st = new StringTokenizer(newLine, ",");

                                while (st.hasMoreTokens())
                                {
                                        dbDriver = st.nextToken();
                                        dbUrl = st.nextToken();
                                        dbName = st.nextToken();
                                        dbUser = st.nextToken();
                                        dbPassword = st.nextToken();
                                }

                                readerStatus = fileRead.ready();

                        }

                        Class.forName(dbDriver);
                        String url = dbUrl + dbName;
                        con = DriverManager.getConnection(url, dbUser, dbPassword);

                        //System.out.print(url + "\n");
                        //System.out.println(dbUser + "\n");
                        //System.out.println(dbPassword + "\n");


                }

                catch (IOException ioex)
                {
                        ioex.printStackTrace();

                }
                catch (SQLException sqlex)
                {
                        sqlex.printStackTrace();
                }

                catch (Exception ex)
                {
                        ex.printStackTrace();
                }

               
        }

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

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

                response.setContentType("text/html");
                PrintWriter printer = response.getWriter();
                String title = "Result from custom query";

                try
                {

                        stmt = con.createStatement();
                        printer.println("<html><body><font color=black> test </font></body></html>");

                       
                        rs = stmt.executeQuery("SELECT customerID" +
                                                                        "  FROM customer");


                        while (rs.next())
                        {

                                printer.println("<html> <head><title>" + title + "</title><br><br><table border=\"0\"> <tr><td>"
                                                                + rs.getString(1));

                        }

                }

                catch (SQLException sqlx)
                {
                        printer.println("SQL Exception");
                        sqlx.printStackTrace();
                }

               

        }

        public void destroy()
        {
                try
                {
                        rs.close();
                        stmt.close();
                        con.close();
                       
                                               
                }
                catch (SQLException e)
                {
                       
                        e.printStackTrace();
                }
                catch (Exception e)
                {
                       
                        e.printStackTrace();
                }

        }
}


Apache Output:


:

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
        at CustomQuery.doPost(CustomQuery.java:94)
        at CustomQuery.doGet(CustomQuery.java:81)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:696)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:809)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:198)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:144)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:138)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
        at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2459)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:132)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
        at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:593)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:126)
        at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:432)
        at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:954)
        at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
        at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
        at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
        at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
        at java.lang.Thread.run(Thread.java:619)

Apache Tomcat/4.1.34


any ideas ?


I have ensured that MySql is running and the connection data looks right

Arevos Mar 8th, 2007 5:01 AM

Output some logging information. Verify that init runs, and verify that it gets to the part where you assign con without throwing an exception. Then verify afterwards that con is not null, and that the correct data was passed to it. (Incidentally, why are you using a text file instead of putting the data in a XML config file?)

paulchwd Mar 8th, 2007 1:03 PM

init() not firing
 
Hello,

It seems that init() does not fire, thus my connection is never established. I cannot figure out why

Here is the updated servlet code:
:

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletResponse;

public class CustomQuery extends HttpServlet
{
        Connection con;
        Statement stmt; //this is where stmt is declared
        ResultSet rs;
        String dbDriver;
        BufferedReader fileRead;

        public void init() throws ServletException
        {

                try
                {
                       
                        dbDriver = "";
                        String dbUrl = "";
                        String dbName = "";
                        String dbUser = "";
                        String dbPassword = "";
                        String newLine = "";

                        fileRead = new BufferedReader(new FileReader("connection.txt"));

                        boolean readerStatus = fileRead.ready();

                        newLine = fileRead.readLine();

                        while (readerStatus)
                        {
                                StringTokenizer st = new StringTokenizer(newLine, ",");

                                while (st.hasMoreTokens())
                                {
                                        dbDriver = st.nextToken();
                                        dbUrl = st.nextToken();
                                        dbName = st.nextToken();
                                        dbUser = st.nextToken();
                                        dbPassword = st.nextToken();
                                }

                                readerStatus = fileRead.ready();

                        }

                        Class.forName(dbDriver);
                        String url = dbUrl + dbName;
                        con = DriverManager.getConnection(url, dbUser, dbPassword);

                        //System.out.print(url + "\n");
                        //System.out.println(dbUser + "\n");
                        //System.out.println(dbPassword + "\n");


                }

                catch (IOException ioex)
                {
                        ioex.printStackTrace();

                }
                catch (SQLException sqlex)
                {
                        sqlex.printStackTrace();
                }

                catch (Exception ex)
                {
                        ex.printStackTrace();
                }


        }

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

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

                response.setContentType("text/html");
                PrintWriter printer = response.getWriter();

                printer.println(dbDriver);

               
        }
}


Server and web.xml files can be downloaded from pcwebs.ca/pf/server.xml and pcwebs.ca/pf/web.xml

note: as i understand because of the way my server and web.xml (web.xml in the conf directory not web-inf directory) are, all i have to do to deploy is stop the server put my newly compiled class into the installdir/\webapps\ROOT\WEB-INF\classes start the server and access it by http://localhost/servler/ClassName

ps: im using a txt file to store the connection details as I dont know XML

Arevos Mar 8th, 2007 1:55 PM

Try:
:

public void init(ServletConfig config) throws ServletException

paulchwd Mar 8th, 2007 2:21 PM

thanks but i still get the same result


All times are GMT -5. The time now is 1:41 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC