• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get a string of parameters from a http servlet and pass it to stored procedure

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your email last time. I got one last question.
I am getting a QueryString from a web page to this SErvlet, different querySTrings will
be generated depending on the different user responses in the webpage. My job in this
servlet is to get the list of parameters and pass them to stored procedures.,
i won't be sure of the parameter names, so i am getting parameters names and values like
this in the Servlet.
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements()) {
String name = (String) enum.nextElement();
String values[] = req.getParameterValues(name);

if (values != null) {
for (int i=0; i<values.length; i++) {

out.println(name + " = " + values[i] + "<BR>" );

}
}
}
I am able to print parameter names and parameter values. But what i want is to collect
the parameter values and pass them to a Stored procedure.
If i collect them in while loop, then i am unable to use them outside the while loop.
all i need is a string of parameter values will comma separated. and pass them to stored
procedure.
I am calling stored procedure like this
try{
CallableStatement cs = dbconn.prepareCall("{Call " + proc_name + "(?,?)}" );
}
catch(SQLException e)
{
System.out.println("GOT SOME SQLEXCEPTION" + e.getMessage());
}

cs.execute;
My question.
1. i am unable to collect the parameter values in a STring.
2. how to pass them to stored PRocedure
can u suggest me something
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you said: "If i collect them in while loop, then i am unable to use them outside the while loop."

That's only if you declare your variable inside the loop! Variables have block scope. Declare your array outside the while loop, populate inside the loop, and it's available 'outside' the loop.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been thinking more about this.

If you're using a stored proc, then you *must* know something of the nature of your request. The stored proc is expecting a certain number of parameters, so even if you don't know parameter names, you must know their number, right?

If this is form processing, how would you handle a textfield left blank? This is not sent on the query string. What about checkboxes? If there are 10 of them, there could be from zero to 10 entries for that particular parameter name. Here is an example of what i did.. but it assumes that the parameters are well known.
I used 10 in the 2D array constructor, because even though it compiles without one, it makes nasty error messages when trying to run. Something about a signature on the method, and that it might not be compatible with array of arrays or objects.

The request i used, looks like this:
?fName=Mike&lName=Curwen&id=1&id=2&id=3&lastOne=99
and the output is:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic