Win a copy of Java Challengers this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Servlet Problem

 
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<blockquote>code:
<pre name="code" class="core">String query2="select a.p_id,s_id,specialty from position a, specialty b where a.p_id=b.p_id";

PreparedStatement ps2=cnn.prepareStatement(query2);
rs=ps2.executeQuery();
while(rs.next()){
String p_id=rs.getString(1);
String s_id=rs.getString(2);
String specialty=rs.getString(3);
out.println("store["+p_id+"] = new Array('"+specialty+"','"+s_id+"');");
}
ps2.close();
</pre>
</blockquote>


OUTPUT IS:
===========
store[1] = new Array('Cardiologist','1');
store[1] = new Array('Dermatologist','2');
store[1] = new Array('Urologist','3');
store[2] = new Array('CEO','4');
store[2] = new Array('COO','5');
store[3] = new Array('Cardiac Nurse','6');
store[3] = new Array('ss Nurse','7');
store[4] = new Array('Xray Technician','8');
store[4] = new Array('ECG Tech','9');


WHEREAS I WANT TO OUTPUT LIKE:
================================
store[1] = new Array('Cardiologist','1','Dermatologist','2','Urologist','3');
store[2] = new Array('CEO','4','COO','5');
store[3] = new Array('Cardiac Nurse','6','ss Nurse','7');
store[4] = new Array('Xray Technician','8','ECG Tech','9');




MY TABLE STRUCTURE
===================
SQL> desc position
Name Null? Type
----------------------------------------------- -------- --------------------------------
P_ID NOT NULL NUMBER(14)
POSITION VARCHAR2(104)

SQL> desc specialty
Name Null? Type
----------------------------------------------- -------- --------------------------------
S_ID NOT NULL NUMBER(14)
P_ID NUMBER(14)
SPECIALTY VARCHAR2(140)

[edit]Add code tags. CR[/edit]
[ July 17, 2008: Message edited by: Campbell Ritchie ]
 
Sheriff
Posts: 67528
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This really has nothing to do with Servlets, or even JDBC, but rather basic algorithms, so I've moved it along to a more appropriate forum.
 
Marshal
Posts: 72650
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you want to group output by specialty groups. Is there anything in your tables which allows such grouping? Yes there is, there is p_id. An "order by" clause in the query would help get them together.

You are going to have to work out some way of adding names of specialties.

How about adding them to Lists and printing out the Lists, based on the p_id numbers? Or using a StringBuilder and adding the names until the p_id changes.

By the way, your identifiers are difficult to read, "p_id" and "s_id" don't tell the reader what they are supposed to mean.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic