Tuesday, March 20, 2018

Form validation in java


In this tutorial, you will be able to learn to to validate user input i.e. Check if input fields are empty

Please watch the video because there are some task to be done beside code

Code to Form validation in java(Check if input fields are empty)  

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if(username.getText().trim().isEmpty() && password.getText().trim().isEmpty()){
            lbl_username.setText("Username is Empty");
            lbl_password.setText("Password is Empty");
        }
           
           
        else if(username.getText().trim().isEmpty()){
           // JOptionPane.showMessageDialog(null, "Username is Empty");
            lbl_username.setText("Username is Empty");
            //JOptionPane.showMessageDialog(this,"<html><h1 style='font-family: Calibri; font-size: 36pt;'>Username is Empty");
        }
        else if(password.getText().trim().isEmpty()){
            //JOptionPane.showMessageDialog(null, "Password is Empty");
            lbl_password.setText("Password is Empty");
        }
        else{
        try{
          //  Class.forName("com.mysql.jdbc.Driver"); 
           // Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb","root",""); 
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String url="jdbc:sqlserver://localhost:1433;databaseName=testdb;user=sa;password=orchid";
           Connection con = DriverManager.getConnection(url);
            String sql = "Select * from logindatabase where username=? and password = ?";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, username.getText());
            pst.setString(2, password.getText());
            ResultSet rs = pst.executeQuery();
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "Username and Password Matched");
                Success field= new Success();
                field.setVisible(true);
                setVisible(false);
              
            }
            else{
                JOptionPane.showMessageDialog(null, "Username and password not Correct");
                username.setText("");
                password.setText("");
            }
            con.close();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
        }  
    }


Please don't forget To Like, Share and Subscribe my Youtube video and Channel.


Youtube Channel Subscription Click Here

No comments:

Post a Comment