Link to home
Start Free TrialLog in
Avatar of rahulkothari
rahulkothari

asked on

Array of different primitives ??

I have a JDBCWrapper class which has a executeStoredProc(String procName,Object[] arguments) method.
When i need to call a stored procedure...i call this method and pass all my arguments as an Object array. Ex. If i need to call a Stored Proc that adds two integer numbers (AddNum) , i would do the following.

executeStoredProc(AddNum,new Object[]{new Integer(5),new Integer(10)}).

The method would prepare the required callable statment with the required number of '?' and depending on the Object[] element types would do a setInt or setString,setFloat etc etc.

Now my problem is that somestimes i have 20 arguments , so i am creating 20 objects (and creating new instance is always expensive).

Is there any other way i could pass my arguments as an array and find out their type inside the method. I regularly use String,int,float.
Basically can we store primitives,String objects in the same Array kind of implementation ? Please suggest
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JakobA
JakobA

Nope. you must eiter wrap your primitives in objects, or else make  3 to the 20'eth different versions of your executeStoredProc method.

a fairly easy intermediate soulution would be to make a wrapThisPrimitive method that returned the parameter value in an object, you would only need one for each of int, String and float.
and then 20 different executeStoredProc methods with from 1 to 20 object parameters.

eg: your example call would become:  executeStoredProc( AddNum, wrap(5), wrap(10) )

There would be absolutely no runtime speed improvement though. rather the reverse.

regards JakobA
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I also wanted to add that if you are doing DB queries, the overhead for creating 20 objects is the least of your performance problems, especially if it is a remote database. In a user-driven application, it is completely ignorable. The objects will be created by the time the user has removed their finger from the mouse button.
I have exactly such a primitive wrapper, what it does better is to re-use elements so that there isn't too much memory wasted and it is also optimized to handle primitive nulls as wll.  If you wish to see the code tell me since its quite long :)
Avatar of rahulkothari

ASKER

doronb....let me see the code
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry.. a little change was in order... (old classes)

New constructor of Argument:

      protected Argument(Class objClass) {
            verifyClass(objClass);
      }

New createArrayObject(...) method for the Arguments class:

      private static Argument createArrayObject(Class objClass, Arguments args, int index) {
            Argument result = null;
            Object x = args.elementAt(index);
            if (x instanceof Argument) {
                  result = (Argument)x;
                  result.verifyClass(objClass);
            } else {
                  result = createArrayObject(objClass);
            }
            return result;
      }

Now, you can write a program like this:

      public static void main(String[] args) {
            // Create a new Arguments object..
            Arguments xArgs = new Arguments();
            // Add a boolean value..
            xArgs.addElement(true);
            // Add a String..
            xArgs.addElement("Hello");
            // Add an int..
            xArgs.addElement(1);
            // Get the objects out of the Arguments as an array of objects..
            Object[] o = xArgs.getArguments();
            // Go through all objects..
            for (int i = 0; i < o.length; i++) {
                  // and print each object..
                  System.out.println(o[i]);
            }
            // Replace the 'true' with 'false', you will notice, no new objects were created now!
            xArgs.setElementAt(false, 0);
            // Replace the String "Hello" with the int 2, we will create an array object to hold the new int value now..
            xArgs.setElementAt(2, 1);
            // Replace the int 1, with the String "GoodByte", we will discard the old array object in place of the new String..
            xArgs.setElementAt("GoodBye", 2);
            // Get the objects out of the Arguments as an array of objects..
            o = xArgs.getArguments();
            // Go through all objects..
            for (int i = 0; i < o.length; i++) {
                  // and print each object..
                  System.out.println(o[i]);
            }
      }

This little method produces the following output:

true
Hello
1
false
Hello
GoodBye
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And in my database applications, I have code like this one:

  public boolean insertCell(String cellName, String sgsnName, String routingArea, String caDomain, String state) {
    boolean result = false;
    String query = "InsertCell";
    SQLArguments sqlArgs = fetchSqlArguments(query);
    sqlArgs.putElement(cellName);
    sqlArgs.putElement(sgsnName);
    sqlArgs.putElement(routingArea);
    sqlArgs.putElement(caDomain);
    sqlArgs.putElement(state);
    result = executeUpdate(query, sqlArgs) > 0;
    databaseChanged(result, true);
    return result;
  }

  public boolean deleteCell(String cellName) {
    boolean result = false;
    String query = "DeleteCell";
    SQLArguments sqlArgs = fetchSqlArguments(query);
    sqlArgs.putElement(cellName);
    result = executeUpdate(query, sqlArgs) > 0;
    databaseChanged(result, true);
    return result;
  }

  public boolean updateCell(String cellName, String sgsnName, String caDomain, String state) {
    String query = "UpdateCell";
    SQLArguments sqlArgs = fetchSqlArguments(query);
    sqlArgs.putElement(sgsnName);
    sqlArgs.putElement(caDomain);
    sqlArgs.putElement(state);
    sqlArgs.putElement(cellName);
    boolean result = executeUpdate(query, sqlArgs) > 0;
    databaseChanged(result, true);
    return result;
  }

Please note that the putElement(..) method in SQLArguments knows whether to use addElement(..) or setElementAt(..) :)

Hope this helps,
Doron
PAQ and Point Split.
No objections here :)