Wednesday 21 March 2012

Create your own library package in Java

Today I am going to post on how you can create your own package and can import it from anywhere just like yo use import java.io.* .For this you need to do following things :
1 : Since you are thinking about package so while writing your source files write
 package <packagename> without <> . For each of your subpackages write source files in subfolders and give proper package name. e.g. creating Paint do this
     package Draw for Draw class in Draw Folder. Then create a sub folder 2DShapes containing Rectangle and Circle source codes having first line as
    package Draw.2DShapes
 2 : Next you will have to gather all your class files in a similar way as your source. To gather them all separately folder by folder use the -d command while compiling at command prompt. Type the following code
   javac -d ../classes Draw.java if you are running cmd from inside Draw folder. But remember to create classes folder in same directory as that of Draw folder.
3 : Next assemble all these classes into a JAR file without a manifest file using -cvf command.
4  ; Next open the Java folder where you have installed Java. Then look for the following folders subsequently.I mean look for
  C:\Program Files\Java\jdk1.7.0\jre\lib\ext . Inside ext folder paste your JAR file.
That's It. Now you can use your own Draw package by importing from anywhere while writing your source codes. This method is known as Java Extension Mechanism. This method is followed while developing projects using Third-Party library functions.

Tuesday 20 March 2012

Java program to connect to a database

Anyone of Java programmers have heard of JDBC. Today I am going to discuss some easiest ways of connecting to a database usin JDBC. JDBC actually stands for Java Database Connectivity. To use this technique you need JDBC drivers first of all to run the java codes. You can use Sun's JDBC-ODBC bridge by using Sun Microsystem's JDBC driver available with easysoft. But I prefer to use jstels JDBC-MDB driver(for connceting to MSAccess database files)as it does not require any installations. I will discuss my codebased on it.We wull need SQL codes to execute the Queries. You need to do the following :
1 : import java.sql.*;
2 : Create a Connection object like this ->
Class.forName("jstels.jdbc.mdb.MDBDriver2");  //loads class files of driver
Connection c=DriverManager.getConnection("jdbc:jstels:mdb:record.mdb");
3 :  Next create a Statement object like  ->
     Statement stmt=c.createStatement();
SQL Writing follow steps below ->
1 : stmt.execute("CREATE TABLE Records(Roll LONG, Sname STRING(50))"); //for creating table having the coloumns as described
2 : stmt.execute("INSERT INTO Records(Roll, Sname) VALUES(100860,'Malinga')");   //inserting values in fields

Related post and read after this --> Java program to connect to a database Contd.

Sunday 18 March 2012

Java program to list all files

This post is about a program that will be able to list all files in your computer's hard disk. This code uses java.io package for this purpose. Here I have use File class to list all the drives of your hard disk using listRoots() function. Then I have defined a recursive function that will go on till all the listings are over. The recur function takes in a File as parameter and if it is a file the it displays its absolute path and if it is a directory then again it lists all its contents and for each of its contents it calls the recur function recursively. Here is the code for you :
import java.io.*;
public class FileList {

    static void recur(File f1){
        if (f1.isFile()){
            System.out.println(f1.getAbsolutePath()); //displays path for a file
            return;
        }
        else{
            File f[]=f1.listFiles();  //lists all contents if a directory
            for (int j = 0; j<f.length; j++)
                try{
                recur(f[j]);  //calls recur for each content of directory
                }catch(Exception e){e.printStackTrace();}
        }
    }
   
    public static void main(String[] args){
        File []A=File.listRoots();  //lists all drives
           try{
               for(int i=0;i<A.length;i++)
                recur(A[i]);  //calls recur for each drive
            }catch(Exception e){e.printStackTrace();}       
    }
}

Sunday 11 March 2012

Best News

I have just done a rocking performance. I think that it is the biggest achievement in my life so far. I suggested some things to a Nokia S40 phone app which has given it a new look. For this performance I was asked to accomplish the job but due to lack of time I couldn't complete it. But for my recommendation Nokia has given me a software for easy creation of Nokia and Windows phone app. Besides this from now on I can advertise my apps on Windows Marketplace and Nokia Store and can also monetise my apps.

Friday 2 March 2012

Remove Recycle Bin from Desktop

In this post I will show you a trick to remove recycle Bin from desktop of Windows7. This is for all those who do not like the Recycle Bin on their Desktop.

1 : Go to Start>Run. Type gpedit.msc
2 : On the left panel under User Configuration expand the tree Administrative templates

3 : Click on the subtree Desktop. Do not expand it
4 : On the right panel select Remove Recycle Bin icon from desktop properties.
5 : Double click the option, on settings tab select the option enabled, press OK and exit the window.
6 : Refresh the desktop, Recycle Bin vanishes. If not then logout and re-login.