Retieving data from mysql

i have an RMI application ang my database connection is in my implementation java file.my methods also found in the implementation file retrieve only a value from the database. Is it possible to retrieve multiple values or records ?Regards,
[261 byte] By [victoriousa] at [2007-11-26 21:15:55]
# 1
Show the relevant parts of your code, and we may be able to help you. But the short answer is Yes, and the easy answer is No.
masijade.a at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

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,

victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

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);

}

masijade.a at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
thank you so much! i'll try it...
victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
for example, to retrieve all the values in the column empname, do i do it the way you previously explained?Regards,
victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6
just simply leave of the "where" clause. i.e.select empname from emptable
masijade.a at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 7
thx loads! great help! :)
victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 8

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,

victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 9

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.

masijade.a at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 10

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,

victoriousa at 2007-7-10 2:54:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 11

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.

masijade.a at 2007-7-10 2:54:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 12

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

victoriousa at 2007-7-10 2:54:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 13

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.

masijade.a at 2007-7-10 2:54:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 14
System.out.println will display the items at the server... i need to return them, to display them at the client side...
victoriousa at 2007-7-10 2:54:26 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 15

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.

masijade.a at 2007-7-21 18:14:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 16
i got it... thx a lot for ur precious help! ure right, i really need to go back to the basics! thx again ...
victoriousa at 2007-7-21 18:14:48 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...