Nov 4, 2014

Internals of database connectivity in java

Java Database Connection

Database connection in java is very important process in JDBC. This post is for demonstrating internals of java database connectivity. In this post we will get to know How to connect with database in java, Java database connectivity, internals of database connectivity in java. I will also give a sample example of java database connectivity.
For database connection we need to import some packages and need to use some special methods. Before getting into deep I will explain about these methods and packages. Following is details of how to connect with database.

Step by step explanation of Java database connection-

1. Import Package-  For creating database connection you need some functions and classes. These functions and classes are found in a package so we need to import that package using "import java.sql.*".

2. Loading Driver-  Driver is required for conversion of sql statements into back end database understandable form and vice versa.
Class.forName is used to register the driver.

3. Create Connection- 

4. Define SQL stmt in the form of String class's object

5. Send the SQL stmt

6. Excecute the query and get the result

Sample example of java database connectivity-

//STEP 1. Import "java.sql" package
import java.sql.*;

public class dbConnect
  // JDBC driver name and database URL
  static final String DRIVER = "com.mysql.jdbc.Driver";
  static final String URL = "jdbc:mysql://localhost/TEST";

 // Database credentials
 static final String USR  = "username";
 static final String PWD = "password";

 public static void main(String[] args) {
 Connection con = null;
 Statement stmtt = null;
     //STEP 2: Register JDBC driver

    //STEP 3: Open a connection
    System.out.println("Connecting to database for I/O operation...");
    con = DriverManager.getConnection(URL,USR,PWD);

   //STEP 4: Execute a query
   System.out.println("Statement Creation for I/O operation...");
   stmtt = con.createStatement();
   String sqlstmt;
   sqlstmt = "SELECT id, first, last, age FROM Emp";
   ResultSet rs = stmt.executeQuery(sql);

   //STEP 5: Extracting the data from result set for next operation
        //Retrieve by column name
        int id1  = rs.getInt("id");
        int age2 = rs.getInt("age");
       String first1 = rs.getString("first");
       String last1 = rs.getString("last");

       //Display values
       System.out.print("ID: " + id1);
       System.out.print(", Age: " + age1);
       System.out.print(", First Name: " + first1);
      System.out.println(", Last Name: " + last1);

     //STEP 6: Cleaning - up environment
    catch(SQLException se)
    //Handle errors for JDBC
  catch(Exception e)
    //Handle errors for Class.forName  
    //finally block is used for closing all  resources
     catch(SQLException se2) 
   // nothing we can do
 catch(SQLException se)
  //end finally try
  //end try


It is very important to know, how to execute this program so now let's know it.
Type following command in command line.

C:\> javac

After execution of this command you will get following result-

Connecting to database First Time...
Statement Creation...
ID: 101, Age:  25, First Name: Vikash, Last Name: Shukla

Id,Age,First Name,Last Name may be different as per records in database.

Oct 27, 2014

Internals of Stored Procedure - JDBC


This post is very useful to know about internals of Stored Procedure. JDBC is used for database operations in java and Stored procedures are used for eliminating overhead from Java. Internals of Stored-Procedure will cover all the concepts like What is Stored Procedure, What is procedure, When do people prefer Stored Procedure, Advantages and uses of Stored Procedure, Disadvantage or flaws of Stored Procedures, API of Stored Procedure, Types of Procedure etc…
Before knowing about Stored Procedure we should know about What is Procedure? Procedure is nothing, It is a only function or method of database. These database methods contains group of SQL statements. We create procedure using PL/SQL programming.
Stored Procedure is nothing but it is one of the most useful mechanism for creating procedure in the database and called by java program is known as Stored Procedure.
There is a one big question that When do we prefer using Stored-Procedure? The answer of this question is that whenever we want to get allowance, gross salary etc. from database, we don’t need to create any logic to calculate allowance, gross salary in the java program. We will just create a separate procedure for calculating the allowance and gross salary and will get the desired result in our java program.

Advantage of Stored Procedure-

     Using stored Procedure the processing of data at the back-end database would be faster.
   Overhead on the java program can be reduce.
Disadvantage of Stored Procedure-
  Each and every database contains its own syntax of creating procedure. So if we want to change the database it will create problems due to changed syntax of each database. In this case we will need to write procedures again.

Note- Stored Procedure are not supported in M.S. Access (A Microsoft’s Product).
java.sql.CallableStatement interface is the API of Stored Procedure in Java.

Types of Stored-Procedure’s

There are 4 types of procedures in java.

1. Zero Parameterized Procedure
2. In-Procedure
3. Out-Procedure
4. In-Out Procedure

Zero Parameterized Procedure:

Those procedure which doesn’t contains any parameter is called Zero Parameterized Procedure.

SQL> create or replace

2 procedure prc1

3 Is

4 Begin

5 Update abc1 set name='VIKAS' where id=101;

6 End prc1;

7 /

Procedure created.


1.It is just like a method of java which is parameterized but void return type.
2. like pass the 'idno' or any conditional data as a parameter..

SQL> r/

1 create or replace

2 procedure prc2(aa IN number)

3 Is

4 Begin

5 Update abc1 set name='VIKAS' where id=aa;

6* End prc2;

Procedure created.


It is just a method of java which is contains zero parameter return.... like max, min,
count, sum etc.

SQL> create or replace

2 procedure prc3(abc OUT number)

3 Is

4 Begin

5 Select count(id) into abc from abc1;

6 End prc3;

7 /

Procedure created

In-Out Procedure:

It is just a method of java which is parameterized as well as return type .... like max,
min, count etc.

SQL> create or replace

2 procedure prc4(x IN number,y OUT number)

3 Is

4 Begin

5 Select max(id) into y from abc1 where id>=x;

6 End prc4;

7 /

Procedure created.

Creating CallableStatement Object:

Following code is for oracle database-





FROM EmpMaster



Note: This post is covering all of the fundamentals of stored procedures and related topics. For more details on other related topics, please visit blog-archive.

Thank You

Oct 13, 2014

Internals of Java Interface

Interface Internals in JAVA

Little description of java internals about Interface: Some people say, Interface is used to implement the concept of Multiple Inheritance,but purpose of Interface is not implementing the concept of Multiple Inheritance in Java, although it looks like Multiple Inheritance.

Apart from this It is very important to know that why do we need Interface? The answer is that, If we know about the entity, but don't know about the behavior then we need Interface.
A very simple example of Interface is as follows-

interface Animal
   void eat ();
class Goat implements Animal
    Void eat ()
        System.out.println("Goat is eating grass");
class Dog implements Animal
       void eat()
         System.out.println("Dog is barking");

Some important points for Interface-
1. Interface is a contract.
2. Interface is a contract between service provider and service utilizer.
3. Interface is a keyword of Java.
4. Interface is a user defined data type.
5. Interface is a reference data type.
6. Interface is also structure of java, because it generate .class file. i.e byte code.
7. Interface is the collection of zero or more than zero abstract method and zero or more           than zero final variable.
8. Object of Interface cannot be created.
9. Interface is a pure abstract class, because it only contains abstract method, not any               concrete method. 
10. Interface is a pure SUPER class.

Characteristics of Interface-
1. Method of Interface by default is an abstract.
2. Each and every method of Interface by default declared as a 'public abstract'. If you don't declare Interface as a public abstract,then it is duty of Java Compiler to use 'public abstract' with each method Interface. If we use any other access specifier with Interface method then compile time error is generated.
3. Each and every variable of Interface must be declared as 'public static final', if you don't use then it is duty of Java Compiler to use 'public static final' with each variable of Interface.

Note- 'implements' keyword is used to implement the property of interface in the class.

Working with JAVA Interface

There are some variations which are as follows-

1. One Interface extends another Interface

interface I1

interface I2 extends I1
} interface can extends zero or more than zero interface.

interface I1

interface I2

interface I3
interface I4 extends I1,I2,I3
} interface cannot extends/implement any class. class extends only one class and implements zero or more than zero interface.

class H

class J extends H implements I1,I2,I3,I4

Keywords used with Interface

1. public
2. abstract
3. default(by default)
4. strictfp
5. public abstract/abstract public

Oct 11, 2014

Types of Final Variable in Java

Final Variable

Before knowing types of final variable you should know What is a Final Variable? Final variable is a variable which  is declared using final keyword before it. For example,

Class Tst
   int final var1 = 10; // var1 is declared as final variable...

There are two very important related to Final Variables that are-

1. Final Variable's value never changes.

2. Final variable must be declared during declaration time.

Now, we will discuss about Types of Final variable.
There are following types of final variables-

1. Final Instance Variable
2. Final Static Variable
3. Final Local Variable
4. Final Block Level Variable
5. Final Parameterized Variable

1. Final Instance Variable -  Those variables which are declared as Final Instance i.e. Final                                                 Instance Variable.

2. Final Static Variable -       Those variables which are declared as Final Static i.e. Final                                                     Static Variable.

3. Final Local Variable -        Those variables which are declared as Final Local i.e. Final                                                     Local Variables.
Note-  Final variable may or may not be initialized at declaration time.

4. Final Block Level Variable-  Those variables which are declared as Final block level i.e.                                                 Final Block Level Variable.

5. Final Parameterized Variable-  Those variables which are declared as Final parameter                                                   in method  i.e. known as Final Parameterized  Variable. 
Note-    'final' is the one of the unique access modifier in java ,that can be use inside method or method parameter.

List of all keywords and reserved words in java

Java Keywords

What is a keyword? Keyword is a special word/reserved word which is already defined in the compiler. We can not use keywords in our program for declaring any variable or program name or in other declarations. Keywords definition is defined in the compiler.
For example, If you try to declare a variable with name 'try' that is not possible as 'try' is a keyword in java language. See following example.
  class Testpgm
    int try; //this variable can not be declared....
    public static void main(String[] args) 
       //class code....  
Below is lists of all Java keywords-
There are 53 keywords in java.
1. 48 special words.
2. 3 special words.
3. 2 unused words.

2 unused keywords
1. goto
2. const

3 reserve words
1. null
2. true
3. false

48 special words

keywords for flow control
1. if
2. else
3. do
4. while
5. for
6. return
7. switch
8. case
9. continue

keywords for Exception Handling

keywords for Access specifier

keywords for Access modifier

keywords for classes 

keywords for object 
36.instance of

Keywords for data type 

What does Java Compiler do

Job of Java Compiler

Before knowing What does Java Compiler do or Job of Java Compiler, You should know What is a compiler? Compiler is a collection of predefined programs which convert source code into machine understandable code. In case of Java Compiler source code is not converted into machine code directly, It is first converted into Byte code then translated into machine code by JVM. But other compiler directly convert source code into machine code. For ex- 'C' Compiler.
Due to this feature of Java, It is a Platform Independent Language. Java has both the Compiler and Interpreter. Compiler converts source code into byte code and the Interpreted i.e. JVM converts byte code into machine code.

Job of Java Compiler-

1. Java Compiler loads source code from Hard Disk to RAM.
2. Java Compiler converts source code into Intermediate code if source code is syntactically     correct.
3. Generate Byte Code i.e.  .Class file.

Note-  By default Byte code is saved into current working directory.

Oct 10, 2014

Internal mechanism of Platform Independent Language

Platform Independence means that no dependency on Platform. Here Platform mean is environment.We will know here Internal mechanism of Platform Independent Language. Learning Java is too easy but understanding it some what difficult. Any one can create programs on java but very few people know the Internal mechanism of java concepts. In this post we are going to discuss about Internal mechanism of Platform Independent Language. As we are discussing on Java and Java is also a Platform Independent Language. So I will explain Internal meaning of Platform Independence and What is the meaning of Platform Independent Language?
First of all think only one thing  that can you execute java program made in windows environment on Unix environment or vice versa? If you are unable to answer, I tell you. 'Yes' we can execute java program in any environment. For this only one thing is needed that is called JVM. Java's most powerful feature is that program written in java can we run on any platform. This powerful feature of java makes it a Platform Independent Language. Java is able to do this by JVM. JVM stands for JAVA VIRTUAL MACHINE. JVM is the interpreter that  translates java byte code into machine code.Due to this programs are executed on any machine. 
So, Platform Independent Language means, the language that does not depend on any platform. Program written on that language can be run any where on any platform. Program made on Platform Independent Languages are called as Platform Independent Programs. We can explain Platform Independent Programs as the programs which are created and compile on one particular machine and are able to execute any where i.e. on any O.S. 

Sep 28, 2014

Internals of Stream in Java

Why do we need Stream?

As we know that Stream is a sequence of data. So we need need this Stream to access data so that we could do I/O operation on that data. Below are some reasons which will tell us why do we need stream.

1)In order to perform input/output operation ,we need Stream.
2)Without Stream ,we cannot perform input/output operation.
3)To save your output and input in the hard-disk permanently,we need Stream.

Banking Application(Before came to the concept of database, developer used
STREAM for developed bank application or etc).

Internal mechanism of File Handling in java

Internals of File Handling in Java

File Handling is a very important topic in java. We can read, write, update files through file handling. It gives us authority to modify files through this mechanism. We will read here internal mechanism of file handling. How files are being read, How Input/Output operation are being performed on file. In order to read about all this we need to understand STREAM. We will discuss here-

  *What is Stream?

  *What is INPUT?

  *What is OUTPUT?

  *What is Input Stream?

  *What is Output Stream?

  *What is Byte Stream?

  *Why do we need Byte Stream?

  *What is Character Stream?

  *Why do we need Character Stream?

  *How many ways to take Input from keyboard?

We will get all answer here while reading all posts. First I would like to tell you concept of File Handling.

File Handling-

We can  read or write something from/to file with file handling mechanism. Our basic purpose is to retrieve or update file through this mechanism. For this we need to connect particular file with java environment. We create a connection between java environment and file and a Stream of data passes from this connection. Stream can be said as a sequence of data.

File handling functions are:

·         We can create  file for reading or writing purpose

·         Read data from file 

·         Writing data in file

·         Appending data to file

·         Assigning name to file
·         Renaming file
·         Positioning to particular byte in the file
·         Checking end of line in the file
·         Can find size of the file
·         We can delete the file
·         Returning result of last file input/output operation
·         Closing file.

Below is a program to understand Internal Mechanism of File Handling.


public class ZCopyFile {
   public static void main(String args[]) throws IOException
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         int uc;
         while ((uc = != -1) {
     finally {
         if (in != null) {
         if (out != null) {

Please Click here to read next about file handling in java.

Mar 14, 2014

How many ways to declare main() method in java?

There are so many ways to declare main() method  or so many predefined signature to declare main() method in java which could be understand by JVM.


for JDK1.0 up to all above version.
1.psvm(String[] a) 
2.spvm(String[] a) 
3.psvm(String []a) 
4.psvm(String a[]) 
5.spvm(String []a) 
6.spvm(String a[])
for JDK1.5 up to above version(VAR-ARGS) 
1.psvm(String... a) 
3.psvm(String ...a) 

4.spvm(String... a) 
6.spvm(String ...a)