Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Close to having database queries displayed to Jlist, what am I missing?

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 361
    Answer it

    Hello, I feel like I may be very close to having my app display the rows from my database in the Jlist the way I would like (with every column from each row  displayed as one item on the Jlist. I think the problem might be in connecting the jList variable to the model(the jlist is called jList1), OR because of the fact that the jlist auto populated with items (item1, item2) etc, which I have not yet tried to remove. Do these have to be removed before I could expect it to work? Any help is greatly appreciated!....... it looks like: 

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
           
            DefaultListModel<String> model = new DefaultListModel<>();
            JScrollPane pane = new JScrollPane(jList1);
            jList1.setModel(model);
            String sqlQuery = "select Column1, Column2, Column3, Column4, Column5 from APP.DATA123 " + 
    "where (Column1 = ?) AND (Column2 = ?) AND (Column3 = ?) OR (Column1 = ?) AND (Column2 = ?)";

                             

                String abc = jTextField2.getText();
                String cba = (String)jComboBox1.getSelectedItem();
                String cab = (String)jComboBox2.getSelectedItem();
                String data = "jdbc:derby://localhost:1527/sample";
            try (
                  Connection conn = DriverManager.getConnection(
                  data, "app", "app");
                  PreparedStatement st = conn.prepareStatement(sqlQuery))   { 
              
              Class.forName("org.apache.derby.jdbc.ClientDriver");
              st.setString(1, abc);
              st.setString(2, cba);
              st.setString(3, cab);       
              st.setString(4, cba);
              st.setString(5, cab);
              ResultSet rec = st.executeQuery();
              
               final String SPACE = " ";
               StringBuilder sBuilder = new StringBuilder();
               
                while (rec.next())  {
                 sBuilder.setLength(0);
                 sBuilder.append(rec.getString("Column1")).append(SPACE)
                 .append(rec.getString("Column2")).append(SPACE)
                 .append(rec.getString("Column3")).append(SPACE)
                 .append(rec.getString("Column4")).append(SPACE)
                 .append(rec.getString("Column5")).append(SPACE);
                 
                 model.addElement(sBuilder.toString());
                }
              st.close();
              
                } catch (SQLException s)  {
              System.out.println("SQL Error: " + s.toString()  + " "
                      + s.getErrorCode() + " " + s.getSQLState());
          } catch (Exception e) {
              System.out.println("Error: " + e.toString()
              + e.getMessage());

 1 Answer(s)

  • Hi
    Here, We are having two ways :
    First of you need to remove all elements from DefaultListModel.

    DefaultListModel<String> model = new DefaultListModel<>();
    model.removeAllElements();
    

    Second of you need to make empty to list.

    jList1.setListData(new String[0]);
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: