0

I'm writing log system for Spring MVC. I will send userId to procedure.

public Object execProc(String storedProcedure, RowMapper rowMapper,
    Object... args)

I need to insert element head of args, how can I do? Ex.

int userId = 9;
args.push(userId)
egemen
  • 779
  • 12
  • 23

2 Answers2

2

If you simply wish to add a new element as the head of the array an easy way is to create a new array and copy the elements using System.arraycopy. Then the new head-element can be added to index 0 as in the example below.

int userId = 9;
Object[] args = new Object[]{"a", "list", "of", "args"}; // the "old" array
Object[] theNewArray = new Object[args.length + 1]; // a new array, 1 element bigger
System.arraycopy(args, 0, theNewArray, 1, args.length); // copy everything
theNewArray[0] = userId; // and insert you head element

// From now on, use "theNewArray"
wassgren
  • 18,651
  • 6
  • 63
  • 77
0

The method execProc has vararg

execProc("your stored procedure here", myRowMapper, userId, firstArg, secondArg, ...);

If you need to merge your parameters before calling the method, see https://stackoverflow.com/a/80559/834

Community
  • 1
  • 1
cringe
  • 13,401
  • 15
  • 69
  • 102
  • i get userId from Session, And I have more than 200 procedure which I had already written. I want to add into head of args. – egemen Dec 20 '14 at 18:54