Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Deleting record from database HELP! :(

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Deleting record from database HELP! :(

    Hi everyone, I am new here. I hope I am in the right place about my problem.
    Okay here it is, I have this Employee Information System, and I already have things almost done, but my problem is when hitting the delete button, it automatically deletes the record without confirmation.



    By the way, this is the code for my delete button. Please help me what I should do to give this a confirmation yes or no prompt before deleting. Your help will be appreciated. Thanks!

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     
        datacon.GetConnection();
     
           String q = "DELETE from info WHERE ( IDno = '" + jComboBox1.getSelectedItem() +"')";
     
            try
            {
                st = datacon.GetConnection().createStatement();
                st.executeUpdate(q);
     
                st.close();
                datacon.GetConnection().close();
                JOptionPane.showMessageDialog(null, " Successfully  Deleted!");
            }
            catch(SQLException ex)
            {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
     
     
     
                    firstn.setText(null);
                    middlen.setText(null);
                    lastn.setText(null);
                    eadd.setText(null);
                    hadd.setText(null);
                    city.setText(null);
                    zip.setText(null);
                    country.setText(null);
                    hphone.setText(null);
                    workphone.setText(null);
                    sss.setText(null);
                    hiredate.setText(null);
                    salary.setText(null);
                    dept.setText(null);
                    supervisor.setText(null);
                    econtact.setText(null);
                    ephone.setText(null);[QUOTE][QUOTE][/QUOTE][/QUOTE]
    Last edited by shando1992; March 1st, 2011 at 07:47 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Deleting record from database HELP! :(

    See How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) It explains how to easily create a confirmation dialog. You can create the dialog, retrieve the user input (as in integer), and use this only if the value has been confirmed.
    A simple example you can work from
    JOptionPane.showConfirmDialog(null,    "choose one", "choose one", JOptionPane.YES_NO_OPTION);
    if ( n == JOptionPane.YES_OPTION ){
    //option was yes
    }
    Last edited by copeg; March 1st, 2011 at 07:54 PM.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deleting record from database HELP! :(

    Hey, thank you so much for the response, I'll give it a go!

  4. #4
    Junior Member
    Join Date
    Mar 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Deleting record from database HELP! :(

    Hi again, I have read the article that you suggested, and I came up to this problem. I have successfully made a prompt for delete confirmation that lets you choose YES and NO, but the problem is that, even if I chose the option NO, it still deletes the record, it's like same function as the first option (YES button).

    So uhmn I just want to clarify if something is wrong with my codes, I'm just new to java and this is one of our assignments. I'm really having hard time figuring this out. I hope you can help me with this one too. Thanks so much!

    datacon.GetConnection();
     
            JOptionPane.showConfirmDialog(null,    "Delete Record?", "Delete Confirmation", JOptionPane.YES_NO_OPTION);
            int n = 0;
    if ( n == JOptionPane.YES_OPTION ){
    String q = "DELETE from info WHERE ( IDno = '" + jComboBox1.getSelectedItem() +"')";
     
     
     
            try
            {
                st = datacon.GetConnection().createStatement();
                st.executeUpdate(q);
     
     
     
                st.close();
                datacon.GetConnection().close();
                JOptionPane.showMessageDialog(null, " Successfully  Deleted!");
            }
            catch(SQLException ex)
            {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
     
     
     
                    firstn.setText(null);
                    middlen.setText(null);
                    lastn.setText(null);
                    eadd.setText(null);
                    hadd.setText(null);
                    city.setText(null);
                    zip.setText(null);
                    country.setText(null);
                    hphone.setText(null);
                    workphone.setText(null);
                    sss.setText(null);
                    hiredate.setText(null);
                    salary.setText(null);
                    dept.setText(null);
                    supervisor.setText(null);
                    econtact.setText(null);
                    ephone.setText(null);
     
    }
    else if ( n == JOptionPane.NO_OPTION ){
     
                    firstn.setText("");
                    middlen.setText("");
                    lastn.setText("");
                    eadd.setText("");
                    hadd.setText("");
                    city.setText("");
                    zip.setText("");
                    country.setText("");
                    hphone.setText("");
                    workphone.setText("");
                    sss.setText("");
                    hiredate.setText("");
                    salary.setText("");
                    dept.setText("");
                    supervisor.setText("");
                    econtact.setText("");
                    ephone.setText("");
     
     
     
        }
     
     
     
    }

Similar Threads

  1. renaming/deleting file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 15th, 2011, 12:18 AM
  2. Drag & drop MVC: deleting an object
    By Alice in forum Object Oriented Programming
    Replies: 0
    Last Post: December 9th, 2010, 11:09 PM
  3. [SOLVED] Can someone verify if this code for deleting a BST works?
    By scottb80 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 2nd, 2010, 10:19 AM
  4. how to delete record from checking checkbox value.
    By -_- in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: March 12th, 2010, 07:09 AM
  5. Jsp code which will enable to view a single row from the Database
    By hundu in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 5th, 2009, 07:56 AM