Hello, I have my class Gestor
with the following code:
Class gestor
{
private final String cadena = "jdbc:sqlserver://localhost:1433;databaseName=Persona";
public final String usuario = "mi_usuario";
public final String contrasenia = "mi_contraseña";
ArrayList<Persona> cargarPersonas()
{
ArrayList<Persona> personas = new ArrayList<>();
try
{
Connection cn = DriverManager.getConnection(cadena, usuario, contrasenia);
Statement st = cn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM Personas");
while (rs.next())
{
String nombre = rs.getString("nombre");
String apellido = rs.getString("apellido");
int edad = rs.getInt("edad");
Persona p = new Persona(nombre,apellido,edad);
personas.add(p);
}
rs.close();
st.close();
cn.close();
}catch (SQLException e)
{
System.out.println("ERROR : " + e);
}
return personas;
}
}
I have a JFrame
call ListadoPersonas
with a JList
call lstPersonas
. I would like to be able to load it with the data from the ArrayList
.
I'm not familiar with the
ArrayList
... But if these work the same as an array/vector/array/matrix you could use afor
to fill the list like this:If
ArrayList
it works the same as vectors you just have to adapt this example