This servlet uses the JDataConnect type 3 driver to query an SQL database running on a different host from the machine where the servlet is running.
The components in this three-tier example are shown in the figure, below.
Three- tier architecture. Shows applet communicating with WWW sever and servlet. Applet forms separate connection with servlet, which contacts ODBC registry for particular database running on a different machine, loads driver, and interacts with remote dbms through the driver 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 jbsJDCServlet extends HttpServlet { //***** Servlet access to data base //***** through JDataConnect type 3 driver, on servlet host //***** connecting to SQL server, on different host public void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String url = "jdbc:JDataConnect://jbspc-cs.cs.unc.edu/jbsPlantsSQL"; String query = "SELECT * FROM PlantsSQL.dbo.plants"; try { Class.forName ("JData1_2.sql.$Driver"); 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>jbsJDCServlet</title></head>"); out.println("<body>"); out.println("<center><font color=AA0000>"); out.println("<h3>jbs JDataConnect Servlet</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 jbsJDCServlet