1

i have a mysql database named tutorial which contain a table "devices", i have a swing programme, which insert a row to the table through text box. it works fine for inserting a row, but when i leave the textbox blank and click "ok" button, an empty column is added into the row.how to avoid adding empty row.and avoid adding the row even if a single textbox is empty. i have already defined not null constraint in mysql.plz help.

here is my code:

jb.addActionListener(new ActionListener(){

     public void actionPerformed( ActionEvent e ) {
         try {
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("Driver loading success!");
                String url = "jdbc:mysql://localhost:3306/tutorial";
                String name = "root";
                String password = "ranjini123";
        try {
                java.sql.Connection con = DriverManager.getConnection(url, name, password);
                System.out.println("Connected.");
                String text1=jt1.getText();
                String text2=jt2.getText();
                String text3=jt3.getText();
                String text4=jt4.getText();
                String text5=jt5.getText();
                ps = con.prepareStatement (
                    "INSERT INTO devices (asset_id,name,project,emp_id,emp_name) VALUES(?,?,?,?,?)");
                  try{ 

                          ps.setString (1, text1);
                          ps.setString (2, text2);
                          ps.setString (3, text3);
                          ps.setString (4, text4);
                          ps.setString(5,text5);                                      
                          ps.executeUpdate(); 
                          JOptionPane.showMessageDialog(null,"new device added");


                   }
                   catch(NullPointerException n)
                   {
                       n.printStackTrace();
                   }
            }
            catch (SQLException n)
            {
                n.printStackTrace();
            }
         }
            catch(Exception n) 
            {
                n.printStackTrace();
            }   
     }

});
1
  • 1
    In the ActionListener for the button you need to check the values of the text field to make sure it contains a value. If not you display a JOptionPane with an error message and set focus back on the text field. Commented Nov 25, 2013 at 5:00

1 Answer 1

2

Basically, you'll want to check to see if any of the text fields are null or 0 in length. You need to check to see if they are null here, because it could throw a NullPointerException otherwise...

String text1=jt1.getText();
String text2=jt2.getText();
String text3=jt3.getText();
String text4=jt4.getText();
String text5=jt5.getText();

if (text1 != null && !text1.trim().isEmpty() &&
    text2 != null && !text2.trim().isEmpty() &&
    text3 != null && !text3.trim().isEmpty() &&
    text4 != null && !text4.trim().isEmpty() &&
    text5 != null && !text5.trim().isEmpty()) {
     //... Do insert
 } else {
     // Deal with the fact that one or more of the values are invalid
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.