Friday, 24 May 2019

base64 encode


import java.util.Base64;

class Main {
  public static void main(String[] args) {
  // Encode
String originalInput = "test input";
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
System.out.println(encodedString); // Output will be: c29tZSBzdHJpbmc=

// Decode

byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println(decodedString);
  }
}

Tuesday, 7 May 2019

get email first part in java

class Main {
  public static void main(String[] args) {
    String email="pawan@gmail.com";
    Integer index=email.indexOf('@');
    String name=email.substring(0,index);
    System.out.println("Hello world!"+name);
  }
}

Valid email defination

https://help.returnpath.com/hc/en-us/articles/220560587-What-are-the-rules-for-email-address-syntax-




For an email to be delivered, it must have a valid address that follows certain rules of syntax.

A valid email address has four parts:
  • Recipient name
  • @ symbol
  • Domain name
  • Top-level domain
Recipient name
The recipient name represents an email mailbox that belongs to:
  • A specific person
  • A mailing list
  • A department
  • A role within a company (such as sales or customer service)
The recipient name may be a maximum of 64 characters long and consist of:
  • Uppercase and lowercase letters in English (A-Z, a-z)
  • Digits from 0 to 9
  • Special characters such as ! # $ % & ' * + - / = ? ^ _ ` { |

email id validation with cutom special char

^[ A-Za-z0-9_@./'*#&+-]*$


import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
  public static void main(String[] args) { 
    System.out.println("Hello world!");
    String email="pawan*k@ed-ynamic.com";
    String regex = "^[ A-Za-z0-9_@./'*#&+-]*$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(email);
    System.out.println("--opp---"+ matcher.matches());
    //return matcher.matches();
  }
}


"^[_A-Za-z0-9-\\+_@./'*#&+-]+(\\.[_A-Za-z0-9-_@./'*#&+-]+)*@" + "[A-Za-z0-9-_@./'*#&+-]+(\\.[A-Za-z0-9_@./'*#&+-]+)*(\\.[A-Za-z_@./'*#&+-]{2,})$"
; https://stackoverflow.com/questions/17439917/regex-to-accept-alphanumeric-and-some-special-character-in-javascript https://www.computerhope.com/unix/regex-quickref.htm start and end with alpha numeric
class Main {
  public static void main(String[] args) { 
    System.out.println("Hello world!");
    String email="a-alsaqal@mada.jo.com.in";
    String regex ="^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+[a-zA-Z0-9]$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(email);
    System.out.println("--opp---"+ matcher.matches());
    //return matcher.matches();
  }
}

Sunday, 5 May 2019

email validation java

import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
  public static void main(String[] args) {
    System.out.println("Hello world!");
    String email="pawan,k@gmail.com";
        String regex ="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(email);
    System.out.println("--opp---"+ matcher.matches());
    //status= matcher.matches();
     //System.out.println("Hello world!"+status);
  }
}

Friday, 3 May 2019

JDBC Difference between execute, executeQuery and executeUpdate in JDBC



execute method can be used with any type of SQL statements and it returns a boolean. A true indicates that the execute method returned a result set object which can be retrieved using getResultSet method. false indicates that the query returned an int value or void. execute method can run both select and insert/update statements.
executeQuery method execute statements that returns a result set by fetching some data from the database. It executes only select statements.
executeUpdate method execute sql statements that insert/update/delete data at the database. This method return int value representing number of records affected; Returns 0 if the query returns nothing. The method accepts only non-select statements.