当前光标位置的无效操作 - JAVA DB

时间:2015-09-29 16:09:57

标签: java database

我正在创建一个可以查看帐户记录的程序。我可以添加,搜索和删除记录而不会出现任何错误。但是当涉及到编辑记录时,我得到错误:当前光标位置的操作无效。但是当我添加一条记录,然后编辑一条记录时,就没有错误。

这是连接:

public void DoConnect(){
    try {
        String host = "jdbc:derby://localhost:1527/CINEMA_TICKETING";
        String uName = "group5";
        String uPass = "group5";

        con = DriverManager.getConnection(host, uName, uPass);
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        String sql = "SELECT * FROM ACCOUNTS";
        rs = stmt.executeQuery(sql);
    }
    catch(SQLException err){
        JOptionPane.showMessageDialog(CINEMA_TICKETING.this, err.getMessage());
    }
}

public void Refresh_RS_STMT(){
    try {
        stmt.close();
        rs.close();
        stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_UPDATABLE);
        String sql = "SELECT * FROM ACCOUNTS";
        rs = stmt.executeQuery(sql);
    }
    catch(SQLException ex){
        Logger.getLogger(CINEMA_TICKETING.class.getName()).log(Level.SEVERE, null, ex);
    }
}

***只是忽略其他声明

以下是帐户记录的代码:

 public ACCOUNTRECORDS() {
    initComponents();
    DoConnect();
    showAll();
 }

 void Get_Set_Records(){
    try {
        nam = rs.getString("NAME");
        bda = rs.getString("BIRTHDATE");
        numb = rs.getString("CONTACT");
        sexxx = rs.getString("GENDER");
        ema = rs.getString("EMAIL");
        una = rs.getString("USERNAME");  
        pas = rs.getString("PASSWORD");  
        secu = rs.getString("SECURITY"); 
        acctt = rs.getString("ACCOUNT");  

        userlabel.setText(una);
        passlabel.setText(pas);
        accountlabel.setSelectedItem(acctt);
        emaillabel.setText(ema);
        namelabel.setText(nam);
        birthdaylabel.setText(bda);
        contactlabel.setText(numb);
        genderlabel.setSelectedItem(sexxx);
        securitylabel.setText(secu);
    }
    catch(SQLException err){
        JOptionPane.showMessageDialog(ACCOUNTRECORDS.this, err.getMessage());
    }
}

void showAll(){
    try {
        userlabel.setEnabled(false);
        passlabel.setEnabled(false);
        accountlabel.setEnabled(false);
        emaillabel.setEnabled(false);
        namelabel.setEnabled(false);
        birthdaylabel.setEnabled(false);
        contactlabel.setEnabled(false);
        genderlabel.setEnabled(false);
        securitylabel.setEnabled(false);

        first.setEnabled(true);
        prev.setEnabled(true);
        next.setEnabled(true);
        last.setEnabled(true);

        add.setEnabled(true);
        search.setEnabled(true);
        edit.setEnabled(true);
        delete.setEnabled(true);

        save_edit.setEnabled(false);
        cancel_edit.setEnabled(false);
        save_add.setEnabled(false);
        cancel_add.setEnabled(false);

        rs.first();
        Get_Set_Records();
    }
    catch(SQLException e){
        System.out.println(e);
    }
}

  private void searchActionPerformed(java.awt.event.ActionEvent evt) {                                       
    usernameee = JOptionPane.showInputDialog(this, "Please type the username: ");
    try {
        Refresh_RS_STMT();
        while(rs.next()){
            una = rs.getString("USERNAME");
            if(usernameee.equals(una)){
                tempuserr = usernameee;
                Get_Set_Records();
            }
        }
        if(usernameee.equals(tempuserr) == false){
            showAll();
            Get_Set_Records();
            JOptionPane.showMessageDialog(this, "Movie record not found!");
        }
    } catch(SQLException err){
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                      

private void addActionPerformed(java.awt.event.ActionEvent evt) {                                    
    try {
        curRow = rs.getRow();

        userlabel.setEnabled(true);
        passlabel.setEnabled(true);
        accountlabel.setEnabled(true);
        emaillabel.setEnabled(true);
        namelabel.setEnabled(true);
        birthdaylabel.setEnabled(true);
        contactlabel.setEnabled(true);
        genderlabel.setEnabled(true);
        securitylabel.setEnabled(true);

        userlabel.setText(null);
        passlabel.setText(null);
        accountlabel.setSelectedIndex(0);
        emaillabel.setText(null);
        namelabel.setText(null);
        birthdaylabel.setText(null);
        contactlabel.setText(null);
        genderlabel.setSelectedIndex(0);
        securitylabel.setText(null);

        first.setEnabled(false);
        prev.setEnabled(false);
        next.setEnabled(false);
        last.setEnabled(false);

        add.setEnabled(false);
        search.setEnabled(false);
        edit.setEnabled(false);
        delete.setEnabled(false);

        save_edit.setEnabled(false);
        cancel_edit.setEnabled(false);
        save_add.setEnabled(true);
        cancel_add.setEnabled(true);

    } catch(SQLException err){
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try {
    rs.close();
    ACCOUNTRECORDS.this.dispose();
   } catch (SQLException err){
    System.out.println(err.getMessage());
   }

}                                        

private void cancel_addActionPerformed(java.awt.event.ActionEvent evt) {                                           
    showAll();
}                                          

private void lastActionPerformed(java.awt.event.ActionEvent evt) {                                     
    try {
        rs.last();
        Get_Set_Records();
    } catch(SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                    

private void nextActionPerformed(java.awt.event.ActionEvent evt) {                                     
    try {
        if(rs.next()){
            Get_Set_Records();
        }
        else {
            rs.previous();
            JOptionPane.showMessageDialog(this, "End of File");
        }
    } catch(SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                    

private void prevActionPerformed(java.awt.event.ActionEvent evt) {                                     
    try {
        if(rs.previous()){
            Get_Set_Records();
        }
        else {
            rs.next();
            JOptionPane.showMessageDialog(this, "Start of File");
        }
    } catch(SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                    

private void firstActionPerformed(java.awt.event.ActionEvent evt) {                                      
    try {
        rs.first();
        Get_Set_Records();
    } catch(SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}                                     

private void save_addActionPerformed(java.awt.event.ActionEvent evt) {                                         
    uuna = userlabel.getText();
    ppas = passlabel.getText();
    //aacctt = accountlabel.getText();
    eema = emaillabel.getText();
    nnam = namelabel.getText();
    bbda = birthdaylabel.getText();
    nnumb = contactlabel.getText();
    //        ssexxx = genderlabel.getText();
    ssecu = securitylabel.getText();


    if (accountlabel.getSelectedIndex()==1){
        aacctt = "Admin";
    }
    else if (accountlabel.getSelectedIndex()==2){
        aacctt = "Cashier";
    }
    else if (accountlabel.getSelectedIndex()==3){
        aacctt = "Member";
    }

    if (genderlabel.getSelectedIndex() == 1){
        ssexxx = "Female";
    }
    else if (genderlabel.getSelectedIndex() == 2){
        ssexxx = "Male";
    }

    if(userlabel.getText().trim().isEmpty() || passlabel.getText().trim().isEmpty() ||
        accountlabel.getSelectedIndex() == 0 || emaillabel.getText().trim().isEmpty() ||
        namelabel.getText().trim().isEmpty() || birthdaylabel.getText().trim().isEmpty() ||
        contactlabel.getText().trim().isEmpty() || genderlabel.getSelectedIndex() == 0 ||
        securitylabel.getText().trim().isEmpty()){
        JOptionPane.showMessageDialog(this, "Some field/s is/are blank");
    }
    else {
        try {
            rs.moveToInsertRow();
            rs.updateString("USERNAME", uuna);
            rs.updateString("PASSWORD", ppas);
            rs.updateString("ACCOUNT", aacctt);
            rs.updateString("EMAIL", eema);
            rs.updateString("NAME", nnam);
            rs.updateString("BIRTHDATE", bbda);
            rs.updateString("CONTACT", nnumb);
            rs.updateString("GENDER", ssexxx);
            rs.updateString("SECURITY", ssecu);
            rs.insertRow();
            Refresh_RS_STMT();
            showAll();
            JOptionPane.showMessageDialog(this, "Account record has been saved!");
        } catch(SQLException err){
            System.out.println(err.getMessage());
        }
    }
}                                        

private void cancel_editActionPerformed(java.awt.event.ActionEvent evt) {                                            
    showAll();
}                                           

private void editActionPerformed(java.awt.event.ActionEvent evt) {                                     
    userlabel.setEnabled(true);
    passlabel.setEnabled(true);
    accountlabel.setEnabled(true);
    emaillabel.setEnabled(true);
    namelabel.setEnabled(true);
    birthdaylabel.setEnabled(true);
    contactlabel.setEnabled(true);
    genderlabel.setEnabled(true);
    securitylabel.setEnabled(true);

    first.setEnabled(false);
    prev.setEnabled(false);
    next.setEnabled(false);
    last.setEnabled(false);

    add.setEnabled(false);
    search.setEnabled(false);
    edit.setEnabled(false);
    delete.setEnabled(false);

    save_edit.setEnabled(true);
    cancel_edit.setEnabled(true);
    save_add.setEnabled(false);
    cancel_add.setEnabled(false);
}                                    

private void save_editActionPerformed(java.awt.event.ActionEvent evt) {                                          
    uuna = userlabel.getText();
    ppas = passlabel.getText();
    aacctt = accountlabel.getSelectedItem().toString();
    eema = emaillabel.getText();
    nnam = namelabel.getText();
    bbda = birthdaylabel.getText();
    nnumb = contactlabel.getText();
    ssexxx = genderlabel.getSelectedItem().toString();
    ssecu = securitylabel.getText();



    if(userlabel.getText().trim().isEmpty() || passlabel.getText().trim().isEmpty() ||
        accountlabel.getSelectedIndex() == 0 || emaillabel.getText().trim().isEmpty() ||
        namelabel.getText().trim().isEmpty() || birthdaylabel.getText().trim().isEmpty() ||
        contactlabel.getText().trim().isEmpty() || genderlabel.getSelectedIndex() == 0 ||
        securitylabel.getText().trim().isEmpty()){
        JOptionPane.showMessageDialog(this, "Some field/s is/are blank");
    }
    else {
        try {
            //rs.moveToCurrentRow();
            rs.updateString("USERNAME", uuna);
            rs.updateString("PASSWORD", ppas);
            rs.updateString("ACCOUNT", aacctt);
            rs.updateString("EMAIL", eema);
            rs.updateString("NAME", nnam);
            rs.updateString("BIRTHDATE", bbda);
            rs.updateString("CONTACT", nnumb);
            rs.updateString("GENDER", ssexxx);
            rs.updateString("SECURITY", ssecu);

            rs.updateRow();
            Refresh_RS_STMT();
            showAll();
            JOptionPane.showMessageDialog(this, "Account record has been updated!");
        } catch(SQLException err){
            System.out.println(err.getMessage());
        }
    }
}                                         

private void deleteActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        ff = JOptionPane.showConfirmDialog(this, "Are you sure you "
            + "want to delete this selected account record?");
        if(ff == JOptionPane.YES_OPTION) {
            rs.deleteRow();
            Refresh_RS_STMT();
            showAll();
            JOptionPane.showMessageDialog(this, "Selected account record has been deleted!");
        }
        else {
            rs.first();
            Get_Set_Records();
        }
    } catch(SQLException err) {
        JOptionPane.showMessageDialog(this, err.getMessage());
    }
}             

0 个答案:

没有答案