the implementation of my methods are like
public String getname(int empno) throws RemoteException
{
try
{
rs = st.executeQuery("Select empname from emptable where empno="+empno);
rs.next();
return(rs.getString(1));
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public double getsal(int empno) throws RemoteException
{
try
{
rs = st.executeQuery("Select salary from emptable where empno="+empno);
rs.next();
return(rs.getFloat(1));
}
catch(Exception e1)
{
e1.printStackTrace();
}
return (0);
}
how do i write methods that retrieve multiple values? i tried
while (rs.next()){
//
}
still this retrieves only the first value
plz help
Regards,
rs = st.executeQuery("Select empname from emptable where empno="+empno);
I assume that the "empno" (assumed employee number) is a unique field, right?
Well, in that case, the above query will always return only one record as each "empno" will only exist one time. If you are looking for multiple "empno" values, then change your query to use an IN clause and multiple "empno" values. As follows (assuming that the "empno" values can be stored in an array):
StringBuffer query = new StringBuffer("Select empname from emptable where empno in (");
for (String empno : empnoArray ) {
query.append(empno).append(", ");
}
rs = st.executeQuery(query.toString().replaceFirst(", $", ")"));
while (rs.next()){
// store the results somewhere or work on them.
}
If you mean that you want to retreive the name and the salary in one query then do the following:
String query = new StringBuffer("Select empname, salary from emptable where empno=" + empno);
rs = st.executeQuery(query);
if (rs.next()){
empname = rs.getString(1);
salary = rs.getDouble(2);
}
public String serviceslist() throws RemoteException
{
try
{
StringBuffer query = new StringBuffer("Select Name from Service");
rs = st.executeQuery(query.toString());
while (rs.next()){
String name = rs.getString(1);
return(name);
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
return (null);
}
ive tried this one, for all the values of Name in the table Service... but it only returns the first record in the table...
Regards,
Code snippet from your post:
while (rs.next()){
String name = rs.getString(1);
return(name);
}
Because you are returning as soon as you receive the first name (as seen above). You need to store the results in an ArrayList and return the entire ArrayList. Or concatenate into a single String. Try this:
public ArrayList<String> serviceslist() throws RemoteException {
ArrayList<String> retStr = new ArrayList<String>();
ResultSet rs;
try {
StringBuffer query = new StringBuffer("Select Name from Service");
rs = st.executeQuery(query.toString()); // I assume st is an instance variable.
while (rs.next()){
retStr.add(rs.getString(1));
}
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
/* rs would only ever be null if st is also null (or otherwise miss defined)
* Normally I wouldn't check it, but since st is being used as an instance
* variable, it makes at least some sense to check
*/
if (rs != null) {
rs.close(); // I must assume you are closing st somewhere else.
}
}
return retStr;
}
Study the above code. You will see other changes from yours in addition to those changes needed to implement an ArrayList.
How do i retrieve the items one by one from the array?
Iterator<String> itr = retStr.iterator();
while (itr.hasNext()) {
String str = itr.next();
return (str);
}
this gives me error: "missing return statement"
Regards,
That is because you have the return statement inside of the while statement. What will happen if "itr" has no elements and itr.hasNext() returns false on the first attempt? It would never enter the while loop and so the return would never be called.
What exactly are you trying to do here? You do realize that this will then, always return the first element from "itr", right? And never anything else, right?
Edit: you probably want to use a for loop in the method where you work on the returns from the query.
i want to retrieve the data items in the array one by one... from previous one, return value is like [a,b,c], i want it like
a
b
c
i tried
for (String str : retStr ) {
return(str);
}
still doesnt work
I assume that currently you have a method that works on a single value, right? something like:
public void doWork () {
String name = serviceslist();
System.out.println(name);
}
That is before servicesList returned an ArrayList. Change the method as follows:
public void doWork () {
ArrayList<String> names = serviceslist();
for (String name : names) {
System.out.println(name);
}
}
I'm sorry, but you really need to go back to the basics. Go to the sun tutorials and work through them, starting at the very beginning, because (and I don't mean to be mean but) you don't seem to have any grip on some of the most basic elements of the language.
And the code I gave was a very simplified example to show you how you should be implementing the for loop in your current code. It was not meant to be, and certainly is not, a cut and paste answer for your problem. Simply an illustration. It showed you how to change the current call to servicesList() and how to wrap the rest of the method in a for loop. Apply those two lessons/examples/illustrations to your code.