I currently have two different fxml pages. One is the main screen, and the other handles connections. The main Screen is currently creating the database and the tables needed on initialize. The other page is for adding and deleting values from the database. The issue becomes that I am currently running a function on the main page to populate the choiceboxes with the database data in the initialize, but when it runs it tries to create the table again and throws an error because the tables is already created. Is there an easy way to solve this?
Below is the Main pages initialize function:
public void initialize() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//below gets the connection specified earlier
conn = DriverManager.getConnection(dbURL);
System.out.println("DB Connection Created!");
String createString = "CREATE TABLE Connections (DETAILS VARCHAR(255) NOT NULL, NAME VARCHAR(255) UNIQUE)";
stmt = conn.createStatement();
stmt.execute(createString);
System.out.println("Connections table created!");
//below is for populating the usernames
String sql2 = "Select NICKNAME FROM Users ORDER BY NICKNAME ASC";
stmt = conn.createStatement();
ResultSet rs2 = stmt.executeQuery(sql2);
usernameList.clear();
while(rs2.next()) {
String name = rs2.getString("NICKNAME");
usernameList.add(name);
System.out.println("User initialize: " + name);
}
System.out.println("Objects: " + usernameList);
sourceUsername.setItems(FXCollections.observableArrayList(usernameList));
cbosUsername.setItems(FXCollections.observableArrayList(usernameList));
tiUsername.setItems(FXCollections.observableArrayList(usernameList));
}
catch(Exception e) {
e.printStackTrace();
}
}
Does anyone have any input on an easy way to solve this?