Problems with local variable scope. How to solve it?

后端 未结 4 1755
生来不讨喜
生来不讨喜 2020-11-28 09:23

I\'m getting the following error when trying to execute statemet.executeUpdate() in my code:

Local variable statement defined in an enclosing sc         


        
4条回答
  •  囚心锁ツ
    2020-11-28 09:33

    I found this approach useful. This way you do not need a class nor final

     btnInsert.addMouseListener(new MouseAdapter() {
            private Statement _statement;
    
            public MouseAdapter setStatement(Statement _stmnt)
            {
                _statement = _stmnt;
                return this;
            }
            @Override
            public void mouseDown(MouseEvent e) {
                String name = text.getText();
                String from = text_1.getText();
                String to = text_2.getText();
                String price = text_3.getText();
    
                String query = "INSERT INTO booking (name, fromst, tost, price) VALUES ('"+name+"', '"+from+"', '"+to+"', '"+price+"')";
                try {
                    _statement.executeUpdate(query);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }.setStatement(statement));
    

提交回复
热议问题