Thursday 9 June 2016

How to export Bootstrap Data table all rows to excel?




Step 1:-
Take a save button on table and assign id


<table id="example2" class="table table-bordered table-hover" >
    <thead>
        <tr>
            <th>S.No.</th>
            <th>Date</th>
            <th>Process</th>
            <th>count</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1<td>
            <td>2015-11-04<td>
            <td>Java<td>
            <td>10<td>
        </tr>
    </tbody>
    <tfoot class="bg-gray">
        <tr>
            <th colspan="3">Summary</th>
            <th>10</th>
        </tr>
    </tfoot>
<table>
    <button id="button">save</button>


Step 2:-Now put javascript code

$('button').click(function(){
$("#example2").table2excel({
                    exclude: ".noExl",
                    name: "Excel Document Name",
                    filename: "myFileName",
                    exclude_img: true,
                    exclude_links: true,
                    exclude_inputs: true
})

    });


Thanks
Keep Happy Learning....

Tuesday 7 June 2016

How can Display single user on Map In Java




Step 1-Call function where you put database code

 <%
   int id=Integer.parseInt(request.getParameter("mapId"));
 
   UserBusinessService us=new UserBusinessService();
        User u=us.getuserlatlong(id); %>

Here User is javabean class where getter setter is defined, and UserBusinessService class is that where put database code, i just create the object and call method.

here i use int id, because for going to map you will click on button and for particular user you will have to take userid on button right? :)


Step 2- all google map code will be within javascript

Javascript code is here

   <script language="javascript">
 
   function initialize()
   {

  var lang=<%=u.getLongitude()%>
  var lat=<%=u.getLatitude()%>

  var myCenter=new google.maps.LatLng(lat,lang);

   var mapProp = {
     center:myCenter,
     zoom:12,
     mapTypeId:google.maps.MapTypeId.HYBRID
     };

   var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

   var marker=new google.maps.Marker({
     position:myCenter,

     animation:google.maps.Animation.BOUNCE
     });

   marker.setMap(map);
   }

   google.maps.event.addDomListener(window, 'load', initialize);
   </script>


Here see
  var lang=<%=u.getLongitude()%>
 var lat=<%=u.getLatitude()%>

Its simple getter setter value, i think you know how to take data from database, if dont  know then let me know

Now if you want to change icon color, by default is is red but want to change the use following code

 var pinImage = new google.maps.MarkerImage("http://www.googlemapsmarkers.com/v1/009900/");

 var marker = new google.maps.Marker({
            position: yourlatlong,
            icon: pinImage,
            map: map
        });

for more details please go here

http://www.googlemapsmarkers.com/


if any query then let me know

Thanks
Keep Happy Learning... :)

How to stop copy paste from jsp page




Put this code with in body of jsp page

<body oncontextmenu="return false" onselectstart="return false"
   onkeydown="if ((arguments[0] || window.event).ctrlKey) return false">

                                  or

You can stop from copy paste using below code

<body ondragstart="return false" onselectstart="return false">


Thanks
Keep Happy Learning....

Mind Games in Java




What will be output?


  1. public class MindGame {
  2.      public static void main(String[] args) {
  3. Float  a=0;
  4. try{
  5. System.out.println(a/0);
  6. }catch(Exception  e){
  7. System.out.println("Error");
  8. }
  9. catch(IOException e1){
  10. System.out.println("Error1");
  11. }}


output-
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Type mismatch: cannot convert from int to Float
Unreachable catch block for IOException. It is already handled by the catch block for Exception
        at MindGame.main(MindGame.java:6)

Here we can see three errors
1.declaration of float a, we should assign it as 0.0
2.in catch block- as we know that we can use more than one catch block for one try block in Java but keep in mind "we cant use superclass exception in 1st catch block, here keep superclass exception in 1st catch block and superclass into 2nd catch block,"

After modification of programme

  1. public class MindGame {
  2.      public static void main(String[] args) {
  3. Float  a=0.0f;
  4. try{
  5. System.out.println(a/0);
  6. }catch(IOException  e){
  7. System.out.println("Error");
  8. }
  9. catch(Exception e1){
  10. System.out.println("Error1");
  11. }
  12. }}

Here error is IOexception is never thrown from the try block, exception must be specific or use only Exception class.

Again after modification 

  1. public class MindGame {
  2.      public static void main(String[] args) {
  3. Float  a=0.0f;
  4. try{
  5. System.out.println(a/0);
  6. }catch(ArithmeticException  e){
  7. System.out.println("Error");
  8. }
  9. catch(Exception e1){
  10. System.out.println("Error1");
  11. }
  12. }}
now we can get successful result and result will be NaN, now think why we got this result

Reason is here-"NaN" stands for "not a number". "Nan" is produced if a floating point operation has some input parameters that cause the operation to produce some undefined result. For example, 0.0 divided by 0.0 is arithmetically undefined. Taking the square root of a negative number is also undefined.

Whats about this

  1. public class MindGame {
  2.      public static void main(String[] args) {
  3. Float  a=0.0f;
  4. try{
  5. System.out.println(a/0.0);
  6. }catch(ArithmeticException  e){
  7. System.out.println("Error");
  8. }
  9. catch(Exception e1){
  10. System.out.println("Error1");
  11. }
  12. }}

In line number 3 dividing from 0.0 then output will be same.


Thanks
Keep Happy Learning.. :)






Monday 6 June 2016

Abstraction in Java




Abstraction is a process of hiding the implementation details and showing only functionality to the user.”
Another way, it shows only important things to the user and hides the internal details for example sending sms, you just type the text and send the message. You don't know the internal processing about the message delivery.
Abstraction lets you focus on what the object does instead of how it does it.

Abstraction is "To represent the essential feature without representing the back ground details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or object by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.

Real world Example of Abstraction: - 
Suppose you have an object Mobile Phone.

Suppose you have 3 mobile phones as following:-

Nokia 1400 (Features:- Calling, SMS)
Nokia 2700 (Features:- Calling, SMS, FM Radio, MP3, Camera)
Black Berry (Features:-Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (Necessary and Common Information) for the object "Mobile Phone" is make a call to any number and can send SMS."

so that, for mobile phone object you will have abstract class like following:-

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {

    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();

    }

Abstraction in java is implemented using  by two way :-
               Abstract class and    interfaces.

-------------------->coming with Abstract class and Interface<-------------

Thanks

Keep Learning.. :)












Sunday 5 June 2016

Encapsulation in Java

                     

People says "Encapsulation" is a process of wrapping code and data member into single unit is called Encapsulation", ok i agree with this, this is exact definition of it, but whats about understanding? is it sufficient to read, as we know definition is only for mug up, but for better understanding we should understand real time example and how really process is going on? ok i am explaining here, :)

As we know change is the law of universe, and in Java if you have to change anything then
"Whatever changes Encapuslates it" :)


we all have smart phone , suppose you are using whatsapp and now you want to change your profile image here you know the how you can change the profile image of whatsapp but dont know internal implementation, means you now how objects work only  internal implementation are hidden from you, this is Encapsulation.
Encapsulation is implemented using private, public,default and protected access modifier.
The beauty of encapsulation is the power of changing things without affecting its users

Why Encapsulation use in Java Code?

Encapsulation is only for developer use.
Encapsulated Code is more flexible and easy to change with new requirements.
By using Encapsulation we can write immutable class in Java which are a good option in multi-   threading environments.
Encapsulation uses abstraction.
Encapsulation allows us to change one part of code without affecting other parts of code.
As we know that for good software there must be loose coupling and high cohesion,s o Encapsulation is best example of this.
Remember one thing Java Bean is best example of Encapsulation,

>I know now you are thinking what is coupling
Coupling is the degree to which one class knows about another class.

Use of Encapsulation in Design Pattern

Factory pattern , Singleton pattern in Java makes good use of Encapsulation.

--------------------->I will explain more with code :) <------------------

Thanks
Keep Learning.....

          

Friday 3 June 2016

Anonymous Inner Class

                             Anonymous Inner Class

                                       


--> A class without name, it is useful when we have to add some extra features from using superclass, For this you can make overriding, but what is the need of overriding if you can implement this without extends superclass, see example



package basic;

class Class_Super {
void methodfirst() {
System.out.println("hi nilesh from method first");
}

void methodSecond() {
System.out.println("hello neeraj from method second ");
}
}

public class Anonymous_InnerClass {
public static void main(String args[]) {
Class_Super obj = new Class_Super() {
@Override
void methodfirst() {
System.out.println("bye from  Anonymous Inner Class");
}
};
obj.methodfirst();
}
}


output--
bye from  Anonymous Inner Class

----> Here see, we take superclass reference varible and create one object, means a class without name, and then override same method and add extra implementation.


Note-But there is some disadvantages of it, if you have to create more than one object then you will have to make two Anonymous classes, means for one object there must be only one Anonymous class.

if any query please let me know


Thanks
Keep Learning.... :)

ZIP Exception when run project on eclipse


                                        ZIP Exception when run project on eclipse



when we get zip exception on console after running project in eclipse and also there is warning regarding any jar files then please changes that jar file, because that can be corrupt,if you dont no change jar file then always get different page and unexpected results,believe me its very important knowledge if you do code in eclipse.

Thanks
Keep Learning.....

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