can't access database from servlet

From: Sanjeev Rathore <intmail2002(at)yahoo(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: can't access database from servlet
Date: 2001-12-29 04:47:05
Message-ID: 20011229044705.73022.qmail@web21207.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Iam trying to access a table from PostgreSQL 7.1.3
from servlet.

These are the software that I am running.
Tomcat 4.0
PostgreSQL 7.1.3
RedHat 7.2

I am able to access PostgreSQL database from stand
alone Java program.
I am able to access meta data of PostgreSQL from
servlet, which means(i think) that JDBC driver must be
working. I am able to access database called 'auction'
and table called 'names' from a sql command prompt
which has one row of record. When servlet is executed
ResultSet returns null when it shoud return result of
SQL query.

I don't know where the problem could be but it seems
that Java code or some type of permission is
incorrect. Here is my simple Servlet code that
compiles but gives ResultSet as null.

please help!
I am working on this problem for about 4 days now.

Thanks,
Sanjeev

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

public class HelloServlet extends HttpServlet {
Connection db; //connection to database
Statement sql; //sql statement to query database
DatabaseMetaData dbmd; //find out the drive
version
String first_name = "blank";
String last_name = " ";
ResultSet result;

public void init(ServletConfig config) throws
ServletException {
super.init(config);
String database = "auction";
String username = "postgres";
String password = "postgres";
try{
Class.forName("org.postgresql.Driver");
db =
DriverManager.getConnection("jdbc:postgresql:" +
database, username, password);
dbmd = db.getMetaData();
sql = db.createStatement();
String sqlText = "select first_name from
names";
result = sql.executeQuery(sqlText);
while(result.next()){
first_name =
result.getString(first_name);
last_name = result.getString(last_name);
}
}catch(Exception e){
e.printStackTrace();
//Connection = null;
}
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Sanjeev! </h1>");
//HelloServlet hs = new HelloServlet();

out.println("<h4>" + last_name + "</h4>");
try{
out.println("<h4>" +
dbmd.getDatabaseProductName() + "</h4>");
out.println("<h2>" +
dbmd.getDatabaseProductVersion() + "</h2>");
out.println("<h4>" + result + "</h4>");
}catch(Exception e){
System.out.println("bad things happened");
}
out.println("</body>");
out.println("</html>");
}
} //end of HelloServlet

__________________________________________________
Do You Yahoo!?
Send your FREE holiday greetings online!
http://greetings.yahoo.com

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Vernon Wu 2001-12-29 04:59:22 Unable complie jdbc
Previous Message Mihai Gheorghiu 2001-12-28 14:15:36 Re: Connection pooling