I have many tables in the database with different number of fields that need to be JTable in the JTable . The idea is:

  private void createUIComponents() { Connection connection; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(connectionString,login,password); String select = "SELECT * FROM printinghousedb.districts"; Vector<String> fields = new Vector(); fields.add("DistrictId"); fields.add("DistrictName"); DistrictsTable = new JTable(data, columnNames); Vector data = new Vector(); data = getTable(connection,select,fields); DistrictsTable = new JTable(data, fields); //инициализация Π΄Ρ€ΡƒΠ³ΠΈΡ… Ρ‚Π°Π±Π»ΠΈΡ†... } catch (Exception e) { e.printStackTrace(); } } private Vector[] getTable(Connection connection, String request, Vector fields) throws SQLException { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(request); while (rs.next()) { //... 

I do not know what to do next, how to form a Vector from ResultSet ?

Update

DistrictsTable = new JTable(data, columnNames); - the extra line remaining from previous attempts. I forgot to remove, I apologize.

  • 2
    why do you create a table 2 times? implementation of own class of the successor TableModel will be more correct option. it will be simpler to use a constructor with the Object[][] parameter - Mikhail Vaysman

1 answer 1

Vector contains an add() method. Use it:

 Vector<Vector<>> mainVector = new Vector<Vector<>>(); while (rs.next()) { Vector vec = new Vector<>(); vec.add(rs.getString(1)); vec.add(rs.getString(2)); vec.add(rs.getString(3)); // ΠΈ Ρ‚Π°ΠΊ Π΄Π°Π»Π΅Π΅ mainVector.add(vec); } 

And a little more incomprehensible is this part:

 //data Π½Π΅ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·ΠΈΡ€ΠΎΠ²Π°Π½Π°, ΠΈΠΌΠ΅Π»ΠΈ Π²Π²ΠΈΠ΄Ρƒ fields? DistrictsTable = new JTable(data, columnNames); Vector data = new Vector(); data = getTable(connection,select,fields); DistrictsTable = new JTable(data, fields); 
  • one
    there is not just Vector , but Vector from Vector . - Mikhail Vaysman
  • @MikhailVaysman corrected - Sv__t