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,n ew 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
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,n
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
ASKER
doronb....let me see the code
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(objClas s);
} 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("GoodBy e", 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
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(objClas
} 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("GoodBy
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(cellNam e);
sqlArgs.putElement(sgsnNam e);
sqlArgs.putElement(routing Area);
sqlArgs.putElement(caDomai n);
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(cellNam e);
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(sgsnNam e);
sqlArgs.putElement(caDomai n);
sqlArgs.putElement(state);
sqlArgs.putElement(cellNam e);
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
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(cellNam
sqlArgs.putElement(sgsnNam
sqlArgs.putElement(routing
sqlArgs.putElement(caDomai
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(cellNam
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(sgsnNam
sqlArgs.putElement(caDomai
sqlArgs.putElement(state);
sqlArgs.putElement(cellNam
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 :)
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