无法执行算法和更新表

时间:2013-01-30 08:56:20

标签: java swing ms-access jdbc defaulttablemodel

运行时没有错误,但我的event_ID是1,2,3,4,5,6,但控制台显示

  

更新crossEvent SET event_Seat = '10'WHERE event_ID = 0

并且由于event_ID

的不同,我的event_Seat为20并未更新为10
    public TicketBooking(int eventID){
        this.eventID = eventID;
    }
    /**
     * This method initializes jButtonSubmit
     * 
     * @return javax.swing.JButton
     */
    private JButton getJButtonSubmit() {
        if (jButtonSubmit == null) {
            jButtonSubmit = new JButton();
            jButtonSubmit.setBounds(new Rectangle(517, 317, 79, 22));
            jButtonSubmit.setText("Submit");
            jButtonSubmit
                    .addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent e) {
                            {
                                submitRegisteration(e);
                            }
                        }
                    });
        }
        return jButtonSubmit;
    }

    public void submitRegisteration(ActionEvent e) {
        String firstName = getJTextFieldFirstName().getText();
        String lastName = getJTextFieldLastName().getText();
        String Contact = getJTextFieldContact().getText();
        String Nric = getJTextFieldNRIC().getText();
        String event = jTextArea1.getText().toString();         

        if (firstName.equals("") || lastName.equals("")) {
            JOptionPane.showMessageDialog(null, "Please enter all text field");
        }

        else {
            bookingEventUser user = new bookingEventUser(firstName, lastName, Contact, Nric, event , eventID);
            if (user.createUser() == true)
                TicketAvailability();

            JOptionPane.showMessageDialog(null,
                    "User record created successfully!");
        }   
    }       

    public void TicketAvailability(){
        String  newseats = jComboBox1.getSelectedItem().toString();
        int seats = Integer.parseInt(newseats);
        DBController db = new DBController();
        db.setUp("IT Innovation Project: Database");
        ResultSet rs = null;
        String dbQuery = "SELECT event_Seat FROM crossEvent WHERE event_ID = "+eventID+"  ";

        rs = db.readRequest(dbQuery);
        int seatAvailable=0;
        try{
            while(rs.next()){
                seatAvailable = Integer.parseInt(rs.getString("event_Seat"));//rs.getString is retrieving data from database and making it to String
            }
            rs.close();
        }catch(Exception e1){

            }
            int resultSeat = seatAvailable - seats ;

            Event update = new Event(resultSeat);
            update.updateSeat();
            }       

    /**
     * This method initializes jTextArea    
     *  
     * @return javax.swing.JTextArea    
     */
    private JTextArea getJTextArea() {
        if (jTextArea == null) {
            jTextArea = new JTextArea();
            jTextArea.setBounds(new Rectangle(67, 289, 84, 16));
            jTextArea.setText(Integer.toString(eventID));
            jTextArea.setVisible(false);
        }
        return jTextArea;
    }
    /**
     * This method initializes jTextArea1   
     *  
     * @return javax.swing.JTextArea    
     */
    private JTextArea getJTextArea1() {
        if (jTextArea1 == null) {
            jTextArea1 = new JTextArea();
            jTextArea1.setBounds(new Rectangle(195, 205, 127, 26));
            jTextArea1.setEditable(false);              
        }
        return jTextArea1;
    }
    public void SetEventTitle(){
        db.setUp("IT Innovation Project: Database");
        String sql = "Select event_Title from crossEvent WHERE event_ID ="+ eventID +"";
            ResultSet rs = null;
        rs = db.readRequest(sql);
        try{
                while(rs.next()){
                    jTextArea1.setText(rs.getString("event_Title"));

                }
                rs.close();
    }catch(Exception e){
        System.out.println(e);

    }   
    }       
    public static void main(String args[]) {
        TicketBooking myWindow = new TicketBooking(eventID);
        myWindow.getJFrame().setVisible(true);
    }    
}

public class Event {        

    private int resultSeat;
    private int eventID;

    public Event(int resultSeat) {
        this.resultSeat = resultSeat;

    }
    public boolean updateSeat(){
        boolean success = false;
        DBController db = new DBController();
        db.setUp("IT Innovation Project: Database");
        String sql = "UPDATE crossEvent SET event_Seat = '" + resultSeat + "' WHERE event_ID ="+ eventID+ "";
        System.out.println(sql);
        if (db.updateRequest(sql) == 1)
            success = true;
        db.terminate();
        return success;         
    }    
}

2 个答案:

答案 0 :(得分:0)

您从未为eventID分配任何值。在Java中,int的默认值为0.

答案 1 :(得分:0)

eventID的{​​{1}}字段似乎隐藏了外部类中具有相同名称的字段

Event