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