[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Close to having database queries displayed to Jlist, what am I missing?

Posted on 2016-09-28
3
Medium Priority
?
80 Views
Last Modified: 2016-09-30
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);
             sBuilder.append(rec.getString("Column2")).append(SPACE);
             sBuilder.append(rec.getString("Column3")).append(SPACE);
             sBuilder.append(rec.getString("Column4")).append(SPACE);
             sBuilder.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());
0
Comment
Question by:Evan Redmond
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
Moussa Mokhtari earned 2000 total points
ID: 41820870
@Evan Redmond
move
 jList1.setModel(model); 

Open in new window

after the while loop
0
 

Author Comment

by:Evan Redmond
ID: 41820917
It didn't work, I also changed the appending part of the loop and it still didnt work..... so it was:

   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();
          jList1.setModel(model);
0
 
LVL 9

Expert Comment

by:Moussa Mokhtari
ID: 41824150
Are you sure that your model is populated with your data before you set it to jList1 ?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question