Servlet not showing JDBC database data

Hi all

For some reason my servlet isn't showing my data from an sql database onto a web page. I've checked against an example on a jdbc web site and also on a servlet book and the code appears to be correct.

: -

package website;

import java.io.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class ProductServlet extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

res.setContentType("text/html");

PrintWriter out = res.getWriter();

try {

// Load (and therefore register) the Oracle Driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

// Get a Connection to the database

con = DriverManager.getConnection(

"jdbc:odbc:products", "", "");

// Create a Statement object

stmt = con.createStatement();

// Execute an SQL query, get a ResultSet

rs = stmt.executeQuery("SELECT TITLE, DESCRIPTION FROM PRODUCTS");

// Display the result set as a list

out.println("<HTML><HEAD><TITLE>Products</TITLE></HEAD>");

out.println("<BODY>");

out.println("<UL>");

while(rs.next()) {

out.println("<LI>" + rs.getString("title") + " " + rs.getString("description"));

}

out.println("</UL>");

out.println("</BODY></HTML>");

}

catch(ClassNotFoundException e) {

out.println("Couldn't load database driver: " + e.getMessage());

}

catch(SQLException e) {

out.println("SQLException caught: " + e.getMessage());

}

finally {

// Always close the database connection.

try {

if (con != null) con.close();

}

catch (SQLException ignored) { }

}

}

}

HTML web page:

<form action ="/servlet/website.ProductServlet" method="post">

Any help would be very much appreciated.

[2108 byte] By [Emmaqa] at [2007-10-3 3:29:56]
# 1
what type of daba base you are ussing? MySQL?
jesusmgmarina at 2007-7-14 21:23:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

[nobr]I have a servlet for MySQL:

import java.io.*;

import java.net.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.lang.*;

import java.sql.*;

public class Confirm extends HttpServlet {

public void init(ServletConfig config) throws ServletException{

super.init(config);

}

protected void doGet(HttpServletRequest peticion, HttpServletResponse respuesta) throws ServletException, IOException {

doPost(peticion, respuesta);

}

protected void doPost(HttpServletRequest peticion, HttpServletResponse respuesta) throws ServletException, IOException {

.....

respuesta.setContentType("text/html");

PrintWriter out = respuesta.getWriter();

Connection dbconn;

ResultSet results;

PreparedStatement sql;

try

{

//Class.forName("con.mysql.jdbc.Driver").newInstance();

Class.forName("org.gjt.mm.mysql.Driver");

try

{

dbconn = DriverManager.getConnection("jdbc:mysql://localhost/NAMEOFTHEDATABASE?user=USERNAME&password=USERPASSWORD");

String consulta = "SELECT * FROM myTable";

sql = dbconn.prepareStatement(consulta);

results = sql.executeQuery();

}

catch (SQLException s)

{

out.println("SQL Error<br>");

}

}

catch (ClassNotFoundException err)

{

out.println("Class loading error");

}

out.println ("</body></html>");

out.close();

}

I hope it helps you[/nobr]

jesusmgmarina at 2007-7-14 21:23:46 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...