Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / March 2004

Tip: Looking for answers? Try searching our database.

newbie JDBC SP exec

Thread view: 
homesick_alien - 14 Mar 2004 14:31 GMT
Hey There,

Ive been thrown into the deep end at work, ...i" want you to write these
servlets....im assuming you gonna need to learn java..you have two
weeks"...kinda thing....

Im using Velocity and the MVC method...i have a servlet that connects to a
Sybase DB and executes a Stored Procedure, the stored procedure returns a
bunch of records consisting of two columns....

CallableStatement cs = con.prepareCall("{call sp_ml_getglobalfilms}");
           ResultSet rs = cs.executeQuery();

while (rs.next()) {

                String fid = rs.getString("film_id");
                String fname = rs.getString("film_name");

Now how do i add these to "something" ..like array or hashtable? ....that
will hold the two coloums of multiple results.....im not sure if this works
but i just threw a couple of things together

HashMap result = new HashMap();
ArrayList resultList = new ArrayList();

.....

              result.put(fidKey,fid);
              result.put(fnameKey,fname);

              resultList.add(result);

Which then i will have to add to the context in velocity..

                ctx.put(fidKey,result.get(fidKey));
               ctx.put(fnameKey,result.get(fnameKey));

so the velocity template can iterate over the context and display the
results on a web page...

#foreach ($item in $result)

<tr class="tableData">

<td>$item.get(fidKey)</td>

<td>$item.get(fnameKey)</td>

</tr>

#end

Now what i get is...the ctx isnt displayed onthe webpage.... and im not sure
if all the results are added to the context....or ...?

Can anybody point me in the right direction?
javamad - 17 Mar 2004 12:53 GMT
Hi there,

The #foreach loop in velocity can iterate through the following java
Object types: Vector, Hashtable or an Array.

For you to display a key,value pair in your web page, the best thing is
to use a Hashtable (I can't remember if you can use a HashMap object
instead, try it!)

The code would look something like this

Hashtable hash = new Hashtable();

>  CallableStatement cs = con.prepareCall("{call
>  sp_ml_getglobalfilms}");
[quoted text clipped - 4 lines]
>                  String fid = rs.getString("film_id");
>                  String fname = rs.getString("film_name");
                  // Add key,value pair to Hashtable hash
                  hash.put( fid, fname );
  }

// Then on to your context ctx:

                ctx.put( "result", hash );
               // result is the name to be used in the template file
               // that is $result

// And on your velocity template file

> #foreach ($item in $result)
>
> <tr class="tableData">

   <!-- displays the key -->
   <td>$item</td>

   <!-- displays the value -->
   <td>$result.get($item)</td>

> </tr>
>
> #end

Hope this helps,

Fabricio.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.