javafx Tableview未显示数据库中的数据

时间:2016-07-15 14:01:03

标签: java mysql javafx tableview

我的表格视图无法在其中显示数据。它在控制台中显示结果集数据但无法在tableview javafx中显示。请帮忙。

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();
            }
    }
    }

班级列表

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;
    }


}

Class 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

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;
            }
        }

1 个答案:

答案 0 :(得分:1)

getRoom.rmview方法中,您一次又一次地分配rmview个实例的字段,但您永远不会修改roomlist的{​​{1}}从未修改过;它仍然是空的。由于您在rmview中使用roomlist,因此无法显示数据。

您应该为数据库查询返回的每一行添加一个新元素:

TableView

此外,我建议遵守the naming conventions。特别是关于缩写的部分,因为这使得你的代码难以为他人阅读。此外,// 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类的目的还不清楚。它包含与rmlist相同的字段,但也包含您希望实际存储数据的列表。你为什么需要这些领域?您是否需要ListRoom或者只能将其替换为rmlist