Servlet Using JBDC-ODBC

This servlet uses the JDBC-ODBC bridge to query an Access database running on the same PC as the Java Server. The database is presumed to already exist. In this "Hello, World" example, a single "canned" query is processed. A real application would include support for adding, deleting, searching on user-supplied data, updating, etc.

The components involved in this two-tier example are shown in the figure, below.

Two- tier architecture. Shows applet communicating with WWW sever and servlet. Applet forms separate connection with servlet, which contacts ODBC registry for particular database, loads driver, and interacts with dbms through it using SQL. Data packaged by servlet and returned to applet.

Servlet

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class jbsJDBCServlet extends HttpServlet {


//*****  Servlet access to data base


    public void doGet (HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException
	{

    String url   = "jdbc:odbc:jbsAddress";
    String query = "SELECT * FROM main "
        + "WHERE last = 'smith'";

        try {

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

            Connection con = DriverManager.getConnection ( url, "", "" );

            Statement stmt = con.createStatement ();

            ResultSet rs = stmt.executeQuery (query);

            printResultSet ( resp, rs );

            rs.close();
            stmt.close();
            con.close();

        }  // end try

        catch (SQLException ex) {
            while (ex != null) {  /*
                System.out.println ("SQL Exception:  " + ex.getMessage ());
                ex = ex.getNextException ();  */
            }  // end while
        }  // end catch SQLException

        catch (java.lang.Exception ex)
        { ; }

    }  // end doGet


    private void printResultSet ( HttpServletResponse resp, ResultSet rs )
        throws SQLException  {

    ServletOutputStream out;

        try  {

	        resp.setContentType("text/html");
	        out = resp.getOutputStream();

	        out.println("<html>");
	        out.println("<head><title>jbsServerThreaded</title></head>");
	        out.println("<body>");
	        out.println("<center><font color=AA0000>");
	        out.println("<h3>jbsJDBCServlet</h3>");
	        out.println("<h3>Data Retrieved:</h3>");
	        
		out.println("<table border='1'>");

            	int numCols = rs.getMetaData().getColumnCount ();
            	while ( rs.next() ) {
	            out.println("<tr>");
                    for (int i=1; i<=numCols; i++) {
                    	out.print("<td>" + rs.getString(i) + "</td>" );
                    }  // end for
                	out.println("</tr>");
            	}  // end while

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

	        out.println("</font></center>");
	        out.println("</body>");
	        out.println("</html>");
	        out.close();
	    }  // end try
        catch ( IOException except)  {
        }  // end catch

    }  // end returnHTML


}  // end jbsJDBCServlet

 

Run the servlet