Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Servlet not showing JDBC database data

843841Aug 24 2006 — edited Aug 24 2006
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 21 2006
Added on Aug 24 2006
2 comments
388 views