Using Resin s JNDI Configuration


Traditional JDBC Connectivity

Before we dive into the details of what Resin brings to the table for advanced database connectivity, let's look at the traditional way a Java application connects to and manipulates a database. Listing 11.1 shows a simple application for communicating with a MySQL database.

Listing 11.1: Traditional JDBC MySQL connectivity.

start example
 package servlet; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Traditional extends HttpServlet {   public void doGet(HttpServletRequest inRequest,    HttpServletResponse outResponse)     throws ServletException, IOException {     Connection myConnection = null;     Statement myStatement = null;     ResultSet myResultSet = null;     outResponse.setContentType("test/html");     PrintWriter out = outResponse.getWriter();     try {       Class.forName("com.mysql.jdbc.Driver");       myConnection = DriverManager.getConnection(         "jdbc:mysql-caucho://192.168.1.25/products?user-spider          &password=spider");       myStatement = myConnection.createStatement();       myResultSet = myStatement.executeQuery("SELECT title,         price FROM product");       out.println("<HTML><HEAD><TITLE>MySQL Example</TITLE></HEAD>");       out.println("<BODY>");       out.println("<UL>");       while (myResultSet.next()) {         out.println("<LI>" + myResultSet.getString("title") + " "           + myResultSet.getString("price"));       }       out.println("</UL>");       out.println("</BODY></HTML>");     } catch(ClassNotFoundException e) {   out.println("Couldn't load driver"); } catch(SQLException e) {   out.println("SQLException catught: " + e.getMessage()); } finally {   myConnection.close(); }   } } 
end example

Before we move to Resin specifics, let's discuss this traditional JDBC code. On the local test network is a Linux machine at IP 192.168.1.75 running MySQL. MySQL has a database called products. This database contains a small table called product that has two fields: title and price.

The code in Listing 11.1 connects to the database, pulls the title and price information, and displays the information to the user. To make the connection to the database, you must have a JDBC driver specifically for MySQL. The primary MySQL JDBC driver is called MySQL Connector/J (formerly called MM.MYSQL) which is available directly from MySQL AB at www.mysql.com/products/connector-j.

When the servlet is executed, the first JDBC-oriented statement is a call to load the JDBC driver. If the class or respective JAR file isn't found, an exception is thrown, indicating the error. Once the driver is pulled into the address space of the application, a connection can be made to the MySQL database. The connection string is defined as follows:

 interface:driver name:ip/database<options> 

In the code, the fully defined string is

 jdbc:mysql://192.168.1.25/products?user=spider&password=spider 

The connection to the database occurs with the getConnection() method of the DriverManager static class. Once the connection is established, SQL can pull the necessary information.

What makes JDBC so powerful is the ability to change from MySQL to Oracle or any other JDBC-compliant driver. Of course, the code in this example would require you to change the driver class name and the connection string in the source code and recompile the code each time you use a new driver. This problem isn't too significant when you consider that Resin automatically recompiles source code that has changed on the server; however, constantly changing source code can lead to problems. Fortunately, Resin has a better solution.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net