1

I am designing a web-service using java and eclipse which returns the user details who are marked as customer in the database

I was successfully able to return details for a single user (as there was only one entry in the dB) with the following code:

public class GetData {

public LoginDetails getDetails(){    
    Connection conn;
    Statement stmt;
    ResultSet rs;

    try {
        LoginDetails lds=new LoginDetails();
        Class.forName(driver);
        conn=DriverManager.getConnection(url,username,password);
        stmt=conn.createStatement();
        String sql="select * from login where usertype='customer'";
        rs=stmt.executeQuery(sql);
        while(rs.next()){
            lds.setUsername(rs.getString(1));
            lds.setPassword(rs.getString(2));
            lds.setUsertype(rs.getString(3));
            lds.setActive(rs.getString(4));

        }
        return lds;
    } 
    catch(ClassNotFoundException c){
        c.printStackTrace();
    }
    catch (SQLException e) {
        e.printStackTrace();
    }

    return null;
}
}

What should I do if there are multiple values in dB matching the criteria and I want to display them all. Please advice.

Kai
  • 38,985
  • 14
  • 88
  • 103
Nelo Angelo
  • 977
  • 3
  • 11
  • 16
  • You can return a [List](http://docs.oracle.com/javase/6/docs/api/java/util/List.html) – m0skit0 Apr 10 '12 at 09:38
  • I'd just return a `List` or `LoginDetails[]`. One thing though, I don't think it would be wise from a security standpoint to return a user's password. – beny23 Apr 10 '12 at 09:38
  • @beny23 Right, I was just trying to get a hang of how services work on a preexisting entry in the dB. That's why I was returning the password. My bad :) – Nelo Angelo Apr 10 '12 at 09:57
  • @m0skit0 I tried to do use List as well as Collection but both of them yield the same result- object references. – Nelo Angelo Apr 10 '12 at 10:27
  • Create a new object for each one. You cannot reuse the same one (`lds`) like you do now. – m0skit0 Apr 10 '12 at 10:36

2 Answers2

3

Change your method signature to public LoginDetails[] getDetails()

And extend your while loop as follows:

    Collection<LoginDetails> details = new ArrayList<LoginDetails>();
    while(rs.next()){
        LoginDetails lds=new LoginDetails();
        lds.setUsername(rs.getString(1));
        lds.setPassword(rs.getString(2));
        lds.setUsertype(rs.getString(3));
        lds.setActive(rs.getString(4));
        details.add(lds);

    }
    return details.toArray(new LoginDetails[0]);
Kai
  • 38,985
  • 14
  • 88
  • 103
  • I made the required changes and ran the program to get the following warning: _The service class "com.tutorial.GetData" does not comply to one or more requirements of the JAX-RPC 1.1 specification, and may not deploy or function correctly. The method "getDetails" on the service class "com.tutorial.GetData" uses a data type, "java.util.Collection", that is not supported by the JAX-RPC specification. Instances of the type may not serialize or deserialize correctly. Loss of data or complete failure of the Web service may result._ – Nelo Angelo Apr 10 '12 at 10:15
  • Ignoring the warning an running the code gives the following output: **[[Ljava.lang.Object;@19214b1, [Ljava.lang.Object;@1afd9cc]** – Nelo Angelo Apr 10 '12 at 10:16
  • Do I have to make any changes in the wsdl file?? – Nelo Angelo Apr 10 '12 at 10:30
  • @NeloAngelo: Ok, Didn't know that. I've changed the code above. – Kai Apr 10 '12 at 10:39
  • Thanks for the reply, the changes made the warning go away but I am still getting object references. This is what I got after I made the changes as suggested: **[com.tutorial.LoginDetails@2507a053, com.tutorial.LoginDetails@349fbf38]** – Nelo Angelo Apr 10 '12 at 10:51
  • @NeloAngelo: That looks fine. Now you can iterate over that Array. – Kai Apr 10 '12 at 10:55
  • I am very new to web services and don't have any idea how to iterate the array received in web-serives mode. This is what I am getting so far: [Picture](http://www.flickr.com/photos/77069305@N04/6918005392/). Any suggestions on how i should proceed? – Nelo Angelo Apr 10 '12 at 11:18
  • @NeloAngelo: This is a common array. There is nothing special about it because you have received it from a web-service. Your question was about returning multiple values from a web-service. This question is obviously solved now. You should proceed your tutorial now and/or learn the Java basics. – Kai Apr 10 '12 at 11:25
  • :) Yeah, the question is resolved. Thanks for the replies man. – Nelo Angelo Apr 10 '12 at 11:44
0

Return an collection type suggestively java.util.List , preferably ArrayList from the method.

Akash Yadav
  • 2,411
  • 20
  • 32
  • It's a good practice to use the most common type. I'd use `Collection` here. A `List` is ordered a `Collection` isn't. If you use a `List` you should be able to describe in which way it is ordered. – Kai Apr 10 '12 at 09:42