JavaFX表行颜色,数据库连接太多

时间:2016-11-28 18:23:11

标签: mysql javafx tableview

我希望在书籍缺货时打印我的红色行,但每当我尝试新想法来管理时,我都会收到错误:

  

" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:   数据源拒绝建立连接,消息来自   服务器:"连接太多"

即使我尝试在同一个循环中关闭所有连接...... 所以我们走了:

private boolean checkIfOutOfStock(BookDetail book) throws SQLException{
    String query = "select * from tbl_loan where book_id = " + book.getId() + " ";

    dc = new DbConnection();
    conn = dc.connect();

    PreparedStatement checkPst = conn.prepareStatement(query);
    ResultSet checkRs = checkPst.executeQuery(query);

    if(checkRs.next()){
        checkRs.close();
        checkPst.close();
        return true;
    } else 
        {
            checkRs.close();
            checkPst.close();
            return false;
        }
}

@Override
    public void initialize(URL location, ResourceBundle resources) {
        dc = new DbConnection();
        conn = dc.connect();


        selectionModel = editTabPane.getSelectionModel();


        editTableBooks.setRowFactory(tv -> new TableRow<BookDetail>() {
            @Override
            public void updateItem(BookDetail item, boolean empty) {
                super.updateItem(item, empty) ;
                if (item == null) {
                    setStyle("");
                } else
                    try {
                        if (checkIfOutOfStock(item)) {
                            setStyle("-fx-background-color: tomato;");
                        } else {
                            setStyle("");
                        }
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
        });
    }

它工作正常,直到我在桌子上下滑动几次......就像每次我滑动桌子我打开新的连接。知道怎么解决吗?

嘿,你的意思是什么?

editTableBooks.setRowFactory(tv -> new TableRow<BookDetail>() {
        @Override
        public void updateItem(BookDetail item, boolean empty) {
            super.updateItem(item, empty) ;
            Platform.runLater(new Runnable() {

                @Override
                public void run() {


                    if (item == null) {
                        setStyle("");
                    } else
                        try {
                            if (checkIfOutOfStock(item)) {
                                setStyle("-fx-background-color: tomato;");
                            } else {
                                setStyle("");
                            }
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                }
            });

        }
    });

它没有改变任何东西,或者我只是没有改变它:P

0 个答案:

没有答案