View Single Post
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