September 28, 2011

Connecting Java program to Database


It is very simple to establish a connection to the Database from a Java program

         Class.forName("com.mysql.jdbc.Driver");
         String url = "jdbc:mysql://serveripaddress:portnumber";
         Connection con = DriverManager.getConnection(url,"username","password");

In first line, Class.forName() method is mainly used to register the Database Driver.

The second line specifies the URL for establishing connection to the database, which you want to connect.
URL stands for "Uniform Resource Locator". You will be familiar with HTTP URL's that you normally use to access a web site (Eg: http://www.websitename.com). URLs are used to identify a resource using a unique name.Similarly JDBC also uses URL  to connect to the database.

In HTTP, you will begin a URL with the protocol name i.e. http: . Similarly, the JDBC driver URL also should start with the protocol name jdbc: . Next, the subprotocol represents the database you want to connect (eg: mysql, oracle, odbc, etc.). While subname provides additional information on how and where to connect.

      jdbc:subprotocol:subname

August 22, 2011

Integer class in Java

          Integer class is one of the predefined class in Java.
The Integer class wraps a value of the primitive type int in an object. Like other classes, Integer class also have fields, constructors and methods.
First we will see some important  fields.
Fields:
               Fields are nothing but the variables which are present in the class. All these variables are static.
1. MAX_VALUE :
                          A static field holding the maximum value of the integer can have. i.e 231-1 (or) 2147483647.
2. MIN_VALUE :
                          A static field holding the maximum value of the integer can have. i.e -231 (or) -2147483648.
         "Hereafter if you want to know the maximum and minimum range of integer datatype, don't go for google search. Just use the MAX_VALUE and MIN_VALUE fields in the Integer class".
3.SIZE :
              SIZE field is used to indicate the number of bits used to represent an int value in two's complement binary form.

For Example,

class IntegerPractice
{
    public static void main(String ... args)
    {
        Integer n = new Integer(10);
        System.out.println(n);  // Output : 10
        System.out.println(
Integer.MAX_VALUE);  // Output : 2147483647 
        System.out.println(Integer.MIN_VALUE); // Output : -2147483648
        System.out.println(Integer.SIZE); // Output : 32
    }
}


Constructors:
          There are two constructor's in Integer class.
1.Integer( int value) 
              Constructs a newly created Integer object that represents the int value.
2.Integer(String s) 
             Converts the given string into integer and assign it into the Integer object. 
   Note: There is no default constructor in Integer class.So, we cannot create an Integer object with no arguments.

Methods:
                  Now we will see some important methods in Integer class.0
1. public static int parseInt(String s)
                  This method will get the string as input and returns it as integer value.The characters in the string must all be digits.
2. public static int parseInt(String s,int radix)
                  Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix. 
3. public static int reverse(int i)
                Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified int value.
4. public int compare(Integer anotherInteger)
                 This function is used to compare Integer objects.the value 0 if this Integer is equal to the argument Integer.A value less than 0 if this Integer is numerically less than the argument Integer and a value greater than 0 if this Integer is numerically greater than the argument Integer.
5. String toString()
                Returns the given Integer value as a string.

6. byte byteValue()
                Returns the Integer value as byte.
7. int intValue()
                Returns the Integer value as int.
8. float floatValue()
                Returns the Integer value as float.
9. long longValue()
                Returns the Integer value as long.
10. short shortValue()
                Returns the Integer value as short.

August 19, 2011

Difference between String and StringBuffer

The main difference between String and StringBuffer is that the former is immutable, while the latter is mutable.
By immutable, i mean that the value stored in the String object cannot be modified. Whereas, we can modify the values in StringBuffer.

For example,
Case 1:
               String str = new String("This is first ");
               str.concat("String");
               System.out.println(str);    //Ans : This is first
Case 2:
               str = str.concat("String");
               System.out.println(str);    //Ans : This is first String

In the first case, the value is not appended, since the string is immutable. In the second case, we assigning the appended value to the object.
From this it is obvious that String is immutable.

August 18, 2011

Data

Data means source of meaningful information. In other words, it is set of values (or) a value that denotes "something".

Data Structure means that Systematic or Organised way of storing the data that shows some relationship among the data.

Codd developed the relational model for database management system. Relational model means that the way of storing the information in tables.

August 16, 2011

String class in Java

String:
String is a sequence of characters and it is widely used in Java.
First thing which we need to know about string in Java is that, it is not a primitive data type.
String is one of the predefined class that is implemented in Java. It helps to handle the strings in a required way.

We have to create an object or reference variable for the String class.

Syntax:
              String str = new String();

String Constructors:
There are many constructors in String class. Now we will see the most important ones.
  1. String( ) - Initialises the String object with no values in it.
  2. String(String str) - Initialises the String object with the values in str.
  3. String(byte[ ] b) - Initialises the String object using byte array b. Normally a byte stores numerical value. In this when we pass a byte array, the numerical values will be taken as ASCII values and converted into corresponding characters. Then the characters will be assigned to the String object.
  4.  String(byte[ ] b, int startindex, int count) - Initialises the String object from the byte array b starting from the startindex and length be the count.

    August 03, 2011

    Abstract in Java

    • An abstract class or method can be declared by using the keyword abstract.
    • Example
                       abstract class AbstractClassname{   }
                       abstract returntype methodname();
    •  All methods inside the abstract class should be declared abstract.Otherwise, the method definition should be provided for the particular method in the abstract class itself.
    • Abstract class can be extended to any class by using the keyword extends.
    • Example
                      class classname extends AbstractClassname{    }
    • Abstract class is mearly like a ordinary class except that we cann't be able to create objects from it.
    • When an abstract class is subclassed, the subclass should provide implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.
    • We can create constructors for the abstract classes.

    Difference between Interface and Abstract class

    There are many difference between Interface and Abstract class
    •  By using interface we can acheive Multiple Inheritance and it cannot be done by using abstract class.
    • In interface, we can declare only static final variables, where as in asbstract class we can also declare non-static and non-final variables.
    • In abstract class we can also declare non-abstract methods and it cann't be done in interface.

    August 02, 2011

    Interface in Java

    • In real world, Interface is nothing but thing which acts as connection between two objects.
             For example,
                    Power button acts as an interface between Human and Computer.
    • In Java,Interface is created by using the keyword interface.
    • The interface name can be start with letter I, which is used to identify that the particular block is an Interface.
    • In interface, we can only declare variables and methods.
    • The initerface is implemented in a class by using the keyword implements (Refer Syntax)
    • The variables declared in interface are by default "public and final" .
    • The definition part of the method should lies in the class that implements the particular interface and the method should be declared as public.
    • A class can implement any number of interfaces and it is used to achieve multiple inheritance in java.
    Syntax:

               interface Interfacename
               {
                   
                    //Variable and Method declaration
                }

               class Classname implements Interfacename
              {
                   //Definition for methods in Interface
               }

    July 27, 2011

    Use of super keyword in Java

    1. Super keyword can be used to access the super class constructor from the subclass constructor.
    2. Super must be the first function in the subclass constructor.
    3. It is the first function to be executed in the subclass constructor.
    4. Super function will access the super class constructor according to the matching arguments.
    5. It can also be used to access the members of the Super class.
    6. When method overriding is done,then super can be used to access the overrided method of super class from subclass.

    My understanding of Inheritance in Java

    1. A class can be inherited by using the keyword extends.
    2. The class which is inherited is called Super class or Parent class.The class which does the inheritance is called Subclass.
    3.  The private members of the super class are cannot be accessed by the sub class.

    July 13, 2011

    My definition for Process

    • A Process is a set of actions or activities that is being carried to acheive the goal.
    • It should be processed and managed to find the best way to attain the solution.
    • A process has to be managed in such a way that it can be carried out independent of same people.

    July 11, 2011

    Things to remember while developing a New programming language

    • Varibles
    • Keywords
    • Operators
    • Syntax
    • Conditional statements
    • Looping statements
    • Predefined functions
    • Garbage Collection (GC)  -->  It frees the memory of the unused variables.
    • Data Structures

    Advantages of OOPS

    • Effective memory management
    • Class
    • Objects
    • Encapsulation
    • Polymorphism
    • Reuse  --> Time management, Resourse management
    • Easy to Maintain/Understand/Implement

    Disadvantages of Procedural languages


    1. Procedural languages are difficult to relate with the real world objects.
    2. Procedural codes are very difficult to maintain, if the code grows larger.
    3. Procedural languages does not have automatic memory management as like in Java. Hence, it makes the programmer to concern more about the memory management of the program.
    4. The data, which is used in procedural languages are exposed to the whole program. So, there is no security for the data.

    Examples of Procedural languages :

    • BASIC
    • C
    • Pascal
    • FORTRAN

    July 08, 2011

    Points to remember while developing a program

    • Don't Assume yourself about the result of the program.
    • Donot call any function inside any loop.This may reduce the performance and processing speed of the program.
    • Documenting the code is important when we design a program. It should be done before the classes and methods.
    • Documentation helps to maintain the program and i'll help for the  better understanding of program.

    July 07, 2011

    Points to consider while designing a Optimal solution

    • The program should be opt for various Inputs.
    • Minimize the number of loops in the program.
    • Minimize the use of Variables.
    • Consider the memory usage of the program.
    • Do proper testing in the program.

    Connecting Java program to Database

    It is very simple to establish a connection to the Database from a Java program          Class.forName("com.mysql.jdbc.Driver...