简体   繁体   中英

How do i get variable name of jbutton here“s101”?

I want to get the value of my jbutton and compare it with my database column and set the text of the label

private void s101ActionPerformed(java.awt.event.ActionEvent evt) {                                    

         try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/abc","root","");
            PreparedStatement stm = con.prepareStatement("select * from seats where Seat_id="+s101.getName());
            ResultSet rs = stm.executeQuery(); 
            while(rs.next())
               // if(s101.getAccessibleContext().equals(rs.getString("Seat_id")))
                    {
                        lbl_id.setText(rs.getString("Seat_id"));
                        lbl_name.setText(rs.getString("Seat_name"));
                        lbl_price.setText(rs.getString("Price"));
                        lbl_type.setText(rs.getString("Type"));
                    }    
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }         

I think you want to get the text display on button and compare it with database value. To achieve that you should get source from action event.

JButton button = (JButton) evt.getSource();
String name = button.getText();

Then use name to compare with database.

name.equals(rs.getString("Seat_id")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM