2 Replies - 850 Views - Last Post: 28 September 2016 - 12:59 PM Rate Topic: -----

#1 evanr12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-September 16

JList items as groups of rows from the DB, displaying multiple rows?

Posted 27 September 2016 - 01:25 PM

I currently have a database that is working well. It displays the text from each column of the database into 5 labels on the app user interface. Some of the time, depending on what the user enters, I want it to display a list of rows from the database, I was thinking of using the JList.

the current code to display to the labels is:
 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();
            while (rec.next())  {
              jLabel1.setText(rec.getString("Column1"));
              jLabel2.setText(rec.getString("Column2"));
              jLabel3.setText(rec.getString("Column3"));
              jLabel4.setText(rec.getString("Column4"));
              jLabel5.setText(rec.getString("Column5"));
              
          }
          st.close(); 




Is there an easiest way to make it so that if there is more than one row of the database to be displayed to the user, I can make a JList so that all of the columns of one row (and possibly an image but ill think about that later), are displayed as one item on the JList. So Columns1-5 are one JlistItem, and then columns 1-5 for the next database row are the next JList item etc. ??

Or another similar way that anyone knows of?

There is not that much text per column only about 5-15 characters.

Thanks for your time!

Is This A Good Question/Topic? 0
  • +

Replies To: JList items as groups of rows from the DB, displaying multiple rows?

#2 IOExceptional   User is offline

  • D.I.C Head
  • member icon

Reputation: 28
  • View blog
  • Posts: 141
  • Joined: 15-September 16

Re: JList items as groups of rows from the DB, displaying multiple rows?

Posted 27 September 2016 - 02:34 PM

Hi, evanr12,

I suppose you are able to create a JList and fill it with data from each row, but why bother? If you did make one, how would you display the Strings that it would contain?

If you're simply wondering what kind of GUI element to use to display the data, I might suggest a JTable as a visual representation of your database. It's not exactly original, but it works.

By the way...

View Postevanr12, on 27 September 2016 - 10:25 PM, said:

Column1
String abc
String data
jLabel1


I strongly suggest that you get better at naming things, because names like that will confuse you later on.
Was This Post Helpful? 0
  • +
  • -

#3 evanr12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-September 16

Re: JList items as groups of rows from the DB, displaying multiple rows?

Posted 28 September 2016 - 12:59 PM

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()); 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1