Question

I have a button inside my application that should be saving to my database. Can anyone tell me if you see anything wrong with my code? It is not saving anything at all.

saveButton.addActionListener(
        new ActionListener()
        {
           public void actionPerformed(ActionEvent e)
           {
              //gets text from texfields and saves to instance variables
              String fname = fNameTextBox.getText();
              String lname = lNameTextBox.getText();
              String email = eMailTextBox.getText();
              String signUpDate = signUpTextBox.getText();

              try
              {
                 //moves cursor to new row
                 //rs.moveToInsertRow();

                 //statement that checks if user enters all letters      
                 if(fname.matches("[a-zA-Z]+"))
                 {
                    //statement that checks if user enters all letters      
                    if(lname.matches("[a-zA-Z]+")) 
                    {
                       //statement and actions if user enters a '.'
                       if(email.contains("."))
                       {
                          //gets last period in email
                          int emailDotCheck = email.lastIndexOf(".");

                          //substring to period in variable "emailDotCheck"
                          String extensionCheck = email.substring(emailDotCheck);

                          //statement and actions if user doesn't enter email correctly                 
                          if(!email.contains("@") || !extensionCheck.matches("\\.[a-z]{3}"))
                          {
                             JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                             eMailTextBox.setText("");                                       
                          }

                          else
                          {  
                             //instance variables
                             int month = 100;
                             int day = 100;
                             int year = 10000;

                             //statement and actions if user enters 'signUpDate' in correct format    
                             if(signUpDate.matches("\\d{2}/\\d{2}/\\d{4}"))
                             {
                                //gets substring of instance variables
                                String monthStr = signUpDate.substring(0,2);
                                String dayStr = signUpDate.substring(3,5);
                                String yearStr = signUpDate.substring(6);

                                //parsing intstance variables to Integers         
                                month = Integer.parseInt(monthStr);                       
                                day = Integer.parseInt(dayStr);
                                year = Integer.parseInt(yearStr);

                                //statements and actions if user doesn't enter date in correct format   
                                if(month > 12 || day > 31 || year > 2100)
                                {
                                   JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                                   signUpTextBox.setText("");
                                } 

                                else
                                { 
                                  //String sql4 = "INSERT INTO Table1 (Fname, Lname, [E_mail], [Sign_up_date]) VALUES (fname, lname, email, signUpDate)";

                                    //execute query, assigning specified record in db to 'rs4'
                                   //rs4 = st.executeQuery(sql4);                                       

                                   rs.moveToInsertRow();

                                   //inserts record into db                                     
                                   rs.updateString("Fname", fname);
                                   rs.updateString("Lname", lname);
                                   rs.updateString("E-mail", email);
                                   rs.updateString("Sign_up_date", signUpDate);

                                   //inserts data into db      
                                   rs.insertRow();

                                   //closes statement variable so there won't be a gap in db                                       
                                   st.close();

                                   //closes result set variable so there won't be a gap in db
                                   rs.close();

                                   //create new statement to help us gain access to table in db
                                   st = con.createStatement(rs.TYPE_SCROLL_INSENSITIVE, rs.CONCUR_UPDATABLE);

                                   //statement that selects everything from our table
                                   String sql = "SELECT * FROM Table1";

                                   //execute query, assigning all records in db to 'rs'
                                   rs = st.executeQuery(sql);

                                   //gets next row in db
                                   rs.next();

                                   //sets text in text fields to specified fields in db
                                   fNameTextBox.setText(rs.getString("Fname"));
                                   lNameTextBox.setText(rs.getString("Lname"));
                                   eMailTextBox.setText(rs.getString("E_mail"));
                                   fNameTextBox.setText(rs.getString("Sign_up_date"));
                                }
                             }

                             //statement and actions if user does enter date in correct format        
                             else
                             {
                                JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                                signUpTextBox.setText("");
                             }
                          }
                       }

                       //statement and actions if user doesn't enter email in correct format        
                       else
                       {
                          JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                          eMailTextBox.setText("");
                       }

                    }

                    //statement and actions if user doesnt enter last name in correct format        
                    else
                    {
                       JOptionPane.showMessageDialog(null, "Please enter last name in correct format!");
                       lNameTextBox.setText("");
                    }
                 }

                 //statement and actions if user doesn't enter first name in correct format       
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
                    fNameTextBox.setText("");
                 }
              }
              catch(Exception ex)
              {

              }         
           }          
        });    
Was it helpful?

Solution

Start by reporting any errors that are encountered

          catch(Exception ex)
          {

          }      

a catch block that silently consumes errors is usually a bad idea.

Second, use a debugger, step through the code and see what's going on. Attempting to solve a problem like this by desk-checking, ie. just reading the code is usually very inefficient. Here we don't have key information, such as whether we have confidence that

  rs.moveToInsertRow();

can be expected to work - we can't see how rs gets its value. Many database problems only occur at runtime, there's a misconfiguration of the database connection or an attempted insert collides with existing data. Hence it's only by understanding what your code is doing in your test run that the problem can be solved.

Here I'd guess that printing out some diagnistics in your catch will be really helpful, and otherwise stepping in a debugger (or adding trace statements) will get you there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top