JDBC Connections

Top  Previous  Next

org.moremotion.util.JdbcConnection class provides static methods to obtain JDBC connections in a practical way. It is enough just to pass the MoreMotionRequest object and the name of a JDBC connection configuration (jdbcConnection element).

Connection con = org.moremotion.util.JdbcConnection.getConnection(requestconnection-name);        

 

request

MoreMotionRequest Object

connection-name

The name of the configuration element defined with jdbcConnection in the MoreMotion configuration.
 
Example:

  <jdbcConnection name="SuperStore">
    <jndids></jndids>
    <driver>org.gjt.mm.mysql.Driver</driver>
    <dburl>jdbc:mysql://localhost:3306/superstore</dburl>
    <user>root</user>
    <password>mmsa</password>
  </jdbcConnection>

If jndids element was defined in the connection configuration, the getConnection method first tries to get the connection through JNDI, if it fails it then loads the JDBC Driver Class (driver element) and gets the connection from the Driver Manager.

 

JNDI

During the start up, MoreMotion creates an InitialContext object to access JNDI definitions (if defined any) through it. If there is a file named {APPLICATION_PATH}/WEB-INF/MM-INF/jndi.properties, the InitialContext is constructed using it as follows.

  IntialContext ic;
  File f = new File(mmHomeFile + "/jndi.properties");
  if (f.exists()) {
    Properties props = new Properties();
    props.load( new java.io.FileInputStream(f) );
    ic = new InitialContext(props);
  } else {
    ic = new InitialContext();
  }  

 

 

Example:

 

  package mypack;

 

  import org.moremotion.servlet.*;

  import org.moremotion.action.*;

  import org.moremotion.adom.*;

  import org.moremotion.util.*;

  import java.sql.*;

 

  public class ProductList implements org.moremotion.action.ActionService {

  

      public void doService(ActionServiceContext asc) 

      throws ServiceException, java.io.IOException {

    

      MoreMotionRequest request = asc.getRequest();

 

      String tableName = request.getParameter("TABLE_NAME","products");

      double resellerDiscount = request.getParameterAsDouble("RESELLER_DISCOUNT",0);

    

      // Create the "products" ADOM.

      ADOM products = request.newRequestADOM("products");

    

      Connection conn = null;

      try {

 

        /* Get a JDBC connection from MoreMotion. "SuperStore" is the 

         * name of the connection configuration defined with "jdbcConnection"

         * element. */

        conn = JdbcConnection.getConnection(request, "SuperStore");

      

        Statement stmt = conn.createStatement();

        String sql = "select * from " + tableName;

        ResultSet rs = stmt.executeQuery(sql);

 

        while (rs.next()) {

          String productId = rs.getString("ID");

          String productName = rs.getString("NAME");

          double unitPrice = rs.getDouble("UNIT_PRICE");

          double resellerPrice = (1 - (resellerDiscount / 100)) * unitPrice;

        

          ADOMNode item = new ADOMNode("item");

          products.addNode(item);

 

          item.setNodeValue("ID", urunNo);

          item.setNodeValue("NAME", urunAdi);

          item.setNodeValue("UNIT_PRICE", unitPrice + "");

          item.setNodeValue("RESELLER_PRICE", resellerPrice + "");

        }

      

        rs.close();

        stmt.close();

        /* conn.close();

         * Do not close the connection since it may be reused by other units.

         * Before the current request terminates the MoreMotion closes 

         * all the connections acquired using 

         * org.moremotion.util.JdbcConnection.getConnection() method.

         */

      } catch(Exception e) {

        e.printStackTrace();

      }

    

      /* Generate "products" page

      request.generatePage("products");

    }

  

  }