This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Problem

 
Farakh khan
Ranch Hand
Posts: 833
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic