Friday, April 10, 2009

Java DataBase Connectivity.....Simple

Here we are with the java database connectivity....
Suppose you are using a Oracle database with user name and password as root and root....

Now as we have already seen that we always prefer to use type 4 driver and also the "BUILD PATH " has to updated with the corresponding jar file....(can be downloaded from http://www.oracle.com/technology/software/tech/java/sqlj_jdbc/htdocs/jdbc101040.html)

We look at the JDBC in the steps....This makes it look lot more simple to learn and implementation always remains the same,i.e. always same steps have to be followed....

STEP 1:Register for the driver using Class.forName("oracle.jdbc.driver.OracleDriver");
=>this registers for the driver which will be used by ur
program to interact with the database
=> string argument in the forName method is a class developed by Sun
Microsystem .It's in a fully qualified package structure.
=>this is a checked exception type...meaning you have to use try,catch
or you have to delegate it the caller by using throws..

STEP 2:Get the connection object by using:
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE",
"root","root");
=>this basically gives the connection object by using the
driver that you have register in the
previous step.
=>now we have established the connection with the
database...
=>this again is a checked exception type.
=>first string in the arguments list is the url of the
database,thin is the type of the database in oracle,
@localhost meaning its there in you system...can
also be the ip address,1521 is the port number for
Oracle DB and at last as we know is the user name
and the password.(given at the time of installing Oracle database).

STEP 3:Get the statement objects:
Statement stmt = con.createStatement();
=>this basically helps you in executing any type of SQL
query, may be of type ddl,dml,dql..
=>this again is a checked exeception type.

STEP 4:Generate sql query which you want to execute,in our
case as:
String sql = "create table student(name varchar(12),ssn int,primary key(ssn));";
=>it can be any sql query that you want to execute.

STEP 5:Execute the sql query as:
stmt.execute(sql);
=>this creates a table in the db by name student...

STEP 6:close all the connection...
=>this again is a checked exeception type.
=>this step according to me is the most importent step and
ignored by people most of the time...its closes all the
connection that we have created...
Here we are with the complete implementation of
the JDBC...

package jdbc.con;
//this is package used to organize the project

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
//all the packages needed to be imported

public class Connectivity
{
public static void main(String[] args)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex)//here we are to handdle the exeception
{
ex.printStackTrace();
}
Connection con =null;
Statement stmt = null;
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "root", "root");
stmt = con.createStatement();
String sql = "create table student(name varchar(12),ssn int,primary key(ssn));";
stmt.execute(sql);
System.out.println("see the data base");
}
catch(SQLException ex)//throws SQLException
{
ex.printStackTrace();
}
finally//best way to close all the connections
{
try
{
if(stmt != null)
{
stmt.close();
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try
{
if(con != null)
{
con.close();
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}//end of main
}//end of the class
So here we are with the Java program which makes changes in the DataBase,the way you want it to make changes....Its simple and really feels great to have interaction from any Java Program to DataBase......As i always say:"Enjoy the Free world of JAVA"..

No comments:

Post a Comment