Create as many String objects as many columns in different JTable tables



  • Tell me how to do that. Object[] row filled with the number of columns as provided in the requested table.

    headers.size() = 7That's why rs.getString(i++) It says seven times. Less, more is a mistake.

    How to write a code so that you don't need to create so much manually. rs.getString(i++)How many columns in the other database table?

    static void select() {
        try {
        PreparedStatement st = c.prepareStatement("select * from " + table_name);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            int i = 1;
            Object[] row = new Object[]{
                    rs.getString(i++),
                    rs.getString(i++),
                    rs.getString(i++),
                    rs.getString(i++),
                    rs.getString(i++),
                    rs.getString(i++),
                    rs.getString(i++)
            };
            Gui.model.addRow(row);
        }
        } catch () {}
    }
    


  • int columnCount = rs.getMetaData().getColumnCount(); // Получаем число столбцов
    
    Object[] arr = new Object[columnCount];
    Arrays.setAll(arr, x -> rs.getString(x)); // Заполняем массив
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2