1

i am trying to create a simple signup form in java. I have made a table in sql in which i have selected not to allow null values but when i signup without entering any values no exception is being given .... all the null values are being excepted..!! What should i do?

private void b1ActionPerformed(java.awt.event.ActionEventevt)                          {                                   
try{


        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con = DriverManager.getConnection("jdbc:odbc:mydsn1");
        PreparedStatement pstmt= con.prepareStatement("insert into project.dbo.signup values(?,?,?,?,?,?)");

        pstmt.setString(1,t1.getText());
        pstmt.setString(2,t2.getText());
        pstmt.setString(3,String.valueOf(t3.getText()));
        pstmt.setString(4,t4.getText());
        pstmt.setString(5,t5.getText());
        pstmt.setString(6,c1.getSelectedItem().toString());

        pstmt.executeUpdate();

    }

    catch(SQLException e)
    {
        JOptionPane.showMessageDialog(null,e.toString());           
    }
      catch(Exception ob)

    {

        JOptionPane.showMessageDialog(null,ob.toString());
    }

}                                   

3 Answers 3

1

Null and empty are not same. If you set a column value as not null then you can not insert any null or you must enter a value. But that doesn't mean that you cannot enter emptry string..

The example of null and emptry string

String s = null;
String s = "";

So in your case, when you are not inserting any input in the TextBox and getting the value using getText() it is returning an empty string not null. I hopy You understood the differenc

So to have an exception you can do one thing. Check the input string if the length is 0 then don't insert it , or insert a null value there like

String s = t1.getText();
if(s.length() == 0)
    pstmt.setString(1, null);  // or dont insert
Sign up to request clarification or add additional context in comments.

2 Comments

then how should i imlement what i want..?
there is a function getSelectedItem for a textField ...it wuld return a null value for an empty textfield
0

t1.getText() return empty value "" not null. For that you need to check the empty value like

"".equals(t1.getText())

5 Comments

i am an amatuer at java ...it wud be very helpful if u cud edit the code above?
String text=t1.getText(); text="".equels(text)? null: text; pstm.setString(1, text); ///Do it for all other filed and assign to pstm.
i wud have to do the code for every textfield..!! isnt there any compact way to do that!!
If your all fields are String then use for-loop.
There is a function getSelectedText .it returns null if the textField is blank..!! It pretty much solved my query ..Thnx for your answer it was really helping..!!
0

getText() method, infact, never return null. It returns CharSequence whose contents may be empty.

Instead of doing getText().toString().equals("") or vice-versa, it may be faster to do getText().length() == 0

1 Comment

i wud have to do the code for every textfield..!! isnt there any compact way to do that!!

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.