jsp & select from database

Hi everyone,

I am making a connection in jsp, and i am making a query from a database(Microsoft Access) like this:

"Select * from table";

the query is executed correctly and i get the values from the database like this:

out.println("<TABLE Border=10 CellPadding=5><TR>");

out.println("<th bgcolor=brown>id</th><th bgcolor=yellow>name</th><th bgcolor=Red>phone</th><th bgcolor=Blue>salary</th></TR>");

out.println("<TR>");

out.println("<TD>"+recordset.getString(1)+"</TD>");

out.println("<TD>"+recordset.getString(2)+"</TD>");

out.println("<TD>"+recordset.getString(3)+"</TD>");

out.println("<TD>"+recordset.getString(4)+"</TD>");

out.println("<TR>");

the values are printed correctly,

the problem I have is that I want to print also the values from the next row in the database,

for example, lets say this is the table in the database:

idnamephonesalary

23jack23455000

54marc5689 7000

right now i'm printing the row where the name is jack,

but what i am trying to achieve is to print that row, and also the next row where name is marc.

can someone help me with this?

thanks...

[1661 byte] By [deroka] at [2007-11-27 8:37:55]
# 1

That's quite easy.

while(resultset.next()){

// Looping through the results.

String name = resultset.getString("name");

int phone = resultset.getInt("phone");

int salary = resultset.getInt("salary");

// Getting the variables from the resultset. Make sure that phonenumbers and salaries are saved as ints in the database.

out.println("<TR><TD>"+name+"</TD>");

out.println("<TD>"+phone+"</TD>");

out.println("<TD>"+salary+"</TD></TR>");

// Putting them in a table.

}resultset.close();

// Ending the loop and closing the resultset.

Make sure you put the first row of the table (i.e. the 'Name, Phonenumer and Salary' part) above the loop, otherwise the first row will be printed every time it hits a new name.

Nidhuggura at 2007-7-12 20:35:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
.
deroka at 2007-7-12 20:35:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

while (resultset.next()) {

String price = resultset.getString("price");

out.println("<TR><TD>"+price+"</TD>");

}

I did it like this,

It is printing all the rows,

example:

2

59

4

but,

how can I store each row value in an correspondent independent variables?

deroka at 2007-7-12 20:35:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

var priceArray = new Array();

while (resultset.next()) {

String price = resultset.getString("price");

String name= resultset.getString("name");

priceArray .push(price+_+name);

out.println("<TR><TD>"+price+"</TD>");

}

while reteriving elements u can split('_') function.

============================================

you can also use a map.

var itemDetailMap = new Object;

while (resultset.next()) {

String price = resultset.getString("price");

String name= resultset.getString("name");

String type= resultset.getString("type");

itemDetail= new ItemDetail();

itemDetail.name = name;

itemDetail.period = price ;

itemDetail.type = type;

key = name +'_' + period;

// or key can be any unique attribute e.g id only.

// key = resultset.getString("id");

itemDetailMap[key] = itemDetail;

out.println("<TR><TD>"+price+"</TD>");

out.println("<TD>"+name+"</TD>");

out.println("<TD>"+type+"</TD></tr>");

}

function ItemDetail()

{

this.name;

this.period;

this.type;

}

now u can reterive elements via :: var itemDetailValue = itemDetailMap[key];

itemDetailValue.period;

itemDetailValue .name;etc........

nitin_talwara at 2007-7-12 20:35:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...