Tuesday 5 June 2012

Dynamic Queries

A Static query is one in which the data is passed with in the itself


In order to improve the performance of JDBC application we must use the concept of  Dynamic Queries.
to execute dynamic queries we must use an interface called java.sql.PrepardStatements.

                                  java.sql.Statements(interface1)
                                             |(extends)
                                  java.sql.PrepardStatements(interface2)

In order to set the values for the Positional Parameters we must use the following methods 
                             
 java.sql.PreparedStatement
public void setByte(int,byte);
public void setShort(int,short);
public void setInt(int,int);                                          
public void setLong(int,long);
public void setFloat(int,float);
public void setDouble(int,double)
public void setChar(int,char);
public void setBoolean(int,boolean);
public void setString(int,String);

In order to execute the dynamic Queries we have to use following methods present in java.sql.PreparedStatement
  a) public int executeUpdate()
  b)public ResultSet exeucteQuery()
  c)public boolean execute()

a)method for executing DML Statement
b)method for executing DRL Statement
c)method for executing Functions,StoredProcedures in pl/sql

Example Program to describe DML by using PreparedStatement
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");
String qry="insert into emp values(?,?,?)";
PreparedStatement ps=con.prepareStatement(qry);
DataInpuStream dis=new DataInputStream(System.in);
System.out.println("Enter empno");
int eno=Integer.parseInt(dis.readLine());
System.out.println("Enter ename");
String ename=dis.readLine();
System.out.println("enter emp salary");
int esal=Integer.parseInt(dis.readLine());
ps.setInt(1,eno);
ps.setString(2,ename);
ps.setInt(3,esal);
int i=ps.exeucuteUpdate();
System.out.println("Row is inserted");
}
}

No comments:

Post a Comment