Tuesday 5 June 2012

Steps to Develop First Jdbc Applications

To Write Standard JDBC Application we need to follow the following Sequence of steps.


1. Loading the Drivers/ Registering the JDBC Driver with Driver Manager Service
Registering Drivers is nothing but creating an object of specific JDBC Driver which is developed by dabtabase vendors in General
   
             Driver Manager Class contains the following static method to register JDBC Driver with Driver Manager Service.
                                                 java.sql.DriverManager
                                                            |
                                                            |
                                                public static void registerDriver(Driver)
In the above method Driver is an interface which is present in java.sql.* package.all the specific JDBC Drivers must implements java.sql.Driver

ex:1
 sun.jdbc.odbc.JdbcOdbcDriver d=new sun.jdbc.odbc.JdbcOdbcDriver();
  DriverManager.registerDriver(d);
                                       (or)

java.sql.Driver d=new sun.jdbc.odbc.JdbcOdbcDriver();
DriverManager.registerDriver(d);
                             (or)

DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());

above example code indicates driver which is type-1.

2.Obtain the Database Connection by using DriverManager Service
Obtaining the connection is nothing but establishing the communication channel between java application and database software.
                            java.sql.DriverManager
                                          |
                           public static Connection getConnection(String url,String uname,String password)
                           public static Connection getConnection(String url)(this is for excel,access,notepad...)


ex:Connection con=DriverManager.getConnection("jdbc:odbc:ss","system","tiger");


3.Create Statement Object for sending SQL Queries to the database
Creating the Statement object is nothing but establishing the communication service for sending SQL Statements.
                                     java.sql.Connection
                                               |
                                      public Statement createStatement()
ex: Statement st=con.createStatement();

4. Use Statement Object for  sending SQL Queries to the Database software execute the Query in the Database and get the result back from database to java application

Using an object of Statement interface is nothing but passing the SQL Queries to the Database
the SQL Queries are classified into 3 types
          a)DRL  b)DML    c)DDL
   if it is DRL use


   java.sql.Statement
            |
  public ResultSet executeQuery(String)

  if it is DML


 java.sql.Statement
         |
public int executeUpdate(String)

5. Close the Database Connections
it is highly recommended for the java programmer to release/ close the db connection after retrieving the data from the db.
                                 java.sql.Connection
                                              |
                                       public void close()


Example Program for retriving the Data from oracle by using type-1 Driver
for this we need to create DSN for system
steps to create DSN
control panel---->administrative tools--->Data Sources(ODBC)--->add---->select oracle in XE(if it is oracle db)--->give datasource name(sri)--->ok

Program to Select Data from Oracle Database

import java.sql.*;
class first
{
public static void main(String arg[]) throws Exception
{
sun.jdbc.odbc.JdbcOdbcDriver d=new sun.jdbc.odbc.JdbcOdbcDriver();
DriverManager.registerDriver(d);
System.out.println("Drivers are loaded");
Connection con=DriverManager.getConnection("jdbc:odbc:sri","system","manager");
System.out.println("Connection is established");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
while(rs.next())
{
String s1=rs.getString(1);
String s2=rs.getString(2);
System.out.println(s1+"       "+s2);
}
con.close();
}

Program for Inserting /Updating the record from database

import java.sql.*;
class second
{
public static void main(String arg[]) throws Exception
{
sun.jdbc.odbc.JdbcOdbcDriver d=new sun.jdbc.odbc.JdbcOdbcDriver();
DriverManager.registerDriver(d);
System.out.println("Drivers are loaded");
Connection con=DriverManager.getConnection("jdbc:odbc:sri","system","manager");
System.out.println("Connection is established");
Statement st=con.createStatement();
String s1="insert into emp values(1,'abc','hyd')";
int i=st.executeUpdate(s1);
System.out.println("row is inserted");
con.close();
}
}

No comments:

Post a Comment