Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 2nd, 2007, 11:59 PM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Question Servlet problems

Hello:

Im a new to working with servlets so please bear with me:

I have 2 classes, one that reads database connection details from a file and establishes a database connection with a mysql database. and a second class (a servlet) that for now just does a simple query on the database and returns the result.

2 problems with the servlet code (no problems with the other class):

1. from the init method of the servlet, i call a method in my EstablishDBConnetion class to establish the connection. The compiler says i cannot reference this no-static method from a static context(Must it be static for my to reference it by Class.method ?)

2. The compiler doesnt seem to recognize its a servlet.

errors:

CustomQuery.java:12: non-static method establishConnection() cannot be referenced from a static context
                EstablishDBConnetion.establishConnection();
                                    ^
CustomQuery.java:19: package response does not exist
                PrintWriter printer = new response.getWriter();
                                                  ^
CustomQuery.java:23: cannot find symbol
symbol  : variable establishConnection
location: class EstablishDBConnetion
                Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();
                                                     ^
CustomQuery.java:40: cannot find symbol
symbol  : variable con
location: class CustomQuery
                con.close();
                ^
4 errors


Servlet Code

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

public class CustomQuery extends HttpServlet
{

	public void init() throws ServletException
	{
		EstablishDBConnetion.establishConnection();
		
	}

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

		String title = "Result from custom query";

		Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();

		ResultSet 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) +"</td></tr></table></body></html>");
			
                
				                
		}

		rs.close();
		stmt.close();
		con.close();
	}

}


Establish Connection Code

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

class EstablishDBConnetion
{

   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;
			Connection con = DriverManager.getConnection(url, dbUser, dbPassword);

			
		}

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

			e.printStackTrace();
		}

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

}
My Classpath:

.;C:\Program Files\QuickTime\QTSystem\QTJava.zip;"X:\Documents and Settings\Paul\My Documents\java\Code\servlets\invoice";"C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar"

Again, many thanks
paulchwd is offline   Reply With Quote
Old Mar 3rd, 2007, 2:02 AM   #2
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Question

pls ignore above code and errors, and see correct below

Errors

CustomQuery.java:4: package javax.servlet does not exist
import javax.servlet.*;
^
CustomQuery.java:5: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
CustomQuery.java:7: cannot find symbol
symbol: class HttpServlet
public class CustomQuery extends HttpServlet
^
CustomQuery.java:10: cannot find symbol
symbol : class ServletException
location: class CustomQuery
public void init() throws ServletException
^
CustomQuery.java:16: cannot find symbol
symbol : class HttpServletRequest
location: class CustomQuery
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
^
CustomQuery.java:16: cannot find symbol
symbol : class HttpServletResponse
location: class CustomQuery
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
^
CustomQuery.java:16: cannot find symbol
symbol : class ServletException
location: class CustomQuery
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
^
CustomQuery.java:12: non-static method establishConnection() cannot be referenced from a static context
EstablishDBConnetion.establishConnection();
^
CustomQuery.java:19: package response does not exist
PrintWriter printer = new response.getWriter();
^
CustomQuery.java:23: cannot find symbol
symbol : variable establishConnection
location: class EstablishDBConnetion
Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();
^
CustomQuery.java:40: cannot find symbol
symbol : variable establishConnection
location: class EstablishDBConnetion
EstablishDBConnetion.establishConnection.con.close();
^
11 errors



Code

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

public class CustomQuery extends HttpServlet
{

	public void init() throws ServletException
	{
		EstablishDBConnetion.establishConnection();
		
	}

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

		String title = "Result from custom query";

		Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();

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

		rs.close();
		stmt.close();
		EstablishDBConnetion.establishConnection.con.close();
	}

}

db Connection code
import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;

class EstablishDBConnetion
{

   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;
			Connection con = DriverManager.getConnection(url, dbUser, dbPassword);

			
		}

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

			e.printStackTrace();
		}

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

}
paulchwd is offline   Reply With Quote
Old Mar 3rd, 2007, 5:29 AM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by paulchwd View Post
CustomQuery.java:4: package javax.servlet does not exist
import javax.servlet.*;
Check your classpath. Also, what are you using to compile the code?
Arevos is offline   Reply With Quote
Old Mar 3rd, 2007, 11:25 AM   #4
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
AS far as I understand the classpath should have :

., my development directory and the location of the servlet jar file.

X:\Documents and Settings\Paul\My Documents\java\Code\servlets\invoice" is the exact directory where my class and java files are.

To compile I am using the command line (javac command)

Any thoughts?
paulchwd is offline   Reply With Quote
Old Mar 3rd, 2007, 1:41 PM   #5
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Updated

I ensured my Classpath is correct, and it still wont work, but if i use the -classpath option at the commandline when compiling it recognizes that i am importing the servlet packages, but i still get these errors:


CustomQuery.java:19: package response does not exist
PrintWriter printer = new response.getWriter();
^
CustomQuery.java:24: cannot find symbol
symbol : variable establishConnection
location: class EstablishDBConnetion
Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();
^
CustomQuery.java:39: cannot find symbol
symbol : variable establishConnection
location: class EstablishDBConnetion
EstablishDBConnetion.establishConnection.con.close();
^
3 errors


For starters, it doesn't understand what the response object is
and then it doesxnt like the way i call Statement stmt = EstablishDBConnetion.establishConnection.con.createStatement();,

(FYI: con is in the establishConnection method of the EstablishDBConnetion class)
paulchwd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Office Problems NightShade01 Coder's Corner Lounge 11 Nov 30th, 2006 1:37 PM
jsp & servlet tutorials bae Java 1 Feb 10th, 2006 10:41 PM
problems loading 2 dlls in Delphi7 nico765 Delphi 0 Jan 7th, 2006 3:03 PM
2 problems on permutations alejandrito Assembly 0 Dec 15th, 2005 3:07 AM
New Switch, FTP Problems ViOLATiON Coder's Corner Lounge 6 Sep 13th, 2005 1:44 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:52 AM.

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