Thursday, November 26, 2009


Example to Determine if a File or Directory Exists

This program checks whether the file or directory exist in your computer or not. This can be possible using exists() method and in the example we are using if condition to check whether the given file or directory exists or not. The example is given below :

import java.io.*;
public class filedirexists{
public static void main(String args[])
{
File filedir=new File("File or Directory exists or not!!")
boolean exists = filedir.exists()
if (!exists)
{
System.out.println("The File/Directory you are looking for is not found")
}
else
{
System.out.println("The File/Directory you are looking for available")


Sunday, November 15, 2009


Comparing Two Dates

Here your will find the example to compare two dates in java. We use java.util.Date class and compareTo() method for comparison. The compareTo() method returns integer value when we comparing two dates.

The compareTo() method returns the following integer value if comparing two dates :

The integer value '0' returns if both dates are equal.
The integer value '1' returns when first date is greater than second date.
The integer value '-1' returns when first date is less than second date.

Here is the example of comparing 2 dates and display the final result :

import java.util.Date;
public class Comparedates{

public static void main(String[] args) {
Date Date1 = new Date();
try{
Thread.sleep(1000);
}catch(Exception e){
}
Date Date2 = new Date();
System.out.println("First Date:="+Date1);
System.out.println("Second Date:="+Date2);
if(Date1.compareTo(Date2) > 0)
System.out.println("Date1 is Greater than Date2");
else if(Date1.compareTo(Date2) < 0)
System.out.println("Date1 is less than Date2");
else
System.out.println("Date1 and Date2 are equal!!!");
}
}


Wednesday, March 25, 2009


JDBC and JDBC Driver Connectivity

The JDBC or Java database connectivity is a unique technology that empowers applications to be written once but run anywhere. JDBC enabled devise driver could access corporate data even if the environment is not homogeneous. This unique feature is very much Java specific for Java itself is platform independent and Java API which is actually a short term of JDBC enables the easy execution of SQL statements.

JDBC is an application program to the core used to access database in tubular format encrypted by Java and driven by standard interfaces. RDBMS like SQL and Oracle are quarried and updated by JDBC with its niche in using and allowing heterogeneous implementation with the same application.

Since SQL is supported by all Relational Database Management System any corporate database could be accessed, quarried or updated via this single Java written JDBC program. In order to interact with database Java interface is used by programmers but it is the job of a JDBC driver to implement the interface according to the data base management system. Though Java Database Connectivity Driver categorically implements connection some are better suited for specific applications over the other types.

Four types of JDBC driver connectivity are common with Type 1 driver heading the list. It’s a popular, platform independent driver that uses ODBC to connect to database. Type 2 Native API driver is not coded by Java in its entirety making it more versatile in interpreting client libraries of database into recognizable native calls.

Application server, namely middle tier technology is used by Type 3 drivers and therefore client software is not required and database access could be gained through internet.

A type 4 driver also known as Native Protocol Driver and is a complete Java written program, which displays superior performance over the other two types and is completely platform independent. Communication with client application is speedy and easy as direct conversion of JDBC into vendor database protocol is enabled excluding middleware function; however a separate client side driver is required for a different database which poses the only challenge at times.


Tuesday, March 17, 2009


Changing Mouse Icon Example

In this example, you will learn how to change the cursor icon. In this program, the cursor will change when user moov the mouse to "Yes" button and then "No" button. The source code is available below :

import java.awt.*;
import java.awt.event.*;

public class ChangeCursor{
public static void main(String[] args) {
Frame m = new Frame("Change cursor");
Panel panel = new Panel();
Button b1 = new Button("Yes");
Button b2 = new Button("No");
panel.add(b1);
panel.add(b2);
m.add(panel,BorderLayout.CENTER);
m.setSize(300,300);
m.setVisible(true);
Cursor c1 = b1.getCursor();
Cursor c2 = b2.getCursor();
b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
b2.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
m.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});

}
}