0

ta.setText is a TextArea where I want to show all my data from the database, after a button click. But with rs.get("name") I just output one value and it is always the last. How can I print out the whole table from the database, so all the information which are stored there?

            try { String newquery = "SELECT * FROM kunden";
            java.sql.PreparedStatement ps = con.prepareStatement(newquery);
            rs = ps.executeQuery(newquery);


            while (rs.next()){              

            ta.setText(rs.getString("name"));
            ta.setText(rs.getString("nachname"));
            }


        }// try
        catch(Exception e1) {
        JOptionPane.showMessageDialog(null, "fail");


      }
      }//actionperformed
3
  • If you have multiple values, then you can't use .setText to display all of them, you would either need to display those values in a table component or concatenate all values before calling .setText on ta. Commented May 26, 2018 at 12:10
  • @ErnestKiwele thx for your answer. Yes there are two rows of data and I want to show them all. How can I handle that with component ? Commented May 26, 2018 at 12:44
  • You need to choose a component that displays multiple values... like a table, a list, etc. Seems like a table is your best option as you have also multiple columns... Commented May 26, 2018 at 12:56

1 Answer 1

2

Either you build a string an then set that string using setText()

StringBuilder builder = new StringBuilder();
while (rs.next()) {
   builder.append(rs.getString(“name”));
   builder.append(“ “);
   builder.append(rs.getString(“nachname”));
   builder.append(“\n“);       
}
ta.setText(builder.toString());

Or you use the append method that exists for TextArea

while (rs.next()) {
   ta.append(rs.getString(“name”));
   ta.append(“ “);
   ta.append(rs.getString(“nachname”));
   ta.append(“\n“);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you @Joakim Danielson. When I try that I get the error " The method getString(int) in the type ResultSet is no ablecable for th arguments ()."

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.