-1

i have a signup page connected to sql database.now i want to have validations in signup page like firstname,lastname,username etc can not be empty using java how can i do that My code is

String fname=Fname.getText();
    String lname=Lname.getText();
    String uname=Uname.getText();
    String emailid=Emailid.getText();
    String contact=Contact.getText();
    String pass=String.valueOf(Pass.getPassword());
    Connection conn=null;
    PreparedStatement pstmt=null;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/zeeshan","root","sHaNi97426");
        pstmt=conn.prepareStatement("Insert into signup1 values(?,?,?,?,?,?)");
        pstmt.setString(1,fname);
        pstmt.setString(2,lname);
        pstmt.setString(3,uname);
        pstmt.setString(4,emailid);
        pstmt.setString(5,contact);
        pstmt.setString(6,pass);
        int i=pstmt.executeUpdate();
        if(i>0)
        {
            JOptionPane.showMessageDialog(null,"Successfully Registered");
        }
        else
        {
            JOptionPane.showMessageDialog(null,"Error");
        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,e);
    } 
zeeshan nisar
  • 553
  • 2
  • 4
  • 18
  • Suggestion: use proper capitalization and punctuation in your question to make it easier to understand. Otherwise people will ask things like "What is your question?" etc. Besides that: _what did you try?_ Checking a string for `null` or being empty shouldn't be too hard. – Thomas Mar 30 '16 at 09:12

4 Answers4

0

First your question is not direct. Validation occurs before database query. You should not proceed to database Connetction or making any query.

What should you do:

public static boolean nullOrEmpty(String value) {
        return value == null || value.trim().equals("") ? true : false;
    }
public void yourMethod(){
    try{
        //YourCode Here
        String fname=Fname.getText();

        if(nullOrEmpty(fname)){
            new throw ValidationException("First name should not be null.");
        }
        //YourCode Here
   }catch(ValidationException e){
     System.err.println("Exception:"+e.getMessage());
   }
}

Check for every string to validate.

Sarz
  • 1,970
  • 4
  • 23
  • 43
0

that should not be hard, you can do it with simple if and else like below

   if(fname != null && fname.isEmpty()){
                throw new Exception(fname+" cannot be empty");
            }else if(lname != null && lname.isEmpty()){
                throw new Exception(fname+" cannot be empty");
            }
   .....

as a recommendation you should abstract validation and database access objects . see example of MVC here

Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
0

You may do it just by downloading a jar named org.apache.commons.lang Stringutils Class Reference

Sample Code

    StringUtils.isBlank(null)      = true
    StringUtils.isBlank("")        = true  
    StringUtils.isBlank(" ")       = true  
    StringUtils.isBlank("bob")     = false  
    StringUtils.isBlank("  bob  ") = false

or

  StringUtils.isEmpty(obj_String); // Another method to check either null or "";
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
-1

To check if a String is empty you can use the method .isEmpty(). You'll probably want to use .trim() first, as this removes all the whitespaces at the beginning and ending of the String. For more options check out the full documentation here.

Jur Clerkx
  • 698
  • 4
  • 14