Great Deals

How To Create Nested Class in java

19:41 Unknown 0 Comments

InnerClass
class A {
            class B {
                        public void print() {
                                    System.out.println("HI ");
                        }
            }


}

public class InnerClasses {

            public static void main(String[] args) {
                        A aObj=new A();
                       
                        A.B bObj=aObj.new B();
                       
                                    bObj.print();           

            }

}

 Inner Class2

class AOuter {
            class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }
           
            public void aMethod()
            {
                        BInner b=new BInner();
                        b.printMe();
            }
}

public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter a=new AOuter();
                                    a.aMethod();

            }

}
Method Local Inner Class


class AOuter {
            void aOuter()
            {
            class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }
            BInner bObj=new BInner();
            bObj.printMe();
            }
           
}

public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter a=new AOuter();
                        a.aOuter();

            }

}


Static Nested Class

class AOuter {
           
            static class BInner {
                        public void printMe() {
                                    System.out.println("i m B");
                        }

            }          
}
public class MethodLocalInnerClass {

            public static void main(String[] args) {
                        AOuter.BInner a=new AOuter.BInner();
                        a.printMe();
            }
}

0 comments:

Difference between TRUNCATE, DELETE and DROP commands

20:08 Unknown 0 Comments

 1>TRUNCATE is a DDL command whereas DELETE is a DML command. 2>When compared with delete TRUNCATE is much faster than DELETE.
Because
Step 1:When you DELETE.all the data first gets copied into the Rollback Tablespace first.

2> Then delete operation get performed.
Benefit of delete is  when you  ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).

But when you type TRUNCATE,it removes data directly no extra copy is maintained .Thats why TRUNCATE is faster.

Once you Truncate you cann't get back the data.

3>You cann't rollback in TRUNCATE but in DELETE you can rollback.TRUNCATE removes the record permanently.

4>In case of TRUNCATE ,Trigger doesn't get fired.But in DML commands like DELETE .Trigger get fired.

5>You cann't use conditions(WHERE clause) in TRUNCATE.But in DELETE you can write conditions using WHERE clause

0 comments:

Tricky Facts About views

19:12 Unknown 0 Comments

  1. Restrictions on Updating Data Through Views You can insert, update, and delete rows in a view, subject to the following limitations:

  2. Cool Gadgets
  3. If the view contains joins between multiple tables you can only insert and update one table in the view, and you can't delete rows.

  4. You can't directly modify data in views based on union queries. You can't modify data in views that use GROUP BY or DISTINCT statements.

  5. All columns being modified are subject to the same restrictions as if the statements were being executed directly against the base table.

  6. Text and image columns can't be modified through views.

0 comments:

Tricky Sql Interview Questions

15:59 Unknown 0 Comments

Sql Mostly used queries.

1.Views

CREATE OR REPLACE VIEW passStudent AS
SELECT StudentId,StudentName
FROM Student
WHERE result=pass

2.GroupBy Clause
Student Table
create table student(studId number(5),studName varchar2(10),marks number(3));  
  • insert into student values(1,'sur',30);   
  • insert into student values(2,'jit',65); 
  • alter table student add(collegeId number(3));
  • SQL> update student set collegeId=1 where studId=1;   
  • SQL> update student set collegeId=2 where studId=2; 
  • SQL> insert into student values(3,'ramesh',45,1); 

  Question select collegeId,count(collegeId) from student group by collegeId;                          

COLLEGEID COUNT(COLLEGEID)                                                                                                                  
  • ---------- ----------------                                                                                                                        
             1                2                                                                                                                        
             2                1                                               
Onother Table College

CREATE TABLE `TestSuresh`.`college` ( `cid` INT(7) NOT NULL , `cname` VARCHAR(15) NOT NULL ) ENGINE = InnoDB;

  • INSERT INTO `college` (`cid`, `cname`) VALUES ('1', 'pvm'), ('2', 'asc')

                                                                                                                        

SQL> select * from student order by marks desc;                                                                                                    
     
STUDID  STUDNAME         MARKS   COLLEGEID
2 jit 65 2
3 ramesh 45 1
1 suresh 30 1
                                                                                                       

Question:To Get the first three records in ascending order

select distinct(s1.marks) from student s1 where 3>=(select count(distinct(marks)) from student s2 where s1.marks<=s2. marks) order by s1.marks desc       

Question:To get The student also include students which does not belong to any college

This gives you hint you need to use left join.
select * from student s1 left join college c on s1.c_id=c.cid                

0 comments:

When to use comparator and when we should use comparable.

19:04 Unknown 0 Comments


When to use comparator and when we should use comparable.

Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this. Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour.

See the example where used comparable for sorting elements in ascending order.


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class vehicle implements Comparable<vehicle> {
     int vehNumber;

     public int getVehNumber() {
           return vehNumber;
     }


     public void setVehNumber(int vehNumber) {
           this.vehNumber = vehNumber;
     }

     @Override
     public int compareTo(vehicle o) {
           int result = 0;
           return result=this.getVehNumber() - o.getVehNumber();
     }

}

class Fruit {

public class Sorting {

     public static void main(String[] args) {
           vehicle v = new vehicle();
             v.setVehNumber(123);
             vehicle v2 = new vehicle();
             v2.setVehNumber(12);
             vehicle v1 = new vehicle();
             v1.setVehNumber(1111);
            
             List<vehicle> vehs = new ArrayList<vehicle>();
             vehs.add(v);vehs.add(v2);
             vehs.add(v1);
            
             Collections.sort(vehs);
             for (int i = 0; i < vehs.size(); i++) {

               System.out.println("" + vehs.get(i).getVehNumber());
             }
          
          
           /*Fruit f1=new Fruit("Mango1233",1233);
           Fruit f2=new Fruit("Mango123",123);
          
           List<Fruit> fs=new ArrayList<Fruit>();
           fs.add(f1); fs.add(f2);
          
           Collections.sort(fs,new FruitComparator());
           System.out.println(""+fs.get(0).getFruitName());
*/
     }

}


Example For Comparator
Fruit.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;


class Fruit {
String fruitName;
int fruitPrice;

            public Fruit(String fruitName, int fruitPrice) {
                        super();
                        this.fruitName = fruitName;
                        this.fruitPrice = fruitPrice;
            }
            public int getFruitPrice() {
                        return fruitPrice;
            }

            public void setFruitPrice(int fruitPrice) {
                        this.fruitPrice = fruitPrice;
            }
            public String getFruitName() {
                        return fruitName;
            }

            public void setFruitName(String fruitName) {
                        this.fruitName = fruitName;
            }

           

}

public class Sorting {

            public static void main(String[] args) {
                                               
                        Fruit f1=new Fruit("Mango1233",1233);
                        Fruit f2=new Fruit("Mango123",123);
                       
                        List<Fruit> fs=new ArrayList<Fruit>();
                        fs.add(f1); fs.add(f2);
                       
                        Collections.sort(fs,new FruitComparator());
                         for (int i = 0; i < fs.size(); i++) {                  
           System.out.println("" + fs.get(i).getFruitPrice());
                           }

            }

}



FruitComparator.java-
This is where we will write logic for comparing two custom objects.
import java.util.Comparator;


import java.util.Comparator;

public class FruitComparator implements Comparator<Fruit> {
            public int compare(Fruit o1, Fruit o2) {
                        int result = 0;
                        if (o1.getFruitPrice() < o2.getFruitPrice()) {
                                    result = -1;
                        } else {
                                    result = 1;
                        }
                        return result;
            }
}


Sorting.java
public class Sorting {

     public static void main(String[] args) {
                    
           Fruit f1=new Fruit("Mango1233",1233);
           Fruit f2=new Fruit("Mango123",123);
          
           List<Fruit> fs=new ArrayList<Fruit>();
           fs.add(f1); fs.add(f2);
          
           Collections.sort(fs,new FruitComparator());
            for (int i = 0; i < fs.size(); i++)                                        {                   System.out.println("" + fs.get(i).getFruitPrice());
                  }

     }

}

0 comments:

Advertising