JDBC-JavaDatabaseConnectivity
Before going to JDBC lets know how database connections are made int the past. There are several api's to connect to database. For each database there is a particular API that should be known by the programmer to interact with Database. There will be different API's for different Databases. So one cant simple master one API and work with all Databases, in order to work with all databases one should have to learn all API's related to Databases. This is a big ask normally for any one and proved to be expensive thing.Then there comes the JDBC for the rescue, it is a single API that connected to any database with the help of driver provided by Database vendor. Here we can talk to any database if we know JDBC API unlike past learning different API's. In this part only the driver differs not the API. A driver is a set of calsses provided in form of jar file. Actually driver is the implntation of JDBC API. So if we have used Oracle in our application for sometime using JDBC api and suddenly we have to shift to mysql then there is no need to change the whole API it is fine if we just simply change the driver to mysql's.Thats the power of JDBC.
What is JDBC for and how is it used lets see.
- JDBC is used to connect to any database using java programming language.
- JDBC is an API which consists of several set of classes and interfaces written in java.
- JDBC allows to perform all operations on database from basic like insertion,deletion,updation to complex like executing stored procedures,transactions etc.
- JDBC grately reduced the burden of connecting to Database when compared to past.
- JDBC solved the problem of learning different API's.
Actually there are 4 types of drivers in JDBC. They are :
1.Type-1 JDBC-ODBC Bridge driver (least prefferable)
2.Type-2 Native API(stucks to particular machine)
3.Type-3 Net Pure Java
4.Type-4 Native Protocol
Oracle provides two drivers oci and thin of type 2 and type 4. We use thin driver in our examples.
Steps to connect to Database
- Import java.sql.* package
- Load the driver.
- Get the connection.
- Prepare the statement.
- Execute the statement.
- Get the result.
- Close the connection.
>>Load the driver : Class.forName(String classname);
It loads the driver into memory.
for oracle :
Class.forName("oracle.jdbc.driver.OracleDriver";
>>Get the connection : DriverManager.getConnection(String url,username,Password);
It connects to required database user.
for oracle :
for oracle :
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","xxxx","xxxx");
>>Prepare statement : PreparedStatement ps=con.prepareStatement(String sql);
It prepares the statement ready to be executed.
PreparedStatement ps=con.prepareStatement("insert into jobs values(a,b,c)");
>>Execute the satatement : ps.executeupdate();
It executes the statement and retuns no.of rows affected by the query.
int rows_affected=ps.executeUpdate();
>>Formatting output depends on your choice.
>>Close the connection and statements.ps.close(); con.close();Note: Class.forName() arises class not found exception and DriverManager.getConnection(), con.prepareStatement(),ps.executeUpdate(),close() arises sql exception so they should be thrown or caught by keeping in try catch block. NEXT>>


No comments :
Post a Comment