Разработка контента курса дистанционного обучения "DB2 универсальная база данных", страница 44

public class ReplicatorBean implements SessionBean {

    private SessionContext sessionContext;

    public void ejbCreate() throws CreateException {}

    public void ejbRemove() {}

    public void ejbActivate() {}

    public void ejbPassivate() {}

    public void setSessionContext(SessionContext ctx){

        sessionContext = ctx;

    }

    public int proceedReplication(ArrayList rows){

      int returnMe = 0;

        try{

            Context ctx = new InitialContext();

            Object ref = ctx.lookup("DOROGADB");

                  DataSource ds = (DataSource)ref;

            Connection conn = ds.getConnection();

            Statement stm = conn.createStatement();

            for(int i = 0; i < rows.size(); i++){

                Row row = (Row)rows.get(i);

                try {

                    stm.executeUpdate(row.sql);

                } catch(SQLException sqlEx){

                    ((Row)rows.get(i)).sqlCode = sqlEx.getErrorCode();

                }

                String sql = "INSERT INTO REPL_LOG VALUES('"+row.timeStamp+"', '"+row.id+"', "+row.sqlCode+")";

                try {

                    stm.executeUpdate(sql);

                } catch(SQLException sqlex){

                    System.out.println("ReplicatorBean logging SQL error: "+sqlex.getMessage()+", SQLCODE:"+sqlex.getErrorCode());

                    returnMe = 1;

                }

            }

            stm.close();

        } catch(NamingException nex){

            System.out.println("ReplicatorBean JNDI error: "+nex.toString(true));

            returnMe = 2;

        } catch(SQLException sqlex){

            System.out.println("ReplicatorBean SQL error duaring getting db connection: "+sqlex.getMessage()+", SQLCODE:"+sqlex.getErrorCode());

            returnMe = 2;

        }

        return returnMe;

    }

}

Интерфейсы

ReplicatorHome.java

package courseExample;

import javax.ejb.EJBHome;

import javax.ejb.CreateException;

import java.rmi.RemoteException;

public interface ReplicatorHome extends EJBHome {

    public Replicator create() throws CreateException, RemoteException;

}

Replicator.java

package courseExample;

import javax.ejb.EJBObject;

import java.util.ArrayList;

import java.rmi.RemoteException;

public interface Replicator extends EJBObject {

    public int proceedReplication(ArrayList rows) throws RemoteException;

}

Классы

Row.java

package courseExample;

import java.io.Serializable;

import java.util.HashMap;

public class Row implements Serializable {

    public String timeStamp = null;

    public String id = null;

    public String sql = null;

    public int sqlCode = 0;

    public String tableName = null;

    public String schemaName = null;

    public String sqlOperation = null;

    public HashMap keys = new HashMap();

    public HashMap keyTypes = new HashMap();

    public HashMap columns = new HashMap();

    public HashMap columnTypes = new HashMap();

    public static void buildSql(Row row){

        String temp = null;

        if(row.sqlOperation.equals("INSERT")){

            temp = "INSERT INTO ";

            if(row.schemaName != null){

                temp += row.schemaName+".";

            }

            temp += row.tableName + " (";

            Object[] colList = row.columns.keySet().toArray();

            for(int i = 0; i < colList.length; i++){

                temp += (String)colList[i];

                if(i < colList.length-1){

                    temp += ", ";

                }

            }

            temp += ") VALUES (";

            for(int i = 0; i < colList.length; i++){

                String value = (String)row.columns.get(colList[i]);

                if(value == null){

                    temp += "NULL";

                } else {