I have jList and DB the DB have first name , last name , id and email
the jList will only display the first name , last name and id
what I'm trying to do is when the user select item in the jList the email will appear in the left side in textbox
here is what i did , but I want a better why if there is cuz I think my way is not good enough
what I did is the class of Person I did this for the to string method

@Override
    public String toString() {
        return  id + " | "+ fname + " "+ Lname;
    }

  //  I already managed to connect DB to JList  here 

public void fill() {

    model = new DefaultListModel();
    ResultSet rs = null;

    try {
        Connection conn = ConnectionManager.getConnection();
        Statement st = conn.createStatement();
        rs = st.executeQuery("select * from person");
        while (rs.next()) {
            int id = rs.getInt("ID");

            String fname = rs.getString("FirstName");
            String lName = rs.getString("LASTNAME");
            String email= rs.getString("EMAIL");

            person p = new person(fname, lName, id,email);
            model.addElement(p);

            ListPerson.setModel(model);                

            rs.close();
            st.close();

        }
    } catch (SQLException ex) {
    }

Dani AI

Generated

— the simplest, safest pattern is to keep the full Person object in the JList model and use a custom cell renderer to control what the list shows (so you don’t have to rely on toString for display). is right: the JList just displays whatever you give it — you aren’t “connecting” it to the DB. Build the model from the ResultSet once, set it on the JList after the loop, and use a ListSelectionListener to populate the email JTextField when selection changes. Also do DB work off the EDT (SwingWorker or background thread) and use try-with-resources so statements/resultsets are closed correctly.

Example (Java 7+/8+) — model-building, renderer and selection listener:

DefaultListModel<Person> model = new DefaultListModel<>();
String sql = "SELECT ID, FirstName, LastName, Email FROM person";
try (Connection c = ConnectionManager.getConnection();
     PreparedStatement ps = c.prepareStatement(sql);
     ResultSet rs = ps.executeQuery()) {
    while (rs.next()) {
        model.addElement(new Person(
            rs.getInt("ID"),
            rs.getString("FirstName"),
            rs.getString("LastName"),
            rs.getString("Email")));
    }
}
list.setModel(model);

list.setCellRenderer(new DefaultListCellRenderer() {
    @Override
    public Component getListCellRendererComponent(JList<?> l, Object v, int i, boolean sel, boolean focus) {
        super.getListCellRendererComponent(l, v, i, sel, focus);
        Person p = (Person) v;
        setText(p.getId() + " | " + p.getFirstName() + " " + p.getLastName());
        return this;
    }
});

list.addListSelectionListener(e -> {
    if (!e.getValueIsAdjusting()) {
        Person sel = list.getSelectedValue();
        emailField.setText(sel == null ? "" : sel.getEmail());
    }
});

Troubleshooting notes: don’t close ResultSet/Statement inside the loop; don’t call setModel repeatedly; guard UI updates on the EDT; handle SQL exceptions (log or show an error); and use PreparedStatement (select specific columns) rather than “select *” for clarity and performance.

ehm ... just a small remark: you don't actually 'connect a JList to a DB', you just show the results in it.
I'm not exactly clear on what it is you are looking for, can you be a bit more precise?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.