我有一个Windows应用程序,我想从MySQL数据库填充我的表。我已经编写了下面的代码来获取表中的数据。我能够获取数据库,但在表中添加行时,它只显示MySQL数据库中的最后一行(重复的MySQL行数)。
public class ShowDatabase{
private static ObservableList<UserMaster> row;
private static ObservableList<String> col;
public static void buildData(TableView<UserMaster> tableview){
Connection c;
String SQL = "SELECT * from user_info";
UserMaster cm = new UserMaster();
try{
String colHeading[]={"Name","Course","e-mail","City"};
col = FXCollections.observableArrayList(colHeading);
row= FXCollections.observableArrayList();
c= DBConnection.connect();
ResultSet rs = c.createStatement().executeQuery(SQL);
TableColumn<UserMaster,String> colName = new TableColumn<UserMaster, String>(col.get(0));
colName.setMinWidth(200);
colName.setCellValueFactory(new PropertyValueFactory<UserMaster,String>("name"));
TableColumn<UserMaster, String> colCourse = new TableColumn<UserMaster, String>(col.get(1));
colCourse.setMinWidth(200);
colCourse.setCellValueFactory(new PropertyValueFactory<UserMaster,String>("course"));
TableColumn<UserMaster, String> colEmail = new TableColumn<UserMaster, String>(col.get(2));
colEmail.setMinWidth(200);
colEmail.setCellValueFactory(new PropertyValueFactory<UserMaster,String>("email"));
TableColumn<UserMaster, String> colCity = new TableColumn<UserMaster, String>(col.get(3));
colCity.setMinWidth(200);
colCity.setCellValueFactory(new PropertyValueFactory<UserMaster,String>("city"));
tableview.getColumns().addAll(colName,colCourse,colEmail,colCity);
//colMobile,colEmail,colDoj,colCity);
while(rs.next()){
ObservableList<UserMaster> data = FXCollections.observableArrayList();
cm.name.set(rs.getString("name"));
cm.course.set(rs.getString("course_name"));
cm.email.set(rs.getString("email"));
cm.city.set(rs.getString("city"));
row.add(cm);
System.out.println(row.get(0).getName());
}
tableview.setItems(row);
}catch(SQLException sqex){
sqex.printStackTrace();
System.out.println("Error on Building Data");
}
}
}
构建表的代码(应用程序):
public class DisplayDatabase extends Application{
@Override
public void start(Stage stage) throws Exception {
stage.setFullScreen(false);
TableView tableview;
//TableView
tableview = new TableView();
DisplayDatabase.buildData(tableview);
//Adding GridPane
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(20,20,20,20));
gridPane.setHgap(5);
gridPane.setVgap(5);
//Main Scene
Scene scene = new Scene(tableview);
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
用于格式化Observable List的类:
public class UserMaster{
public SimpleStringProperty name = new SimpleStringProperty();
public SimpleStringProperty course = new SimpleStringProperty();
public SimpleIntegerProperty mobile = new SimpleIntegerProperty();
public SimpleStringProperty email = new SimpleStringProperty();
public ObjectProperty<Date> doj = new SimpleObjectProperty<Date>();
public SimpleStringProperty city = new SimpleStringProperty();
public String getName() {
return name.get();
}
public String getEmail() {
return email.get();
}
public String getCity() {
return city.get();
}
public String getCourse() {
return course.get();
}
}
虽然我有2个唯一的数据行,但它只是重复显示数据库表的最后一行(相同的数据)。 这是图片附件,以便更清楚地了解问题。
答案 0 :(得分:1)
在每次迭代时创建新的UserMaster
:
while(rs.next()){
UserMaster cm = new UserMaster();
cm.name.set(rs.getString("name"));
cm.course.set(rs.getString("course_name"));
cm.email.set(rs.getString("email"));
cm.city.set(rs.getString("city"));
row.add(cm);
}
tableview.setItems(row);