How do I display data from a query in a database within a JtextArea? I have done all my Listing method (which is in the class CarroDAO), and I want it to show the data inside the textArea (which is in the Screen class)
follow the code:
DAO Car class with list method:
@Override
public ArrayList<Carro> listar() {
ArrayList<Carro> carros= new ArrayList<Carro>();
String sql="select * from carro";
try(Connection con= new ConnectionFactory().getConnection()){
PreparedStatement ps= con.prepareStatement(sql);
ResultSet rs= null;
rs=ps.executeQuery();
while(rs.next()) {
Carro c= new Carro();
//pegando os dados da tabela
c.setId(rs.getInt("id"));
c.setMarca(rs.getString("marca"));
c.setModelo(rs.getString("modelo"));
c.setCor(rs.getString("cor"));
c.setPlaca(rs.getString("placa"));
carros.add(c);
}
ps.close();
rs.close();
}catch(SQLException e){
JOptionPane.showMessageDialog(null, "Erro ao realziar consulta:"+e, "ERROR", JOptionPane.ERROR_MESSAGE);
throw new RuntimeException(e);
}
return carros;
}
Screen Class with JtextArea:
JTextArea textArea = new JTextArea();
//Jscrollpane:
//colocando o text area dentro do ScrollPane
jspane= new JScrollPane(textArea);
jspane.setBounds(286, 48, 162, 170);
contentPane.add(jspane);