JDBC for postgresql db
843854Apr 20 2004 — edited Apr 21 2004I working on a project for school, where I need to create a servlet that will display the contents of a small database table, when activated by an html page. I also need to allow the user to update the database, and do a few queries.
My problem is that I cannot even get my java program to do a select *, because I keep getting the following error, "java.lang.ClassNotFoundException: No ClassLoaders found for: org.postgresql.Driver "
Our class is sharing a postgresql database entitled testdb, we were each asked to produce our own table for this assignment, mine is entitled books, and we're using JBOSS 3-2-3.
When the database was installed and configured, the administrator should have set the class path, and environment variables, right?
I don't know what do do next, this is what my code looks like:
import java.net.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Books extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
// defining my strings
String genre, author, query, username = "", password = "";
// getting data from my web-page
res.setContentType("text/html");
PrintWriter out = res.getWriter();
genre = req.getParameter("genre");
try
{
// connecting to the database
String url = "jdbc:postgresql:testdb";
Class.forName("org.postgresql.Driver");
Connection db_connection = DriverManager.getConnection (url, username, password);
// creating the query statement
Statement db_statement = db_connection.createStatement();
query = "SELECT * FROM books";
ResultSet result = db_statement.executeQuery(query);
// formatting the output
out.println("<html>");
out.println("<head>");
out.println("<title>Welcome to Eric's Book Depository</title>");
out.println("</head>");
out.println("<body>");
out.println("<center>");
while(result.next())
{
out.println(result.getString("BookName") + "<br>");
}
db_statement.close();
db_connection.close();
out.println("</center>");
out.println("</body>");
out.println("</html>");
}
catch (ClassNotFoundException cnfe) // driver not found
{
out.println("<html>");
out.println("<head><title>Error Log</title></head>");
out.println("<body>");
out.println ("Unable to load database driver" + "<br>");
out.println ("Details : " + cnfe);
out.println("</body></html>");
}
catch (java.sql.SQLException sqle)
{
out.println("<html>");
out.println("<head><title>Error Log</title></head>");
out.println("<body>");
out.println("Error with SQL statements: " + sqle +"<br>");
out.println("</body></html>");
}
}
}
Any help would be greatly appreciated.
Growler