无法执行算法和更新表

Posted

技术标签:

【中文标题】无法执行算法和更新表【英文标题】:Unable to perform algorithm and update table 【发布时间】:2013-01-30 08:56:20 【问题描述】:

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

UPDATE 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;         
        

【问题讨论】:

1) 学习 Java 命名约定并遵守它们 2) 使用适当且连贯的缩进 3) 永远不要默默地忽略异常 4) 发布 SSCCE。 FWIW:使用适当的 LayoutManager 并停止使用 setBounds/setSize/setLocation/setPreferredSize/setMinimumSize/setMaximumSize 运行时没有错误 如果数据库中不存在行,我知道的所有 JDBC 驱动程序都会抛出异常。因此,再次确保你没有默默地捕捉到Exception(可能在你的类 DBController 中的某个地方) 【参考方案1】:

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

【讨论】:

【参考方案2】:

貌似Event类的eventID字段隐藏了外部类中的同名字段

public class Event         

    private int resultSeat;
    private int eventID;  <-- remove this line

【讨论】:

以上是关于无法执行算法和更新表的主要内容,如果未能解决你的问题,请参考以下文章

oracle存储过程中update语句一直在执行中,无法更新完成

oracle 执行存储过程 无法中断 但是是循环执行 怎么办

无法在mysql中指定更新、删除查询的目标表

你能说出为啥 DapperExtensions 更新方法无法执行吗?

无法更新表中的列 它返回单行子查询返回多个

使用 gspread 写入 Google 工作表时无法实现批量更新行