Thursday, 11 May 2017

What is Composition in JAVA !!!

Composition is restricted form of agreegation. For example, a class car can't exist without engine.


class Car
{
  private Engine engine;
  Car(Engine en)
  {
    engine en;
  }

}

Thursday, 4 May 2017

Why String[] args in main() method in JAVA?

The main objective is designed very intelligently by developers. Actual thinking is very deep. Which is basically developed under consideration of C & C++ based on Command line argument but nowadays nobody uses it more.
Thing 1- User can enter any type of data from the command line can be Number or String & necessary to accept it by the compiler which datatype we should have to use?
Thing 2- String is the datatype which supports all of the primitive datatypes like int, long, float, double, byte, shot, char in Java. You can easily parse it in any primitive datatype.
let's see the following example, 
If input is -> 1 1
// one class needs to have a main() method
public class HelloSilan
{
  // arguments are passed using the text field below this editor
  public static void main(String[] x)
  {   
System.out.println(x[0] + x[1]); // Output is 11

//Comment out below code in case of String
    System.out.println(Integer.parseInt(parameter[0]) + Integer.parseInt(parameter[1])); //Output is 2
    System.out.println(Float.parseFloat(parameter[0]) + Float.parseFloat(parameter[1])); //Output is 2.0   
    System.out.println(Long.parseLong(parameter[0]) + Long.parseLong(parameter[1])); //Output is 2   
    System.out.println(Double.parseDouble(parameter[0]) + Double.parseDouble(parameter[1])); //Output is 2.0   

  }

}

What is checked exception?

The exception which is checked by the compiler at the time of compilation for smooth execution of the program is known as checked exception.
Example:
import java.io.*;
class Test{
public static void main(String[] args){
PrintWriter pw=new PrintWriter("abc.txt");
pw.println("SILAN TECHNOLOGY");
}
}
sometimes the compiler check some specific program for smooth execution at run time, so when we compile this program we will get following compile time error :

unreported exception java.lang.FileNotFoundException must be caught or declared to be thrown

Friday, 21 April 2017

JDBC-Store image in Oracle Database

It is possible that we can store images in the database by executing jdbc program with the help of PreparedStatement interface.

The setBinaryStream() method of PreparedStatement is used to set Binary information into the parameterIndex.

For example:
CREATE TABLE user  
(
id varchar2(100),
name varchar2(100),
image BLOB
);  
Let's write the program to store the image in the database. Here we are using e:\\a.jpg for the location of image. You can change it according to the image location.

JDBC Example to store image in the database


import java.sql.*;  
import java.io.*;  
public class InsertImageDemo {  
public static void main(String[] args) throws Exception{  
Class.forName("oracle.jdbc.driver.OracleDriver");  
Connection con=DriverManager.getConnection(  
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");  
              
PreparedStatement ps=con.prepareStatement("insert into user values(?,?,?)");  
ps.setString(1,101);  
ps.setString(2,"Tapuuu");  

  
FileInputStream fin=new FileInputStream("e:\\a.jpg");  
ps.setBinaryStream(2,fin,fin.available());  
int k=ps.executeUpdate();  
System.out.println(k+" row inserted");  
          
con.close();  
}
}  
Output
1 row inserted



Note : If you see the table, record is stored in the database but image will not be shown. For this, you need to retrieve the image from the database.

Tuesday, 4 April 2017

What is JAVA !!!

1.    JAVA is a technology.
2.    Java is an OOP language.
3.    JAVA is a Platform.

Note : A Platform is hardware & software environment where we execute our java program. JAVA is itself a platform, bcz JAVA has it's own JRE(Java Runtime Environment).

How to convert ArrayList to String array in Java.

ArrayListExample1.java

package java8s;

import java.util.*;
public class ArrayListExample1 {

    public static void main(String[] args) {
        List al = new ArrayList<String>();
         
        al.add("First");
        al.add("Second");
        al.add("Third");
        al.add("Fourth");
        al.add("Fivth");
     
        String[] s1 = new String[al.size()];
        al.toArray(s1);
     
        for(String s2 : s1)
        System.out.println(s2);

    }


}

Output:
First
Second
Third
Fourth
Fivth

How to avoid duplicates from List / ArrayList

Hii frnds…this is the question that I have faced many times in interview, very popular frequently asked question. Let’s see in detail.
ArrayListExample.java

package java8s;

import java.util.*;
public class ArrayListExample {

          public static void main(String[] args) {
                  
                  
                                      ArrayList al = new ArrayList();
                    
                                            al.add("First");
                                            al.add("Second");
                                            al.add("Third");
                                            al.add("First");     //Duplicate
                                            al.add("First");     //Duplicate
                    
                                          
                                              ArrayList al1 = new ArrayList(new HashSet(al));     
                    
                                           // ArrayList al2 = new ArrayList(new LinkedHashSet(al));     
                    
                                           Iterator it= al1.iterator();
                                           while(it.hasNext())
                                           {
                                                 System.out.println(it.next());
                                           }
                    
                             }
                  

          }
Output:
Second
Third
First