Well the important thing about forName() method of Class is that it basically loads the driver.
Now, what do we mean by loading the driver?
It basically takes the class name passed as the string and it executes the static blocks of that particular class.
Now these static block have the code that is needed for making Driver Manager aware of the fact that there is one more driver been added to the list of the available drivers.Hence when are request for that particular driver is made, it is available for the Driver Manager.
Whats interested to notice is the fact that forName() does not create an instance of the class passed as string parameter.It basically executes the static part of a class.
Showing posts with label JDBC. Show all posts
Showing posts with label JDBC. Show all posts
Sunday, July 4, 2010
Friday, July 31, 2009
Is it that we should always use preparedStatement???(statement vs preparedStatement)
Well when it comes to the competition between preparedStatement and the statement object in JDBC,most of the time its preparedStatement that wins the game....
Reason being simple and not suppressing....
1:)Performance
2:)Efficiency
Actually when we get the statement object from connection object in JDBC there are three things that are done on DB side "FOR EVERY RUN OF THE JAVA PROGRAM"...
#1:The database query is first compiled
#2:there is a query plan that is generated by the database manager
#3:than at last we have the query being executed...
Now if we consider the case of the preparedStatement,than at the first run or execution of the program there is all the three steps that are carried out..but for the subsequent running of the program there will only be the last step that is carried out...
That's because the compiled query and the execution plan is already there with the DB...
Well than whats the use of having the statement object????
the prepapredStatement is considered to the static object...as there is only one type of query that you can possible execute with it....
Whereas in case of the statement object it considered to be the dynamic object as you can execute multiple query from one statement object.....
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"..
Friday, February 27, 2009
Types of Drivers....


Anyway yesterday i thought of writing about the Hibernate-the framework in java but than to know about that we need to know the Java DataBase Connectivity(JDBC) and to know about the JDBC we must know about the different types of drivers.......
So here we are with the different types of drivers.....huum
First thing is WHAT is a driver basically...
Now to interact with the database(any database may be mysql,oracle) you need to have a driver so that this interaction can possibly be done!!!!!in other words the driver basically is responsible for the connection between your program and the dabtabase.....and that's the reason WHY we need driver.
Now when it comes to the types of driver there are basically FOUR types of drivers
- Type one Driver-This driver is also called as brige drive.As shown in the picture (forget about the JDBC api and stuff)what is noticeable is the the Database liberary API's.This type of driver basically needs the support from the Database library API.Not only library but also the ODBC driver is quite noticeable.Hence this type of driver basically needs a ODBC driver support(which can be provided by create a DSN).We will look at this supports in the jdbc connectivity.Because of these when ever you want to move your application from one computer to other you need to install these Database library and also you need to make sure that you have the DSN already been created with the same name as in the other system.From the java prospective this type of driver is basically also called as default driver as this comes with the jdk...i.e.you don't have to update the build path of the application.This is called as bridge driver as it acts as a bridge between the Calling Java Application and the ODBC.So a application has to through the JDBC,which interacts with the ODBC(through the DSN)which in turn interacts with the DataBase...
- Type Two Driver and Type Three Driver-Both these drivers have the DB Directory so these don't basically need the ODBC support....And also these drivers must be provided by the DB vendors that you are using.....These drivers are not at all in use any where including industry....So we will give more importance to the next driver...
- Type Four Driver-This basically is the widely used driver because of the features offered by this driver.The one you can see on the TOP.As you can see all you need is the type 4 driver,nothing else(no ODBC,no DSN and stuff).This type of drivers are basically provided by the DB vendors and you must "update the BuildPath" of the project by adding the external jar files.In case of Oracle you have to include the ojdbc14.jar.... Now to find this jar file you must go to the location where you have installed the Oracle,and in the directory D:\Oracle10GXE\app\oracle\product\10.2.0\server\jdbc\lib you will find the ojdbc14.jar.One very big advantage with the type 4 driver is that it can be used in any system where there is a Oracle database.No library and DSN of the same name all this is not reqiured(as in case of type 1 we need all this stuff).
Subscribe to:
Posts (Atom)