0

I wrote some java code to display database, when I run the code it gives me just the last element of database , but I wanna display all elements of table

the code :

public String RecupererPuissance() {
    try {
        Connection con =  myDbvoiture.getConnection();
        String queryPattern ="select Power from bd_voiture";
        PreparedStatement pstmt =  con.prepareStatement(queryPattern);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            puissance=rs.getString("Power");
            System.out.println(puissance);
        }
    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }
    return puissance;
}

What should I do? Can anyone please help me to display all values? Thank you for helping me.

3
  • 1
    Your code looks correct. How many elements are inside the table bd_voiture? Also, how many elements inside the Resultset rs? Also, try to pass an order by clause to that select, so you get predictable results (select get unordered results by default) Commented May 26, 2016 at 9:47
  • @DiegoFreniche thank you for your help, I have 40 elements in my table , but one element inside the Resultset. Commented May 26, 2016 at 10:20
  • Do you get all 40 elements printed by that println? Also, look JHead answer Commented May 26, 2016 at 10:25

2 Answers 2

1

I think the problem is with your code that the value of puissance is overwritten every time you get the next element. Only the last value is returned. You should put the results into a list and return the whole list:

public List<String> RecupererPuissance() {
    List<String> puissances = new ArrayList<String>();
    try {
        Connection con =  myDbvoiture.getConnection();
        String queryPattern ="select Power from bd_voiture";
        PreparedStatement pstmt =  con.prepareStatement(queryPattern);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            puissance = rs.getString("Power");
            puissances.add(puissance);
            System.out.println(puissance);
        }
    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }
    return puissances;
}
Sign up to request clarification or add additional context in comments.

5 Comments

You need to correct the following lines: puissances.add(rs.getString("Power")); System.out.println(puissance); to be puissance = rs.getString("Power"); puissances.add(puissance); System.out.println(puissance); At the moment, your puissance isn't being set, which could cause issues at the print line statement
Thank you for helping me with this matter, I did that but nothing has changed , i don't understand why?
@Draken when i change my code and call my web service i used this code ` mws.MyWebService_Service service = new mws.MyWebService_Service(); mws.MyWebService port = service.getMyWebServicePort(); String puis= port.recupererPuissance(); System.out.println(puis); ` do you think the problem is in this a part
It is, it should be: List<String> puisList= port.recupererPuissance(); instead of String puis= port.recupererPuissance(); Then you could iterate it using a for loop, as an example. So if you want to print it, it would be: for(String puis : puisList) System.out.println(puis);
@Draken Great, My code is working , thank you very much for your reply.
0
public String RecupererPuissance() {
    try {
        Connection con =  myDbvoiture.getConnection();
        String queryPattern ="select Power from bd_voiture";
        PreparedStatement pstmt =  con.prepareStatement(queryPattern);
        ResultSet rs = pstmt.executeQuery();
        rs.first();
        int count=0;
        while (rs.next()) {
        System.out.println("count = "+count);
        count+=1;
            puissance=rs.getString("Power");
            System.out.println(puissance);
        }
    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
    }
    return puissance;
}

If you see count printed only 1 time :

it would mean that you have only 1 row in column Power and you maybe referring to the wrong column in your code

1 Comment

thank you for your help, I did that but nothing has changed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.