javafx Tableview未显示数据库中的数据 [英] javafx Tableview not showing data from database
问题描述
我的表格视图无法在其中显示数据。它在控制台中显示结果集数据但无法在tableview javafx中显示。请帮忙。
My table view is not able to show data in it. It is showing resultset data in console but unable to show in tableview javafx. Please help.
Class RoomDetails
Class RoomDetails
package application;
public class RoomDetails<object> {
rmlist rmList = new rmlist();
getroom getRoom = new getroom();
@FXML
private Button btnRoom_add;
@FXML
private Button btnRoom_update;
@FXML
private Button btnRoom_del;
@FXML
private TableView<ListRoom> tblroomdetails;
@FXML
private TableColumn<object, Object> tblroomno;
@FXML
private TableColumn<object, Object> tblroomtype;
@FXML
private TableColumn<object, Object> tblac;
@FXML
private TableColumn<object, Object> tbltariff;
@FXML
private TableColumn<object, Object> tblstatus;
@FXML
private Label lblRoomdetails;
Dbconnection dbcon = new Dbconnection();
Connection con;
PreparedStatement pst;
ResultSet rs;
@FXML
public void btnRoom_addonAction(ActionEvent event) throws IOException{
FXMLLoader fXMLLoader = new FXMLLoader();
fXMLLoader.setLocation(getClass().getResource("/application/AddRoom.fxml"));
try{
fXMLLoader.load();
Parent parent = fXMLLoader.getRoot();
Scene scene = new Scene(parent);
scene.setFill(new Color(0, 0, 0, 0));
AddRoom addRoom = fXMLLoader.getController();
addRoom.lbl_Add_Room.setText("ADD Room");
Stage stage = new Stage();
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.initStyle(StageStyle.TRANSPARENT);
stage.show();
}
catch(IOException e){
e.printStackTrace();
}
}
public void viewDetails() {
tblroomdetails.setItems(rmList.roomlist);
getRoom.rmview(rmList);
tblroomno.setCellValueFactory(new PropertyValueFactory<>("roomno"));
tblroomtype.setCellValueFactory(new PropertyValueFactory<>("rmtype"));
tblac.setCellValueFactory(new PropertyValueFactory<>("acstat"));
tbltariff.setCellValueFactory(new PropertyValueFactory<>("rmtariff"));
tblstatus.setCellValueFactory(new PropertyValueFactory<>("rmstatus"));
}
}
班级getroom
package application;
public class getroom {
Dbconnection dbcon = new Dbconnection();
Connection con;
PreparedStatement pst;
ResultSet rs;
public void rmview(rmlist rmList) {
con = dbcon.geConnection();
try{
pst = con.prepareStatement("select * from room");
rs = pst.executeQuery();
System.out.println(rs);
while (rs.next()){
rmList.roomno = rs.getString(1);
rmList.rmtype = rs.getString(2);
rmList.acstat = rs.getString(3);
rmList.rmtariff = rs.getString(4);
rmList.rmstatus = rs.getString(5);
}
rs.close();
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Class ListRoom
Class ListRoom
package application;
public class ListRoom {
public String roomno;
public String rmtype;
public String acstat;
public String rmtariff;
public String rmstatus;
public ListRoom(String roomno, String rmtype, String acstat, String rmtariff, String rmstatus) {
super();
this.roomno = roomno;
this.rmtype = rmtype;
this.acstat = acstat;
this.rmtariff = rmtariff;
this.rmstatus = rmstatus;
}
public String getRoomno() {
return roomno;
}
public void setRoomno(String roomno) {
this.roomno = roomno;
}
public String getRmtype() {
return rmtype;
}
public void setRmtype(String rmtype) {
this.rmtype = rmtype;
}
public String getAcstat() {
return acstat;
}
public void setAcstat(String acstat) {
this.acstat = acstat;
}
public String getRmtariff() {
return rmtariff;
}
public void setRmtariff(String rmtariff) {
this.rmtariff = rmtariff;
}
public String getRmstatus() {
return rmstatus;
}
public void setRmstatus(String rmstatus) {
this.rmstatus = rmstatus;
}
}
类rmlist
package application;
public class rmlist {
public String roomno;
public String rmtype;
public String acstat;
public String rmtariff;
public String rmstatus;
public ObservableList<ListRoom> roomlist = FXCollections.observableArrayList();
}
class DBConnection
class DBConnection
package application;
class DBconnection{
public Connection con;
String username = "root";
String password = "123456";
String driverclass = "com.mysql.jdbc.Driver";
String db_url = "jdbc:mysql://localhost:3306/";
String unicode= "?useUnicode=yes&characterEncoding=UTF-8&useSSL=false";
public Connection mkDataBase() throws SQLException{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(db_url, username, password);
} catch (Exception e){
}
return con;
}
public Connection geConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(db_url+"hotel"+unicode, username, password);
} catch (ClassNotFoundException | SQLException ex) {
System.out.println("Too Many Connection");
}
return con;
}
}
推荐答案
In您填写的 getRoom.rmview
方法一次又一次地分配 rmview
实例的字段,但您永远不会修改 roomlist
rmview
永远不会被修改;它仍然是空的。由于您在 TableView
中使用 roomlist
,因此无法显示数据。
In the getRoom.rmview
method you fill assign the fields of the rmview
instance again and again, but you never modify the roomlist
of rmview
is never modified; it remains empty. Since you use roomlist
in the TableView
, there is no data to show.
您应该为数据库查询返回的每一行添加一个新元素:
You should instead add a new element for every row the database query returns:
// remove data previously in the list
rmList.roomlist.clear();
while (rs.next()){
rmList.roomlist.add(new ListRoom(rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5)));
}
此外我建议坚持命名约定。特别是关于缩写的部分,因为这使得你的代码难以为他人阅读。此外, object
作为类型参数的名称是一个糟糕的选择。它会导致混淆,通常会将单个大写字母用于类型参数。
Furthermore I recommend adhering to the naming conventions. Especially the part about abreviations since this makes your code hard to read for others. Furthermore object
is a bad choice as name for a type parameter. It leads to confusion and usually single uppercase letters are used for type parameters.
此外还有 rmlist
类的用途目前还不清楚。它包含与 ListRoom
相同的字段,但也包含您希望实际存储数据的列表。你为什么需要这些领域?你需要 rmlist
还是只能用 ObservableList< ListRoom>
替换?
Also the purpose of the rmlist
class is unclear. It contains the same fields as ListRoom
, but also contains a list where you want the data to actually be stored. Why do you need those fields? Do you need rmlist
at all or could it just be replaced with ObservableList<ListRoom>
?
这篇关于javafx Tableview未显示数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!