javafx Tableview not showing data from database

ⅰ亾dé卋堺 提交于 2019-12-13 10:51:33

问题


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

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"));

}       
}

Class 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

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:


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

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.

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>?



来源:https://stackoverflow.com/questions/38398050/javafx-tableview-not-showing-data-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!