title: JavaFX ListView controller communication and and Database Issues
body: <p>Basically I have a main scene with a list and an Add Student button. Clicking the Add Student button opens a new window in which there are two text fields, name and surname. These two are then passed to a Student object constructor that contains, among other attributes, the last and first name of the student. The new student object is now stored and I use a method addToNames that uses student.getName() and student.getSurname() to add the attributes into the list view. The issue is that it doesn't display the names in the list, and while debugging with print statements, I found out each student object overrides the previous one the way I implemented it. It got even stranger because I remember trying out simple print statement variations of implementing this, and some worked some didn't. Only example I can remember is that when I used print(&quot;lol&quot;) in my addToNames method as it is right now, It would work. I also want to store these values in a MySQL database with which I already have an established connection. However, I don't know how to do this . Here is my code</p>
  <p>My JavaFX main scene controller class</p>
  <pre><code>public class Events {
  
  @FXML
  private Button studentADD;
  @FXML
  private Button add;
  @FXML
  private ListView&lt;String&gt; nameslist;
  
  private Student[] Students = new Student[200];
  
  
  
  public void addToNames(Student student) {
      nameslist.getItems().add(&quot;xd&quot;);
      System.out.println(student.getName() + &quot; &quot; + student.getSurname());
  }
  
  
  public void switchToStudentAdd(ActionEvent e) throws IOException {
      //Student stud = new Student(&quot;John&quot;, &quot;Appleseed&quot;);
      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(&quot;AddStud.fxml&quot;));
      Parent root = (Parent) fxmlLoader.load();
      Stage stage = new Stage();
      stage.setScene(new Scene(root));  
      stage.show();
      //nameslist.getItems().add(stud.getName() + &quot; &quot; + stud.getSurname());
  }}
          
  </code></pre>
  <p>My JavaFX add student window secondary scene controller class</p>
  <pre><code>public class AddStudEvents{
  
  @FXML
  private TextField firstName;
  @FXML
  private TextField lastName;
  @FXML
  private Button addStudent;
  
  public void addNameToList(ActionEvent e) throws IOException {
      String name = firstName.getText();
      String lastname = lastName.getText();
      
      FXMLLoader loader = new FXMLLoader(getClass().getResource(&quot;Main.fxml&quot;));
      Parent root = loader.load();
      Events maincontroller = loader.getController();
      
      Student student = new Student(name, lastname);
      maincontroller.addToNames(student);
  }}
  </code></pre>
  <p>My SQL adding method (It does work, however I want it to insert variable student.getName() and a variable student.getSurname() strings, as well as an array of student Marks which is another attribute of the student class, I don't know if this is possible for arrays though. I head somewhere I need to use a JSON, but I'm not sure what that means. I am using SQL workbench)</p>
  <pre><code>public class DatabaseControl {
  
  static String url = &quot;jdbc:mysql://localhost:3306/ia&quot;;
  static String username = &quot;root&quot;;
  static String password = &quot;sifra123&quot;;
  static Connection connection = null;
  
  public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, SQLException {
      Class.forName(&quot;com.mysql.jdbc.Driver&quot;).getDeclaredConstructor().newInstance();
      connection = DriverManager.getConnection(url, username, password);
      PreparedStatement ps = connection.prepareStatement(&quot;INSERT INTO ia.student (name) VALUES ('yussef walker');&quot;);
      PreparedStatement ps1 = connection.prepareStatement(&quot;INSERT INTO ia.student (surname) VALUES ('yussef walker');&quot;);
      PreparedStatement ps2 = connection.prepareStatement(&quot;INSERT INTO ia.student (surname) VALUES ('yussef walker');&quot;);
      
      int status = ps.executeUpdate();
      
      if(status != 0) {
          System.out.println(&quot;Connection established&quot;);
          System.out.println(&quot;Inserted&quot;);
      }
  
  }}
  </code></pre>
  <p>Sorry for putting the final curly braces so strangely, the text editor won't let me do it properly.</p>
  <p>EDIT: I would prefer to just ad it to the database and then be able to populate a listview with my database name and last name if possible</p>