Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: How to clear textfield after successfully inserting data to database

  1. #1
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default How to clear textfield after successfully inserting data to database

    My program clears textfield even when data are not successfully inserted into the database. obviously my logic is wrong. i need help pls.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to clear textfield after successfully inserting data to database

    Quote Originally Posted by victaino View Post
    My program clears textfield even when data are not successfully inserted into the database. obviously my logic is wrong. i need help pls.
    Yep your logic is wrong.

    If you need more help, consider telling us more and showing code.

  3. #3
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: How to clear textfield after successfully inserting data to database

    Am new to programming and java please consider helping me

    Java Code:
       private boolean Update = false;
    
    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
            // adding data to database
            // create connection and prepare statement
              
            try{ 
                
                connection = DriverManager.getConnection(DATABASE_URL);
                insertNewData = connection.prepareStatement("INSERT INTO Customer" +
                   "(Name, Phone,Email, Address, Fax, Website, Remarks)" +
                    "VALUES (?,?,?,?,?,?,?)");   
             
                // declare variables to store textfield data
                String t1,t2,t3,t4,t5,t6,t7;
                t1 = txtName.getText();
                t2 = txtPhone.getText();
                t3 = txtEmail.getText();
                t4 = txaAddress.getText();
                t5 = txtFax.getText();
                t6 = txtWebsite.getText();
                t7 = txaRemarks.getText();
            
            
             //add entries
            int result = 0;
            
           // set parameters, then execute insertNewData
            
            insertNewData.setString(1,t1);
            insertNewData.setString(2, t2);
            insertNewData.setString(3, t3);
            insertNewData.setString(4, t4);
            insertNewData.setString(5, t5);
            insertNewData.setString(6, t6);
            insertNewData.setString(7, t7);
            
            
            // insert the new data
            result = insertNewData.executeUpdate();
            
           
            }catch (Exception e){
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            
                try{
                connection.close();
                Update = true;
                }
                catch(SQLException sql){
                
                
            }
                
                    
            lblUpdate.setText("Record Added");
            
            // reset text fields
            txtName.setText("");
            txtPhone.setText("");
            txtEmail.setText("");
            txaAddress.setText("");
            txtFax.setText("");
            txtWebsite.setText(""); 
            txaRemarks.setText("");
            txtFilter.setText("");
                
                // call method refresh
                Refresh();
            }
               
        }

  4. #4
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: How to clear textfield after successfully inserting data to database

    Am new to programming and java please consider helping me

    Java Code:
       private boolean Update = false;
    
    private void jButton16ActionPerformed(java.awt.event.ActionEvent evt) {                                          
            // TODO add your handling code here:
            // adding data to database
            // create connection and prepare statement
              
            try{ 
                
                connection = DriverManager.getConnection(DATABASE_URL);
                insertNewData = connection.prepareStatement("INSERT INTO Customer" +
                   "(Name, Phone,Email, Address, Fax, Website, Remarks)" +
                    "VALUES (?,?,?,?,?,?,?)");   
             
                // declare variables to store textfield data
                String t1,t2,t3,t4,t5,t6,t7;
                t1 = txtName.getText();
                t2 = txtPhone.getText();
                t3 = txtEmail.getText();
                t4 = txaAddress.getText();
                t5 = txtFax.getText();
                t6 = txtWebsite.getText();
                t7 = txaRemarks.getText();
            
            
             //add entries
            int result = 0;
            
           // set parameters, then execute insertNewData
            
            insertNewData.setString(1,t1);
            insertNewData.setString(2, t2);
            insertNewData.setString(3, t3);
            insertNewData.setString(4, t4);
            insertNewData.setString(5, t5);
            insertNewData.setString(6, t6);
            insertNewData.setString(7, t7);
            
            
            // insert the new data
            result = insertNewData.executeUpdate();
            
           
            }catch (Exception e){
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            
                try{
                connection.close();
                Update = true;
                }
                catch(SQLException sql){
                
                
            }
                
                    
            lblUpdate.setText("Record Added");
            
            // reset text fields
            txtName.setText("");
            txtPhone.setText("");
            txtEmail.setText("");
            txaAddress.setText("");
            txtFax.setText("");
            txtWebsite.setText(""); 
            txaRemarks.setText("");
            txtFilter.setText("");
                
                // call method refresh
                Refresh();
            }
               
        }

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to clear textfield after successfully inserting data to database

    What happens if you move your block of code that calls xxxx.setText("") above the catch block and right after result = insertNewData.executeUpdate();? this way if an exception occurs, xxxx. setText("") code is never called.

    Do you see how your question is absolutely impossible to answer without you giving us the information requested? Consider reading my link below on how to ask smart questions, so we don't have to ask you this in the future, and you'll be able to save us and yourself a lot of time.
    Vinx likes this.

  6. #6
    victaino is offline Member
    Join Date
    Apr 2012
    Posts
    38
    Rep Power
    0

    Default Re: How to clear textfield after successfully inserting data to database

    Thank you so much. u are a life saver.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to clear textfield after successfully inserting data to database

    You're welcome!

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2012, 11:54 AM
  2. Inserting Data in database
    By Marty Jones in forum NetBeans
    Replies: 3
    Last Post: 01-03-2012, 01:33 PM
  3. Inserting Data in database
    By Marty Jones in forum JDBC
    Replies: 2
    Last Post: 12-31-2011, 02:55 PM
  4. data is not inserting into database
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 08-21-2010, 04:05 PM
  5. Inserting data to database from the form
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-17-2008, 02:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •