Protected by Copyscape
Powered By Blogger

Tuesday, August 18, 2015

How to connect to mySQL

Pre-requisites:
1)mySQL
2)A sample table with data in it
3)com.mysql.jdbc_5.1.5.jar
--------------------------------------------------------------------------------------------------------------------------
package DatabaseConnectivity;

import java.sql.*;

public class mySqlJdbcDemo
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost";

  //  Database credentials
  static final String USER = "root";
  static final String PASS = "password";

  public static void main(String[] args) throws Exception
  {

     //STEP 1: Register JDBC driver
     Class.forName(JDBC_DRIVER);

     //STEP 2: Open a connection
     Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
     System.out.println("Connected");
     System.out.println("+++++++++++");
   
     //STEP 3:Create statement Object
     Statement stmt=conn.createStatement();
   
     //STEP 4:Execute the query    
     ResultSet rs=stmt.executeQuery("SELECT * FROM test.employee");
   
     while(rs.next())
     {
     System.out.println(rs.getString(2)); // 2 represents column # 2
     }
   
     conn.close();
     System.out.println("++++++++++++");
     System.out.println("connection closed");

       }
}

Output:

Connection established
++++++++++++
Harry
Jack
+++++++++++++
Connection closed

Monday, August 10, 2015

Generic Collection

Generic collection allows you to have only one type of items in collection.Advantage is that typecasting is not required at run time.

Non generic way of creating a collection:

ArrayList list1=new ArrayList();

New generic declaration
ArrayList<String> list1=new ArrayList<String>();

Example:

import java.util.ArrayList;
import java.util.Iterator;

public class GenericArrayList
{

public static void main(String[] args)
{
ArrayList<String> list1=new ArrayList<String>();
       
  list1.add("Ravi");//adding object in arraylist
  list1.add("Vijay");
  list1.add("Ravi");
  list1.add("Ajay");
 //list1.add(new Integer(10));
    //uncommenting the above will give error.

 //Iterating the elements of Collection by using Iterator interface
 Iterator itr=list1.iterator();
 while(itr.hasNext())
 {
 System.out.println(itr.next());
 }
   

 //Iterating the elements of Collection by for-each loop
 for(String obj:list1)
  {
  System.out.println(obj);
  }

}

}

Difference between Abstract Class and Interface

Both are used to achieve abstraction where we can declare the abstract methods. Abstract class and interface both can't be instantiated.

Please see the below differences between these two:

Abstract class                                             
  • Abstract class can have abstract and non-abstract methods.
  • Abstract class doesn't support multiple inheritance. 
  • Abstract class can have final, non-final, static and non-static variables.
  • Abstract class can have static methods, main method and constructor.
  • Abstract class can provide the implementation of interface.
  • The abstract keyword is used to declare abstract class.
Interface
  • Interface can have only abstract methods.
  • Interface supports multiple inheritance.
  • Interface has only static and final variables.
  • Interface can't have static methods, main method or constructor.
  • Interface can't provide the implementation of abstract class.
  • The interface keyword is used to declare interface.
Example:
public abstract class Animal
{
     public abstract void eat();
}

Example:
public interface Animal
{
    void eat();
}

Wednesday, August 5, 2015

Non Access Modifiers in Java.

In this article you will learn about Non Access Modifiers. Below mentioned are the Non Access Modifiers available in Java.
  1. Final
  2. Abstract
  3. Static
  4. Strictfp
  5. Native
  6. Synchronized
  7. Transient

Final Class :

A class when set to final cannot be extended by any other class.

Example: String Class in java.lang package

Final Method :
A method when set to final cannot be overridden by any subclass.

Final Variable :
When a variable is set to final, its value cannot be changed. Final variables are like constants.

Example : public static final int i = 10;


Abstract modifiers are applicable to:

Class
An abstract class can have abstract methods. A class can also be an abstract class without having any abstract methods in it. If a class has an abstract method , the class becomes an abstract class.

Method
Abstract methods are those methods which does not have a body but only a signature.

Example : public abstract void method()


Synchronized Non Access Modifier
Synchronized modifiers are applicable to 'Method' only.

Synchronized methods can be accessed by only one thread at a time.


Native Non Access Modifier
Native modifiers are applicable to 'Method' only.

Naive method indicates that a method is implemented on a platform dependent code.


Strictfp Non Access Modifier
Strictfp modifiers are applicable to Class and  Method

Strictfp non access modifier forces floating point or floating point operation to adhere to IEEE 754 standard. Strictfp non access modifier cannot be applied on a variable