Archive for the ‘OPPS’ Category

Statements In JDBC JDBC Interview Questions JDBC Statements PreparedStatement and CallableStatement JDBC Technology Call Stored Procedures

Statements In JDBC


Statements  in JDBC are carrier for sql queries in JDBC.  Statements in JDBC are obtained from connection objects.

Connection is the handle for the session between JDBC and Database server.Without connection object we cannot have a statement object and without statement object,sql queries cannot be run at the database server side from the JDBC code.

There are three types of statements in JDBC.These are as follows :


2.Prepared Statement

3.Callable Statement


1.Statement : These statements are used to execute queries like select statements at the server side. Every time a statement is run it is compiled at the server (database side),it is compiled at the server side and run.

The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "select * from tablename";
Statement st  = conn.createStatement(sql);


2.Prepared Statement : These statements are used where we have to execute same query multiple number of times with small changes like update value.In such cases we do not need to compile the query again and again at the server side but we need to compile he query once and keep executing again and again at the the server side.Thus,prepared statements help in this case as these are precompiled statements which can be executed ample times thus saving time.


The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "insert into tablename values (?,?,?)";
PreparedStatement pst  = conn.prepareStatement(sql);


The ‘?’ are placeholders to feed the value to the precompiled statements.setXXX methods are used to set the values to the placeholders depending upon the  data type at the server side.In the code pst.setString(1,”java”),1 denotes the first place holder.


3.Callable Statement : These statements are used to execute stored procedures and function at the database server side.

The code looks like this :

Class.forName("driver class");
Connection conn = DriverManager.getConnection("connection string","username","password");
String sql = "{? = execute procesdureorfunctionname(?,?)}";
CallableStatement cst  = conn.prepareCall(sql);

String str = cst.getString(1);


Number of View :2061

Tags: , , , , , , , , , , ,

No Comments

Life Cycle Of A Servlet Explained Handling Servlet Life-Cycle Events Servlet Life Cycle Servlet Interview Questions Java Servlet Programming

Life Cycle Of A Servlet 

A servlet is a java code which runs at the server side to process request and return response to the client.

In other words, a servlet is an extension to the server which processes request and returns response as an HTML page to the client. Like applets we donot have main method in a servlet. As in case of applet,its life cycle is taken care by the browser itself,similarly in case of servlet its life cycle of a servlet is managed by the server/container.


Life Cycle Of A Servlet denotes the phases it goes through. These phases are symbolized by the life cycle methods in the servlet.As server manages the Life Cycle Of A Servlet, these life cycle methods are called by the server itself. Let us have a look at different Life Cycle methods Of A Servlet. These are as follows :


  1. init
  2. service
  3. destroy


1. init : The init method is the first Life Cycle method Of A Servlet. It is this method which after being called by the server makes a servlet eligible for being called a servlet. After the servlet class constructor is called,the init method is called by the server. The method looks like :

public void init(ServletConfig sc) throws ServletException {
  // Initialization code...


Here we can see that the ServletConfig object is passed to the init method. This object is created by serverand passed to the init method. Having received the ServletConfig object ,a servlet is said to be a servlet.

2. service : This life cycle method is called every time a request is sent to the servlet .This means that the servlet’s init method has been called and servlet is ready to process requests.This method looks like :

public void service(ServletRequest request,
                    ServletResponse response)
      throws ServletException, IOException{

3.destroy : This is the last phase in the Life Cycle Of A Servlet. Before the servlet object is garbage collected at the server side this method is called to relinquish the resources at the server side. This method looks like :

public void destroy() {
    // Finalization code...

It can be noted that the init method is called only once in the Life Cycle Of A Servlet, service is called as many times as many the requests are fired and destroy is called only once in the Life Cycle Of A Servlet.

Number of View :1144

Tags: , , , , , , , , , ,

No Comments

Request Dispatching In Servlets Servlets / Request Dispatcher RequestDispatcher Vs SendRedirect What Is A Request Dispatcher And How Does It Work What Is A Request Dispatcher And How Do I Use It Include Forward Redirect

Servlet Request Dispatching

Delegating the request from one servlet to another or transfer of excution from one servlet to another is known as request dispatching in servlet.

In another ways we can define it as interservlet communication,which means one servlet calling another servlet.This concept can be used in cases where some piece of code is needed to be written in different servlets.Thus, in such cases we can write that code in a single servlet and keep on calling that code from different servlets.

There are three ways in which request delegation can be done. These ways are as follows :






1. Include : This method suggests that the response from the another servlet be included in the response of the first  servlet. This is equivalent to copy pasting the response of one servlet into another.In case of inclusion,the response which the client receives is the append of both including and the included servlets.However,for the client it appears that the including servlet only has responded.Following line of code is used to include a servlet response into another servlet :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");


Here RequestDispatcher object is needed .We can see that request and response objects of the first servlet are passed to the servlet which we wish to include through include method so that request remains the same and the included servlet can write to the response of the including servlet and it appears that including servlet itself has processed the request.

2. Forward : It works same as  include except for the difference that the reponse from the first servlet is flushed out and the reponse from the second servlet only is visible.Example code is :

RequestDispatcher rd = request.getRequestDispatcher("url-pattern of the servlet");


Here also it appears to the client that the first servlet only has processed the request.

3. Redirect : In the case of redirect the first servlet issues a new request to a another servlet to which redirection has happened.In this case client sees a new request in the browser url bar and can realize that the response comes from the second servlet.Example code is :

response.sendRedirect("url-pattern of the servlet");


Here we can see that response object is used.This done for the reason that the issuing of the new request at the client side,in the browser url bar has to be done by the first servlet.


Number of View :970

Tags: , , , , , , , ,

No Comments

Driver Registration In JDBC JDBC Connection Using the JDBC Driver Registering the JDBC driver Registering A JDBC driver JDBC Database Connections Basics Of JDBC How To Establish A JDBC Connection

Driver Registration In JDBC

An interface links two unrelated objects.A driver in JDBC context is an interface which links java code to database for communication.

Before ever the connection is established between the java programming end and the database end,the driver needs to be registered with the JVM by the means of DriverManager class.The DriverManager class has a vector of drivers registered to it.Once the entry has been made in the vector of the DriverManager class the driver is said to be registered.Each driver has the implementation and is capable of registering itself with the DriverManager class.This implementation is written in the static initialization block of the driver class.

We know that the static initialization block for a class is executed every time we load the class in the class loader. So, in order to run the static initialization block in the class loader we need to run the following line of code so that the driver registers itself with the DriverManager class :


We can load or compile a class in class loader by calling the Class.forName(“class name”) method of the reflection concept in java.

Now let us have a look at how  the static initialization block in the driver class looks like :


class Driver{

    DriverManager.registerDriver(Driver Class);


The function registerDriver() of the DriverManager class takes driver class as one of the parameters and looks for an entry ofr that driver in the vector it has. If an entry is found it means that the driver is already registered and the reference is returned from the vector.UIf entry is not available in the vector,the entry is made and reference is returned.This way we say that the driver is registered and connection can be made with the database as follows :

DriverManager.getConnection("Connection String","username","password");

Here connection String is depenedent on what database we are using.A typical example is :


Here ,username and password are username and password for the database.

Number of View :966

Tags: , , , , , , , ,

No Comments

Transaction Locking And Isolation In JDBC Transaction isolation levels Using Transactions JDBC Isolation Example JDBC Guide Basics Of JDBC

Transaction Locking And Isolation In JDBC

Transaction is a set of instructions which follow atomicity,consistency,Integrity and durability.

1. Atomicity : This states that either all actions should complete or none should occur.

2. Consistency : This imposes certain business rule.

3. Integrity : This states that the two actions shall not interfere with each other or access each others data.

4. Durability : This states that the changes occurred due to a certain action or transaction must be stored in a durable persistent storage.


When we talk about the Transaction Locking And Isolation In JDBC, following are the issues we need to address when to concurrent transactions are taking place :

1. Dirty read : This happens when uncommitted data is read.Typical example is :

a. Transaction 1 inserts a row into a table.

b.Transaction 2 reads the new row.

c.Transaction 1 rolls back.

2. Non-repeatable read : This happens when data reads from the same table for the same row are not same.Typical example is

a. Transaction 1 reads a row.

b. Transaction 2 updates the row.

c. Transaction 1 reads the same row a second time and gets a different  results.


3. Phantom Read : This occurs due to appearing of new records when same query is run at different times.Typical example is :

a. Transaction 1 reads all rows that satisfy a WHERE clause.

b. Transaction 2 inserts additional rows that satisfies the WHERE clause.

c. Transaction 1 executes  the WHERE condition and picks up the additional row.

There are different transaction isolation levels used from JDBC coding to address these issues.The function to set isolation level is :



Different integer values can be provided as follows :


Once we set the isolation levels using the above function,we can address the issues of transaction level in JDBC connection.As the isolation     level increases it gets stricter and lower level problems are also addressed.If we have set the value to 8 ,the isolation level with value lesser than 8 ,ie. 0,1,2,4, are also taken  care of.

Number of View :987

Tags: , , , , , , , , ,

No Comments

How Container Does The Cookie Work in J2EE Define Cookie Cookies Explained Session In Servlet What Is Cookie How Apache Tomcat Handles Cookies Cookies Explained How Cookies Work Cookie Guide

How container does the cookie work?


A Cookie is a key value pair primarily used by the container to pass the session id in order to maintain session between client and server. There can be user defined cookies as well to use cookie as attributes to pass values from one part of the web application to another.

The user only requests for the session,creating a session object,attaching the session id with the session object and sending it to the client side or the browser with the response is done by the server at the back end. These things the code has nothing to bother about,the code asks for the session and these things happen automatically.


The code below shows how to get a session object in servlet :

HttpSession sess = request.getSession();


With the help of the above written code,the session can be retrieved from the server.Following are the steps covered by the server as soon as this line of code executes :

1. HttpSession object is created

2. Unique Session id is created

3. A new cookie object is created.

4. Session id is associated with cookie

5. Cookie is set Into the response to be sent to client side


These are the steps involved in sending the cookie with the response if session does not exist .

The same code when executed does the work of retrieving a session object from request if it exists.If session already exists,the server finds the session id from the request(since cookie will be sent to the server from client side as part of the url).The server finds out the unique session id from the cookie and retrieves the session object with the matching Session id.

Thus all the cookie work happens behind the scene.

To know whether the session already existed or was just created,we can ask the session object itself as :


If it returns true ,it means session is created otherwise it is a preexisting session.

Number of View :1137

Tags: , , , , , ,

No Comments

Constructor And Destructor In C++ Basics Of OPPS Concepts Explained

Here we will see about Constructor. In that what is constructor,it’s types,use of constructor and some characteristics of constructor.

What Is Constructor?

Constructor is a special member function that constructs an object.In other words we can say Constructor initialize the objects of class.

The first responsibility of constructor is to calculate the memory requirement of an object and allocate the space for its data members.

How to Declare and Define The Constructor

Consider the following example,

class Number


int num;


Number()   // constructor declared




void show()


cout<< num <<“\n”;



void main()


Number N1; // class name  Variable(object);


In above example Number() is a constructor that construct the values of data members of the class.

Types of Constructor

  1. Default Constructor
  2. Copy Constructor
  3. Parametrized Constructor

Default Constructor has no argument,we write this constructor with default parameter.A Constructor that takes the parameters through dummy arguments is called Parametrized Constructor.The constructor that construct an object from some another object of same class is called Copy Constructor.

eg.  class Complex


float x;

float y;


Complex()       // default constructor





Complex( float a,float b)   // parameterized constructor





void show()




};       // end of class

void main()


Complex P1;;    // it displays output 0  0,as x=0 & y=0

Complex P2;,12);  // it displays output 6  12.


Properties/Characteristics of Constructor

This function have some properties.

  • It must have exactly the same name as of the class.
  • Constructor do not have any return type,not even void also.
  • It should declared in public section.
  • We cannot have virtual constructor.
  • When we create the object,the constructors are automatically invoked.


It is a special member function that destroy the object.It is opposite to constructor,the dectructor gets called before some objects gets destroyed.

Destructor also has the same name as of class name,but only the difference is destructor name should be preceded with the sign tilda(~).

syntax:  ~ classname();

The objects are always destroyed in the reverse order of their creation.

Number of View :1229

No Comments

Content Protected Using Blog Protector By: PcDrome.

Mahol Dot Org is Stephen Fry proof thanks to caching by WP Super Cache