Friday, 3 June 2016

User Defined Exception in Java


                        User Defined Exception in Java


package Excep;

import java.util.Scanner;

public class User_Exception {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter Your Age in Number");

int age = scanner.nextInt();

try {

if (age < 0) {
           
//  Anonymous inner class
throw new ArithmeticException() {

private static final long serialVersionUID = -1962716995912160014L;

@Override
public String toString() {

return "Age can not be negative";
}
};

} else {
System.out.println("Age is ok");
}
} catch (Exception e) {

System.out.println("catch block executed");
e.printStackTrace();

}

finally {

System.out.println("finally block executed");
}

}

}

Thanks
Keep Learning.......... :)

Thursday, 2 June 2016

STRING TO DATE CONVERSION IN JAVA

                       STRING TO DATE CONVERSION IN JAVA



package basic;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {
public static void main(String[] args) {

try {
// STEP 1-take date as a string

String date_is = "29/06/20016";

// STEP 2-creates objects creates object of
// SimpleDateFormat(java.text.SimpleDateFormat)
// and pass the date format into its constructor
SimpleDateFormat strformat = new SimpleDateFormat("dd/MM/yyyy");

// STEP 3- use parse() method of SimpleDateFormat and put string
// into its constructor
Date date = strformat.parse(date_is);
System.out.println("String to Date: " + date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}

}


output-->

String to Date: Wed Jun 29 00:00:00 IST 20016



DATE TO STRING CONVERSION IN JAVA

                                    DATE  TO  STRING  CONVERSION IN JAVA


package basic;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateToString {

public static void main(String[] args) {
// STEP1- create a Date object

Date date = new Date();
System.out.println("Date is: " + date);

// STEP2- creates object of SimpleDateFormat(java.text.SimpleDateFormat)
// and pass the date format into its constructor
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");

// STEP 3- use the format() method of SimpleDateFormat and put object of
// Date(creates in STEP 1) into constructor and format() method will
// return String date with pass format date( in Step 2)
String stringDate = simpleDateFormat.format(date);

System.out.println("after convert: " + stringDate);

// see here you can take any date format in parameter,and keep in mind
// here month is in capital MM.if you take lowercase mm then it wont
// give compilation error but give any values but not exact month,,same
// as with date and year

SimpleDateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");

String date2 = format1.format(date);
System.out.println("in different format: " + date2);

}

}

output-->

Date is: Thu Jun 02 18:18:55 IST 2016
after convert: 02/06/2016
in different format: 06/02/2016



Monday, 9 May 2016

How to fetch data into text fields from database using ajax

                            How to fetch data into text fields from database using ajax




<div class="col-md-6">
<div class="form-group has-success">
<span style="color: red">*</span> <label class="text-danger"><strong>BOOK
ID :</strong></label> <input type="text" class="form-control" name="lib_bookID"
id="lib_bookID" maxlength="15" onkeyup="get(this.value,<%=session.getAttribute("sid") %>)" />
</div>


<div class="form-group has-success">
<span style="color: red">*</span> <label class="text-danger"><strong>AUTHOR
NAME :</strong></label> <input type="text" class="form-control"
name="lib_author" id="lib_author" readonly="readonly"
maxlength="25" />
</div>


<div class="form-group has-success">
<span style="color: red">*</span> <label class="text-danger"><strong>DATE
OF ISSUE :</strong></label> <input type="text" class="form-control datepicker"
name="doi" id="doi" maxlength="10"
onkeypress="return isNumber(event)" />
</div>

</div>

<div class="col-md-6">
<div class="form-group has-success">
<span style="color: red">*</span> <label class="text-danger"><strong>BOOK
NAME :</strong></label> <input type="text" class="form-control"
name="lib_book_name" id="lib_book_name" maxlength="25" />
</div>

<div class="form-group has-success">
<span style="color: red">*</span> <label class="text-danger"><strong>PUBLICATION
NAME :</strong></label> <input type="text" class="form-control"
name="publication_book" id="publication_book" />
</div>




Step-1>make a servlet class and put this

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

String book_number = request.getParameter("book_number");


String sid=request.getParameter("sid");

System.out.println(book_number);
System.out.println(sid);

String book, auth, puslh;
try {

Connection con = JDBCUtil.getConnection();
PreparedStatement ps = con
.prepareStatement("SELECT BOOK_NAME,AUTHOR_NAME,PUBLISHER_NAME from BOOK_REGISTRATION where BOOK_NUMBER=? and SID=?");
ps.setString(1, book_number);
ps.setString(2, sid);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
book = rs.getString("BOOK_NAME");
auth = rs.getString("AUTHOR_NAME");
puslh = rs.getString("AUTHOR_NAME");
} else {
book = "";
auth = "";
puslh = "";
}
response.getWriter().write(book + "," + auth + "," + puslh);


} catch (Exception e) {

e.printStackTrace();
}


}


Step-2> take it in jsp page

<script>
     
        function get(book_number,sid)
               {
                   var xmlhttp=new XMLHttpRequest();
                   xmlhttp.onreadystatechange=function()
                     {
                     if (xmlhttp.readyState==4 && xmlhttp.status==200)
                       {
                   
                   
                    var responseArray = xmlhttp.responseText.split(",");
                         document.getElementById("lib_book_name").value=responseArray[0];
                         document.getElementById("lib_author").value=responseArray[1];
                         document.getElementById("publication_book").value=responseArray[2];
                       }
                     };
                  xmlhttp.open("GET","Getdata?book_number="+book_number + "&sid="+sid ,true);
                   xmlhttp.send();

               }

        </script>

After all this, enter book id and press Enter button,then you will get automatic data into fields

Note-Before making this please make book id fields auto complete


Thanks
Keep Learning......................................




Friday, 8 April 2016

JSP SERVLET MVC


Very Important Point

when you dont get any error during updating form but not able to update value in form then check this

1.id
2.form action is stable or not
3.always check annotation or mapping servlet name

and if you rename servlet name then always check annotation in webservlet within servlet.

Sunday, 1 November 2015

METHOD OVERLOADING

Method overloading:->  When two or more methods in a class have the same method names with different arguments, it is said to be method overloading. Overloading does not block inheritance from the superclass. Overloaded methods must have different method signatures. But method name remains same.
                                                  Concentrate on these points
IN JAVA, Compiler checks method signature for duplicate methods or for method overloading. method signature consist of three things,
 1) Method Name   2) Number Of Arguments   3) Types of arguments.
NOTE-: If these three things are same for any two methods in a class, then compiler gives duplicate method error.
How to Check Compiler->Complier first check Method Name, then check further:
Ø  If Method Name is not equal then there will not be Method overloading.(as already said method name must be same.)
Ø  if Method Name is same then Complier will check Number Of Arguments, If methods differs in number of arguments, then it does not check types of argument. It treats as methods are overloaded.   
                           If number of arguments are same then compiler checks types of arguments. If types of arguments are also same, then compiler will give duplicate method error. If types of arguments are not same, then compiler will treat them as methods are overloaded. 

Yaad rhe -> In Method Overloading,  Methods may have same return types or different return types. It does not effect  on method overloading. And may have same access modifiers or different access modifiers. It also does not effect method overloading.  it is clear that compiler will check only method signature for method overloading or for duplicate methods. It does not check return types, access modifiers and static or non-static.
static/early binding polymorphism: overloading

public class TFSian_Class {
     
     
           public void read()                      // I
           {
                System.out.println("All Tfsian read");
           }
           public void read(int x)                 // II
           {
                System.out.println("Ranjeet reads more than " + x + " hours in a day");
           }
           public void read(String str)           // III
           {
                System.out.println("Reshma reads " + str);
           }
           public static void main(String args[])
           {
             TFSian_Class tfs = new TFSian_Class();

                 tfs.read();              // calls I
                 tfs.read(10);                  // calls II
                 tfs.read("SDLC");        // calls III
          }
     }

OutPut-> 
All Tfsian read
Ranjeet reads more than 10 hours in a day
Reshma read SDLC




In the above TFSian_Class  class, the same read() method is declared three times and each time takes different parameters – no parameter, int parameter and string parameter. Compiler can easily differentiate which method is to be called depending upon the number of parameters and their sequence of data types.
Return type in Overloading
See the following two method declarations.
public void read()
public int read()

The above two read() methods differ in their return type but not in parameters. Compiler cannot judge which is to be called depending on the return types; judges only on the parameter list. The above statements raise a compilation error. The return type may or may not be the same in method overloading.

                             

Method Overriding

Method Overriding
Method overriding :  When Subclass acquires the properties of Super class  through method then it is Method Overriding
                              Method Overriding in java is most useful features of java. Through inheritance we can reuse already existed code and through method overriding we can modify that reused code according to our requirements

Method Overriding Rules:-
  1. must be IS-A relationship (inheritance).
  2. method must have same name as in the super class
  3. method must have same parameter as in the super class.
  4. The return type of the overrided method must be compatible with super class method. If super class method has primitive data type as its return type, then overrided method must have same return type in sub class also. If super class method has derived or user defined data type as its return type, then return type of sub class method must be of same type or its sub class
  5. You cannot reduce the visibility of overloaded method in subclass.

Note- You cannot override static method in method overriding.because static method is related with class and method name with object.
dynamic/late binding polymorphism: overriding
public class Bank {
     
     
       
      int getRateOfInterest()
      {
      return 4;} 
      } 
       
      class PNB extends Bank{ 
      int getRateOfInterest(){return 9;} 
      } 
       
      class KENRA extends Bank{ 
      int getRateOfInterest(){return 6;} 
      } 
      class HDFC extends Bank{ 
      int getRateOfInterest(){return 9;} 
      } 
       
      class Test{ 
      public static void main(String args[]){ 
      PNB pnb=new PNB(); 
      KENRA kenra=new KENRA(); 
      HDFC hdfc=new HDFC(); 
      System.out.println("PNB Rate of Interest: "+pnb.getRateOfInterest()); 
      System.out.println("KENRA Rate of Interest: "+kenra.getRateOfInterest()); 
      System.out.println("HDFC Rate of Interest: "+hdfc.getRateOfInterest()); 
      } 
      }
OutPut-:
 PNB Rate of Interest: 9
KENRA Rate of Interest: 6
HDFC Rate of Interest: 9

.
.




Saturday, 31 October 2015

MATRIX MULTIPLICATION



A- Rows in first Matrix
B- Columns for first  Matrix and Rows for Second Matrix
C- Columns for Second Matrix


package com.talent;

import java.util.Scanner;

public class MatrixMulti {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
  System.out.println("enter the rows for first matrix");
   int A=sc.nextInt();
 System.out.println("enter the no. for  columns  in first matirx / rows in second matrix ");
   int B=sc.nextInt();
   System.out.println("enter the columns in second matrix");
   int C=sc.nextInt();
 
 
   //now define all three matrix
       int [][]matrix1= new int[A][B];
       int [][]matrix2= new int[B][C];
       int [][]product= new int[A][C];
     
     
       System.out.println("enter the data for first elements");
     
       for(int i=0; i<A; i++)
       {
        for(int j=0; j<B; j++)
        {
        matrix1[i][j]=sc.nextInt();
        }
       
       }
 
     
      System.out.println("enter the data of second");
                for(int i=0; i<B; i++)
                {
                for(int j=0; j<C; j++)
               
                {
                matrix2[i][j]=sc.nextInt();
               
                }
               
                }
               
     
      System.out.println("first matirx is : ");
      for(int i=0; i<A; i++)
      {
     for(int j=0;j<B;j++)
     {
     System.out.print(matrix1[i][j]+"\t");
   
     }
     
     System.out.println();
      }
   System.out.println("second matirx is: ");
       for(int i=0; i<B; i++)
       {
        for(int j=0; j<C; j++)
       
        {
       System.out.print(matrix2[i][j]+"\t");
        }
        System.out.println();
       }
 
     
       System.out.println("PRODUCT IS : ");
       for(int i=0; i<A;i++)
       {
        for(int j=0; j<C; j++)
        {
       
        for(int k=0; k<B;k++)
        {
        product[i][j] += matrix1[i][k] * matrix2[k][j];
       
        }
        }
       
       }
 
   for(int i=0; i<A; i++)
   {
    for(int j=0; j<C; j++)
    {
    System.out.print(product[i][j]+"\t");
   
    }
    System.out.println();
   }
 
 

}

}

OUTPUT->enter the rows for first matrix
2
enter the no. for  columns  in first matirx / rows in second matrix
3
enter the columns in second matrix
2
enter the data for first elements
3
4
5
6
7
8
enter the data of second
3
4
5
6
7
8
first matirx is :
3 4 5
6 7 8
second matirx is:
3 4
5 6
7 8
PRODUCT IS :
64 76
109 130







Simple Matrix Programs

                                                                           Simple  Matrix

package com.talent;

import java.util.Scanner;

public class Matrix1 {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);
System.out.println("enter the no. of rows");
int row=sc.nextInt();

System.out.println("enter the no. of columns");
 int column=sc.nextInt();

 //now define 2D array to hold the matirx

 int[][]matrix=new int[row][column];

 System.out.println("enter the data");
           
 for(int i=0;i<row;i++)
 {
 for(int j=0; j<column; j++)
 {
 matrix[i][j]=sc.nextInt();

 }


 }

System.out.println("MATRIX IS :  ");

for(int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
System.out.print(matrix[i][j]+"\t");
}
System.out.println();
}


}

}


OutPut->enter the no. of rows
3
enter the no. of columns
2
enter the data
4
5
6
7
8
9
MATRIX IS :
4 5
6 7
8 9






Friday, 30 October 2015

Sorting String


                                                       Sorting String


package com.talent;

public class SortString {

public static void main(String[] args) {

   String []names={"Amit","neeraj","dhiraj","SHIPRA","azak","axdf"};
 
     sortStringBubble(names);
   
     for(int k=0;k<6;k++)
     {
   
    System.out.println(names[k]);
   
   
     }

}

private static void sortStringBubble(String[] x) {


boolean flag=true;

String temp;

while (flag) {


flag=false;

for (int j = 0; j < x.length-1; j++) {

if(x[j].compareToIgnoreCase(x[j+1])>0)

{
temp=x[j];
x[j]=x[j+1];
x[j+1]=temp;
flag=true;

}
}

}


}

}
OutPut->
Sorted String
Amit
axdf
azak
dhiraj
neeraj
SHIPRA

Find Character's Frequency


                                              Find Character's Frequency


package com.talent;

public class Find_Character_Frequency {


static int [] characterFrequency(String s)

  {

       s=s.toLowerCase();

    int frequency[]=new int[26];

   for (int i = 0,c=97; i < 26; i++,c++)

   {

   for (int j = 0; j < s.length(); j++)


     {

   char ch=s.charAt(j);

   

                   if(ch==c)
                  frequency[i]++;


} }
return frequency;
  }

 public static void main(String[] args) {



 String s="I am Java Developer";

 int frequency[]=characterFrequency(s);
 System.out.println("Albhabet \t Frequency");


 for (int i = 0,c=97; i <26; i++,c++) {
 if(frequency[i]!=0){
 char ch=(char)c;

 System.out.println(ch+"\t\t"+frequency[i]);


 }

}

}


}
OutPut->
Albhabet Frequency
a 3
d 1
e 3
i 1
j 1
l 1
m 1
o 1
p 1
r 1
v 2

Duplicate Elements in Java


                                                            




package com.talent;



public class Dublicate_Element {


public static void main(String[] args) {
         

        int A[]=new int[]{1,2,3,4,5,6,7,8,9}; //take element in array
int B[]=new int[A.length*2];      // here we double the size of array
     for (int i = 0,k=0; i < A.length; i++,k++)
           {
            B[k]=A[i];
            B[++k]=A[i];
          

  }
      for (int i = 0; i < B.length; i++) 
            {
   System.out.println(B[i]);
          }
}
}


OuTPuT->112233445566778899

How can increase and decrease the fonts in eclipse

                               How can increase and decrease the fonts in eclipse


Simple Steps for it--

Go to Install New Software

put it and download,


http://eclipse-fonts.googlecode.com/svn/trunk/FontsUpdate/

click ok and you will see two button on eclipse.