使用Java将字段保存到数据库

时间:2014-03-19 21:26:53

标签: java sql ms-access logic

我的应用程序中有一个按钮,应保存到我的数据库中。如果您发现我的代码有任何问题,有人能告诉我吗?它根本没有保存任何东西。

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)
              {

              }         
           }          
        });    

1 个答案:

答案 0 :(得分:0)

首先报告遇到的任何错误

          catch(Exception ex)
          {

          }      

一个无声地消耗错误的catch块通常是一个坏主意。

其次,使用调试器,逐步执行代码并查看正在进行的操作。尝试通过桌面检查来解决这样的问题,即。只是阅读代码通常是非常低效的。在这里,我们没有关键信息,例如我们是否有信心

  rs.moveToInsertRow();

可以预期工作 - 我们无法看到rs如何获得其价值。许多数据库问题仅在运行时发生,数据库连接配置错误或尝试插入与现有数据冲突。因此,只有通过了解代码在测试运行中所做的事情才能解决问题。

在这里,我猜想在你的catch中打印出一些诊断信息会非常有用,否则踩入调试器(或添加跟踪语句)就可以帮助你。