cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blorit...@apache.org
Subject cvs commit: xml-cocoon/src/org/apache/cocoon/components/datasource JdbcConnection.java
Date Thu, 11 Jan 2001 21:01:46 GMT
bloritsch    01/01/11 13:01:46

  Modified:    src/org/apache/cocoon/acting Tag: xml-cocoon2
                        AddEmployeeAction.java DelEmployeeAction.java
                        UpdEmployeeAction.java
               src/org/apache/cocoon/components/datasource Tag: xml-cocoon2
                        JdbcConnection.java
  Log:
  Added implementations for Update and Delete actions...
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.6   +7 -1      xml-cocoon/src/org/apache/cocoon/acting/Attic/AddEmployeeAction.java
  
  Index: AddEmployeeAction.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/acting/Attic/AddEmployeeAction.java,v
  retrieving revision 1.1.2.5
  retrieving revision 1.1.2.6
  diff -u -r1.1.2.5 -r1.1.2.6
  --- AddEmployeeAction.java	2001/01/10 22:07:00	1.1.2.5
  +++ AddEmployeeAction.java	2001/01/11 21:01:41	1.1.2.6
  @@ -34,7 +34,7 @@
   /**
    *
    * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1.2.5 $ $Date: 2001/01/10 22:07:00 $
  + * @version CVS $Revision: 1.1.2.6 $ $Date: 2001/01/11 21:01:41 $
    */
   public class AddEmployeeAction extends ComposerAction {
   
  @@ -100,6 +100,12 @@
                   conn.rollback();
               }
           } catch (SQLException se) {
  +            try {
  +                conn.rollback();
  +            } catch (SQLException sse) {
  +                log.error("Caught an exception trying to roll back transaction", sse);
  +            }
  +
               log.error("There was a SQL error", se);
           } finally {
               try {
  
  
  
  1.1.2.3   +63 -2     xml-cocoon/src/org/apache/cocoon/acting/Attic/DelEmployeeAction.java
  
  Index: DelEmployeeAction.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/acting/Attic/DelEmployeeAction.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- DelEmployeeAction.java	2000/12/30 23:01:01	1.1.2.2
  +++ DelEmployeeAction.java	2001/01/11 21:01:42	1.1.2.3
  @@ -7,6 +7,9 @@
    *****************************************************************************/
   package org.apache.cocoon.acting;
   
  +import java.sql.Connection;
  +import java.sql.PreparedStatement;
  +import java.sql.SQLException;
   import java.util.Map;
   
   import javax.servlet.http.HttpServletRequest;
  @@ -14,10 +17,15 @@
   import javax.servlet.ServletContext;
   
   import org.apache.avalon.Component;
  +import org.apache.avalon.ComponentSelector;
  +import org.apache.avalon.ComponentManagerException;
   import org.apache.avalon.Configuration;
   import org.apache.avalon.ConfigurationException;
   import org.apache.avalon.Parameters;
   
  +import org.apache.cocoon.Roles;
  +import org.apache.cocoon.components.datasource.DataSourceComponent;
  +
   import org.xml.sax.SAXException;
   import org.xml.sax.EntityResolver;
   
  @@ -25,14 +33,25 @@
   /**
    *
    * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1.2.2 $ $Date: 2000/12/30 23:01:01 $
  + * @version CVS $Revision: 1.1.2.3 $ $Date: 2001/01/11 21:01:42 $
    */
   public class DelEmployeeAction extends ComposerAction {
   
  +    DataSourceComponent datasource = null;
  +
       /**
        * Get the <code>Configuration</code> object for this <code>Component</code>
        */
       public void configure( Configuration configuration) throws ConfigurationException {
  +        Configuration connElement = configuration.getChild("use-connection");
  +
  +        try {
  +            ComponentSelector selector = (ComponentSelector) this.manager.lookup(Roles.DB_CONNECTION);
  +            this.datasource = (DataSourceComponent) selector.select(connElement.getValue());
  +        } catch (ComponentManagerException cme) {
  +            log.error("Could not get the DataSourceComponent", cme);
  +            throw new ConfigurationException("Could not get the DataSource Component",
cme);
  +        }
       }
   
       /**
  @@ -41,8 +60,50 @@
        */
       public Map act (EntityResolver resolver, Map objectModel, String src, Parameters par)
throws Exception {
           HttpServletRequest req = (HttpServletRequest) objectModel.get(Constants.REQUEST_OBJECT);
  -        req.setAttribute("message", "You have deleted the employee " + req.getParameter("name"));
  +        String id = req.getParameter("employee");
  +        String name = req.getParameter("name");
  +
  +        if (deleteEmployee(id) == true) {
  +            req.setAttribute("message", "You have deleted the employee " + name);
  +        } else {
  +            req.setAttribute("message", "You did not delete the employee " + name);
  +        }
           return null;
  +    }
  +
  +    private boolean deleteEmployee(String id) {
  +        Connection conn = null;
  +        PreparedStatement ps = null;
  +        boolean returnValue = true;
  +
  +        try {
  +            conn = datasource.getConnection();
  +            conn.setAutoCommit(false);
  +
  +            ps = conn.prepareStatement("DELETE FROM employee_table WHERE (id = ?)");
  +            ps.setString(1, id);
  +
  +            ps.executeUpdate();
  +            returnValue = true;
  +            conn.commit();
  +        } catch (SQLException se) {
  +            try {
  +                conn.rollback();
  +            } catch (SQLException sse) {
  +                log.error("Caught an exception trying to roll back transaction", sse);
  +            }
  +
  +            log.error("There was a SQL error", se);
  +        } finally {
  +            try {
  +                if (ps != null) ps.close();
  +                conn.close();
  +            } catch (Exception e) {
  +                log.error("We should never be in this clause", e);
  +            }
  +        }
  +
  +        return returnValue;
       }
   }
   
  
  
  
  1.1.2.3   +66 -2     xml-cocoon/src/org/apache/cocoon/acting/Attic/UpdEmployeeAction.java
  
  Index: UpdEmployeeAction.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/acting/Attic/UpdEmployeeAction.java,v
  retrieving revision 1.1.2.2
  retrieving revision 1.1.2.3
  diff -u -r1.1.2.2 -r1.1.2.3
  --- UpdEmployeeAction.java	2000/12/30 23:01:01	1.1.2.2
  +++ UpdEmployeeAction.java	2001/01/11 21:01:42	1.1.2.3
  @@ -7,6 +7,9 @@
    *****************************************************************************/
   package org.apache.cocoon.acting;
   
  +import java.sql.Connection;
  +import java.sql.PreparedStatement;
  +import java.sql.SQLException;
   import java.util.Map;
   
   import javax.servlet.http.HttpServletRequest;
  @@ -14,10 +17,15 @@
   import javax.servlet.ServletContext;
   
   import org.apache.avalon.Component;
  +import org.apache.avalon.ComponentSelector;
  +import org.apache.avalon.ComponentManagerException;
   import org.apache.avalon.Configuration;
   import org.apache.avalon.ConfigurationException;
   import org.apache.avalon.Parameters;
   
  +import org.apache.cocoon.Roles;
  +import org.apache.cocoon.components.datasource.DataSourceComponent;
  +
   import org.xml.sax.SAXException;
   import org.xml.sax.EntityResolver;
   
  @@ -25,14 +33,25 @@
   /**
    *
    * @author <a href="mailto:Giacomo.Pati@pwr.ch">Giacomo Pati</a>
  - * @version CVS $Revision: 1.1.2.2 $ $Date: 2000/12/30 23:01:01 $
  + * @version CVS $Revision: 1.1.2.3 $ $Date: 2001/01/11 21:01:42 $
    */
   public class UpdEmployeeAction extends ComposerAction {
   
  +    DataSourceComponent datasource = null;
  +
       /**
        * Get the <code>Configuration</code> object for this <code>Component</code>
        */
       public void configure( Configuration configuration) throws ConfigurationException {
  +        Configuration connElement = configuration.getChild("use-connection");
  +
  +        try {
  +            ComponentSelector selector = (ComponentSelector) this.manager.lookup(Roles.DB_CONNECTION);
  +            this.datasource = (DataSourceComponent) selector.select(connElement.getValue());
  +        } catch (ComponentManagerException cme) {
  +            log.error("Could not get the DataSourceComponent", cme);
  +            throw new ConfigurationException("Could not get the DataSource Component",
cme);
  +        }
       }
   
       /**
  @@ -41,8 +60,53 @@
        */
       public Map act (EntityResolver resolver, Map objectModel, String src, Parameters par)
throws Exception {
           HttpServletRequest req = (HttpServletRequest) objectModel.get(Constants.REQUEST_OBJECT);
  -        req.setAttribute("message", "You have update the employee " + req.getParameter("name"));
  +        String id = req.getParameter("employee");
  +        String name = req.getParameter("name");
  +        String department = req.getParameter("department")
  +
  +        if (updateEmployee(id, name, department) == true) {
  +            req.setAttribute("message", "You have updated the employee " + name);
  +        } else {
  +            req.setAttribute("message", "You did not update the employee " + name);
  +        }
           return null;
  +    }
  +
  +    private boolean updateEmployee(String id, String name, String department) {
  +        Connection conn = null;
  +        PreparedStatement ps = null;
  +        boolean returnValue = true;
  +
  +        try {
  +            conn = datasource.getConnection();
  +            conn.setAutoCommit(false);
  +
  +            ps = conn.prepareStatement("UPDATE employee_table SET id = ?, name = ?, department_id
= ?");
  +            ps.setString(1, id);
  +            ps.setString(2, name);
  +            ps.setString(3, department);
  +
  +            ps.executeUpdate();
  +            returnValue = true;
  +            conn.commit();
  +        } catch (SQLException se) {
  +            try {
  +                conn.rollback();
  +            } catch (SQLException sse) {
  +                log.error("Caught an exception trying to roll back transaction", sse);
  +            }
  +
  +            log.error("There was a SQL error", se);
  +        } finally {
  +            try {
  +                if (ps != null) ps.close();
  +                conn.close();
  +            } catch (Exception e) {
  +                log.error("We should never be in this clause", e);
  +            }
  +        }
  +
  +        return returnValue;
       }
   }
   
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.1.2.2   +2 -1      xml-cocoon/src/org/apache/cocoon/components/datasource/Attic/JdbcConnection.java
  
  Index: JdbcConnection.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon/src/org/apache/cocoon/components/datasource/Attic/JdbcConnection.java,v
  retrieving revision 1.1.2.1
  retrieving revision 1.1.2.2
  diff -u -r1.1.2.1 -r1.1.2.2
  --- JdbcConnection.java	2001/01/08 20:25:43	1.1.2.1
  +++ JdbcConnection.java	2001/01/11 21:01:45	1.1.2.2
  @@ -31,7 +31,7 @@
    * total number of Connection objects that are created.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  - * @version CVS $Revision: 1.1.2.1 $ $Date: 2001/01/08 20:25:43 $
  + * @version CVS $Revision: 1.1.2.2 $ $Date: 2001/01/11 21:01:45 $
    */
   public class JdbcConnection implements Connection, Recyclable {
       private Connection conn;
  @@ -67,6 +67,7 @@
           this.conn.rollback();
       }
       public void close() throws SQLException {
  +        this.setAutoCommit(false);
           this.pool.put(this);
       }
       public void recycle() {
  
  
  

Mime
View raw message