Books Related to Java Technology

Tuesday, December 30, 2008

Four Steps for Java Database Connectivity

Before you can create a java jdbc connection to the database, you must first import the

java.sql package.

import java.sql.*;

The star ( * ) indicates that all of the classes in the package java.sql are to be imported.

1. Loading database driver

In this step we load the driver class by calling Class.forName() with the Driver class name as an argument. Once loaded, the Driver class creates an instance of itself. A user can connect to Database Server through JDBC Driver. Since most of the Database servers support ODBC driver therefore JDBC-ODBC Bridge driver is commonly used.

The return type of the Class.forName (String ClassName) method is “Class”. Class is in class java.lang package.

try {

Class.forName(”sun.jdbc.odbc.JdbcOdbcDriver”);

} catch(Exception e) {
System.out.println(“Driver class not loaded!”);

}

2. Creating a oracle jdbc Connection

The JDBC DriverManager class defines objects which can connect Java applications to a JDBC driver. DriverManager is considered the backbone of JDBC architecture. DriverManager class manages the JDBC drivers that are installed on the system. Its getConnection() method is used to establish a connection to a database. It uses a username, password, and a jdbc url to establish a connection to the database and returns a connection object. A jdbc Connection represents a session/connection with a specific database. Within the context of a Connection, SQL, PL/SQL statements are executed and results are returned. An application can have one or more connections with a single database, or it can have many connections with different databases. A Connection object provides metadata i.e. information about the database, tables, and fields. It also contains methods to deal with transactions.

JDBC URL Syntax:: jdbc: :

JDBC URL Example:: jdbc: : •Each driver has its own subprotocol

•Each subprotocol has its own syntax for the source. We’re using the jdbc odbc subprotocol, so the DriverManager knows to use the sun.jdbc.odbc.JdbcOdbcDriver.

try{

Connection dbConnection=DriverManager.getConnection(url,”userName”,”Password”)

}catch(SQLException e) {

System.out.println( “Driver class not loaded!” );

}

3. Creating a jdbc Statement object

Once a connection is established we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.

Statement statement = dbConnection.createStatement();

A statement object is used to send and execute SQL statements to a database.

Three kinds of Statements

  • Statement: It execute simple sql queries without parameters. Statement createStatement() Creates an SQL Statement object.

  • Prepared Statement: It execute precompiled sql queries with or without parameters. PreparedStatement prepareStatement(String sql) returns a new PreparedStatement object. PreparedStatement objects are precompiled SQL statements.

  • Callable Statement: It execute for call to a database stored procedure. CallableStatement prepareCall(String sql) returns a new CallableStatement object. CallableStatement objects are SQL stored procedure call statements.

4. Executing a SQL statement with the Statement object, and returning a jdbc resultSet.

Statement interface defines methods that are used to interact with database via the execution of SQL statements. The Statement class has three methods for executing statements:

  • executeQuery()
  • executeUpdate()
  • execute()

For a SELECT statement, the method to use is executeQuery . For statements that create or modify tables, the method to use is executeUpdate.

Note: Statements that create a table, alter a table, or drop a table are all examples of DDL

statements and are executed with the method executeUpdate. execute() executes an SQL

statement that is written as String object.

ResultSet provides access to a table of data generated by executing a Statement. The table rows are retrieved in sequence. A ResultSet maintains a cursor pointing to its current row of data. The next() method is used to successively step through the rows of the tabular results.

ResultSetMetaData Interface holds information on the types and properties of the columns in a ResultSet. It is constructed from the Connection object.

Java DataBase Connectivity

  • JDBC is a Java API for executing SQL statements and supports basic SQL functionality. The JDBC ( Java Database Connectivity) API defines interfaces and classes for writing database applications in Java by making database connections. Using JDBC you can send SQL, PL/SQL statements to almost any relational database. It provides RDBMS access by allowing you to embed SQL inside Java code. Because Java can run on a thin client, applets embedded in Web pages can contain downloadable JDBC code to enable remote database access. You will learn how to create a table, insert values into it, query the table, retrieve results, and update the table with the help of a JDBC Program example.
JDBC Architecture

In This jdbc architecture programmer connect his/her application to database by using the jdbc driver. Java application calls the JDBC library. JDBC loads a driver which talks to the database.

Fig: JDBC Architecture

For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.



How to install Java?

What is Java and why do I need it?

  • Java is a programming language that allows programs to be written that can then be run on more than one type of operating system. A program written in Java can run on Windows, UNIX, Linux etc. as long as there is a Java runtime environment installed.

Where can I download Java?

Path Setting of Java
  • Windows 2000/XP users may set their path by right-clicking on 'My Computer' and selecting 'Properties'. Under the 'Advanced' tab, there is a button that allows you to set the 'Environment variables'. Click on this and alter the 'Path' variable so that it also contains the path to the Java executable. For example, if you have installed Java in c:\jdk and your path is currently set to C:\WINDOWS\SYSTEM32, then you would change your path to read C:\WINDOWS\SYSTEM32;c:\jdk\bin
    When you open a new command prompt, it will reflect these changes and allow you to run java programs by typing "java". If you have installed the SDK, then you will also be able to run "javac" to compile stuff.
  • Windows 95/98/ME users may find that their path variable is stored in a different place. Edit the c:\autoexec.bat file and add the following line at the end: SET PATH=%PATH%;c:\jdk\bin
    (This also assumes that you have installed Java in c:\jdk) .
  • Linux, UNIX, Solaris, FreeBSD users must set their PATH variable to point to where the java binaries have been installed. Please refer to your shell documentation if you have trouble doing this. For example, if you use bash as your shell, then you would add the following line to the end of your .bashrc: export PATH=/path/to/java:$PATH.


Sunday, December 28, 2008

History of Java

Java was started in June 1991 by James Gosling as a project called "Oak". The main goal of Gosling to implement a virtual machine and a language that had a familiar C-like notation but with greater uniformity and simplicity than C/C++. The first public implementation was Java 1.0 in 1995. It's main objective of "Write Once, Run Anywhere", with free runtimes on any platforms that's why it called platform independent. To develop a large web application Sun invented the J2EE and J2ME.

New versions for large and small platforms (J2EE and J2ME) soon were designed with the advent of "Java 2".

Download SDK and JRE

What are Access Specifiers available in Java?

  • Access specifiers are keywords that determines the type of access to the member of a class. These are:
  1. Public
  2. Protected
  3. Private
  4. Defaults

For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.

Explain about the Encapsulation in java

  • Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. One way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

How to explain garbage collection?

  • Collection is one of the most important feature of Java. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. User program cann't directly free the object from memory, instead it is the job of the garbage collector to automatically into a variable when no more in use. I Java on calling free the objects that are no longer referenced by a program. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. In Java, it is good idea to explicitly assign nullSystem.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected.

Explain about user defined Exceptions?

  • User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.
Example:

class myCustomException extends Exception {
// The class simply has to exist to be an exception
}

What is Synchronization in java?

Synchronization is a process of controlling the access of shared resources by the multiple threads in such a manner that only one thread can access one resource at a time. In non synchronized multithreaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption.

E.g. Synchronizing a function:
public synchronized void Method1 () {
// Appropriate method-related code.
}

E.g. Synchronizing a block of code inside a function:
public myFunction (){
synchronized (this) {
// Synchronized code here.
}
}

What is transient variable?

  • Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written to an ObjectStream, the value of the variable can't be written to the stream instead when the class is retrieved from the ObjectStream the value of the variable becomes null.




Difference between an Abstract class and Interface?

Differences:

  • Interfaces provide a form of multiple inheritance. A class can extend only one other class.
  • Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.
  • A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.
  • Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.

Similarities:
  • Neither Abstract classes or Interface can be instantiated.
For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.

What is Collection API?

The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hashtables if effectively replaces.

For more details you can visit:
http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.

Saturday, December 27, 2008

Polymorphism in java

Polymorphism, allows the software developer to reuse names as well as code. Polymorphism is a natural consequence of specialization (sub classing relationship implemented via inheritance), the associated principle of substitutability, and message passing.

Different forms of Polymorphism:- From a practical programming viewpoint, polymorphism exists in three distinct forms in Java:
  • Method overloading
  • Method overriding through inheritance
  • Method overriding through the Java interface

Abstract class in java

Java Abstract classes are used to declare common characteristics of subclasses. An abstract class cannot be instantiated. It can only be used as a superclass for other classes that extend the abstract class. Abstract classes are declared with the abstract keyword. Abstract classes are used to provide a template or design for concrete subclasses down the inheritance tree.

abstract class testAbstractClass {
protected String myString;
public String getMyString() {
return myString;
}
public abstract string anyAbstractFunction();
}


For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.

What is a Package?

A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the "Application Programming Interface(API) ". Its packages represent the tasks most commonly associated with general-purpose programming.

What is Inheritance?


Inheritance provides a powerful and natural mechanism for organizing and structuring your software. It is the capability of a class to use the properties and methods of another class while adding its own functionality. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes by the "extends" keyword. In the image we see 'duck', 'cuckoo' and 'ostrich' inherit the all properties of 'bird' which is the super class and the 'duck', 'cuckoo' and 'ostrich' are the sub class. For Example:
public class duck extends bird
public class cuckoo extends bird
public class ostrich extends bird




What is Interface?

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior of the interface. Interfaces are similar to abstract classes but all methods are abstract and all properties are static final. As an example, we will build a Working interface for the subclasses of Man. Since this interface has the method called work(), that method must be defined in any class using the Working interface.

public interface Working {
public void work();
}

When you create a class that uses an interface, you reference the interface with the reserved word implements Interface_list. Interface_list is one or more interfaces as multiple interfaces are allowed. Any class that implements an interface must include code for all methods in the interface. This ensures commonality between interfaced objects.

public class WorkingMan extends Man implements Working {
public WorkingMan(String name) {
super(name);
}
public void work(){
speak();
System.out.println("interface implemented");
}
}

What is class?

A class is a blueprint or prototype from which objects are created. In the real world, you'll often find many individual objects all of the same kind.
The following Bicycle class is one possible implementation of a bicycle:

class Bicycle {

int cadence = 0;
int speed = 0;
int gear = 1;

void changeCadence(int newValue) {
cadence = newValue;
}

void changeGear(int newValue) {
gear = newValue;
}

void speedUp(int increment) {
speed = speed + increment;
}

void applyBrakes(int decrement) {
speed = speed - decrement;
}

void printStates() {
System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
}
}

What is Object?

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Bundling code into individual software objects provides a number of benefits, including:

  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.

  2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.

  3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.

  4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.

Features of Java

  1. Easy to Use:- The fundamentals of Java is based on c++ programming language. The c++ programming language is based on Object oriented so, it is easy to handle by programmer.
  2. Reliability:- Java needed to reduce the fatal errors from programmer mistakes. With this in mind, object-oriented programming was introduced. Once data and its manipulation were packaged together in one place, it increased Java’s robustness.
  3. Secure:- As Java was originally targeting mobile devices that would be exchanging data over networks, it was built to include a high level of security. Java is probably the most secure programming language to date.
  4. Platform Independent:- Java was written to be a portable language that doesn't care about the operating system or the hardware of the computer. It can be run on any operating system and any machine.

What is Java?





  • Java is a “high-level” computer programming language.
  • It has a set of rules that determine how the instructions are written. These rules are known as its “syntax”.
  • The high-level instructions are translated into numeric codes that computers can understand and execute.
  • Java was created by a team led by James Gosling for Sun Microsystems. When Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages.
  • Java source code files (with a .java extension) are compiled into a format called bytecode (with a .class extension), which can then be executed by a Java interpreter. Compiled Java code can run on most computers because Java interpreters and runtime environments, known as Java Virtual Machines (JVM), exist for most operating systems, including UNIX, the Macintosh OS, and Windows. Bytecode can also be converted directly into machine language instructions by a just-in-time compiler (JIT).
For more details you can visit: http://www.worldinfosoft.com

This is a one of the best tutorial site, where you can find lots of example with full description of code and you van also prepare of your interview from this site WORLDINFOSOFT.COM.