Wednesday, 28 September 2011

Database Connectivity


Connecting a Database usually involves 2 important steps which are
  •      Establishing a connection.
  •      Connecting with the URL.

Establishing a connection:
      Inorder to establish a connection, we must load the driver that is going to act as an interface between your java code and the database. For this, we can use Class.forName() method that is defined in Class. This method will return the object of the interface by loading  the specifed driver which can be JDBC or ODBC or Sun depending on the Database going to be used. If for example, jdbc driver is specified, it will load the jdbc driver as the interface between your code and the database. 

Connecting with the URL:
     The Connection object must be created before connecting to the URL. This can be done as follows : 
     Connection con=DriverManager.getConnection(url,username,password); 
      DriverManager class provides a static method, getConnection() for connecting the host system with the database server. Here "url" is the path of the db server and "username" and "password" are the host system's username and password respectively. The "url" part consists of type of the driver followed by ip address or name of the system which inturn followed by port id. For mysql the default port is 3306. 

Example program for Database connectivity: 

import java.sql.*;
public class DBConnect
{
    public static void main(String args[]) throws SQLException, ClassNotFoundException
    {
        Connection con=null;

        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            con=DriverManager.getConnection("jdbc:mysql:// 172.220.26.5 : 3306/","guest","guest");
           
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            if(con!=null)
                con.close();

        }
    }
}
     And its a good practice to close the connection in the finally block, inorder to release the memory that was allocated for the database.     
     

Friday, 2 September 2011

What happens when we clone a object using clone() method???

        clone() method is available in the Object class. This clone method can be used to make an exact copy of the object. It copies the entire fields i.e. state of the object. For an example consider an employee details wrapped up in an object named empObj.
        Lets assume that it has fields such empID, empDesignation, empDept, empSalary, etc. When we clone this object, using clone() method, it returns a local object say empObj1. This object contains all the fields that the empObj has, and when you make change in empObj1, it won't reflect in empObj. This is something like saving a word document in another name using Save as option. The further changes made will be reflected in newly saved document and the older one remains unchanged. Thus clone() method comes into picture, when we need a local object with few changes.

Monday, 22 August 2011

Exceptions

Exceptions and Errors in Java
   To start with exceptions, we have to understand the difference between the Exceptions and Errors in java.
     In Java, Error and Exception can be differentiated based on the program control. Error cannot be controlled by the programmer whereas Exceptions are those that can be guessed and also handled easily. 

For example,
    The OutOfMemory error that occurring due to large size of input is an error and it can be handled only by the system dynamically.
    In other case, consider accessing an array value that is out of range can be guessed by the programmer and handled using  ArrayOutOfBoundsException.

 

How to sort an ArrayList in Java???

     As we know, ArrayList in Java is used to hold any type of data in an array.  ArrayList can be sorted easily using the sort() that is defined in the Collections class.  Since the method is static, we can call the sort method as follows:
     Collections.sort();

For e.g.

      ArrayList<String> arr=new ArrayList<String>;
      Collections.sort(arr);

Wednesday, 10 August 2011

Things Learnt from given Assignment

StringTokenizer and StringBuffer

       StringTokenizer is in java.util package which is used to separate the given string into individual tokens. The difference here is that StringTokenizer methods do not distinguish among identifiers, numbers, and quoted strings. Also they wont recognize and skip comments. That is, it even takes the commented lines in your program also String.

      StringBuffer is also in java.util package. It differs from the String by its mutable character. That is, the string's length and content stored in it can be changed in course of time.

ArrayList

    ArrayList  in java.util package which is advantageous because of its capability to store any kind of object. It is similar to array but the difference comes when efficiency(in storing the contents) is required.

Monday, 1 August 2011

Interface in Java

What is Interface in Java?
       In laymen term, Interface can be said as a medium that serves to connect two or more unrelated entities.  For example, keyboard is an interface between you and the computer. Telephone cables are interface between two people speaking over phone. The Language English is another easy example for an interface which is a universal communicating language.
   
      Now considering with respect to Java, an interface is a group of related methods with empty bodies. 

Why Interface? 
       It is something that is more useful Multiple inheritance comes into picture as JAVA does not support multiple inheritance.

Syntax for Interface: 

   interface ICalculator
  • here interface is the keyword
  • ICalculator is the name of the interface. Here Calculator is the Interface name and letter 'I' is added for easy understanding of the programmer that it is an interface.
  • It is not mandatory to have letter 'I' as starting letter.

       A class must have the following syntax to make use of that particular interface as below.
       class Classname implements ICalculator
  • Here implements is the keyword 
  • ICalculator is the interface name
  • Classname is the class that implementing that interface

Tuesday, 19 July 2011

  • The number of columns in the table must be reduced as much as can.
  • The database must allow future enhancement i.e. any additional data required can be added without much difficulties.
  • Instead of adding more columns, it is advisable to have more number of  tables ( even that too for a particular extent, since time for referring more no.of tables equals referring more no.of columns).
  • In a developer's perspective, the database must be simple enough in order to make the coding simple. By doing so, the future changes might not have more effect on the code.

Wednesday, 13 July 2011

Week 2

PROCESS ???

         Process in general can be understood as the task that is to be carried out to
meet our expectation. A Process needs some methodology to achieve that expectation. Regardless of whomever, achieving the expectation remains same if he or she follows same procedure.

        A process can be understood using the following example.

         Lets assume that you wanna cross a road. In this, crossing the road is our expectation. To meet this expectation we must ensure whether following questions are answered.
  • Whether the road is free, else we need traffic police to assist - Ensures the support
  • Whether we are going to cross - Ensuring the ability.
  • What mode you are selecting either by walk, or you can take a bike - Activities
  • How long the road is, How much time we are going to take for this and so on - Measure
  • Which method would make you reach faster- optimizing    
     
        By answering all these, we can achieve our expectation and thus the process is done.
                           

Friday, 8 July 2011

---July 8th--- 

    Hi everyone here is the tips for the proper documentation of a project. Use the following link

Tips for proper Documentation of a project
 
  The Mantra to be kept in mind - "Don't Assume" (especially for every s/w engineer)

Thursday, 7 July 2011

Things Learnt in STG

---July 7th---

Approaches that differs the Software Engineer and the Programmer

-->  Minimizing the No.of Loops
--> Minimizing the No.of Variables
--> Analyzing the Problem in all aspect (Thorough Testing)
--> Memory Conception  
--> Providing a more Generic solution 

Solution for a program can have

  - Datatype that is required
  - Checking for the necessity of loop(Iteration)
  - Looking for an Exception that may rise

      --> Throwing the exception since
  •  We cannot expect the client/user to give the correct datatype as input
  •  We have to look into the negative part of the problem
  - Finally the Logic that is used to solve the problem  
         
 
Morning everyone :-)
Cool Climate in Chennai !!! Nice to see