JDBC Notes
JDBC Notes
Java JDBC is a java API to connect and execute query with the database. JDBC API uses
jdbc drivers to connect with the database.
1. JDBC stands for Java Data Base Connectivity Where as ODBC stands for Open Data
Base Connectivity.
2. Before JDBC, ODBC API was the database API to connect and execute query with
the database. But, ODBC API uses ODBC driver which is written in C language (i.e.
platform dependent and unsecured). That is why Java has defined its own API (JDBC
API) that uses JDBC drivers (written in Java language).
3. JDBC drivers are written in java and JDBC code is automatically installable, secure,
and portable on all platforms. ODBC requires manual installation of the ODBC driver
manager and driver on all client machines.
What is API
API (Application programming interface) is a document that contains description of all the
features of a product or software. It represents classes and interfaces that software programs
can follow to communicate with each other. An API can be created for applications, libraries,
operating systems, etc
JDBC Driver
JDBC Driver is a software component that enables java application to interact with the
database. OR
The JDBC driver is the mediator between the Java application and the database.
There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The JDBC-
ODBC bridge driver converts JDBC method calls into the ODBC function calls. This is
now discouraged because of thin driver.
Advantages:
o easy to use.
o can be easily connected to any database.
Disadvantages:
o Performance degraded because JDBC method call is converted into the ODBC
function calls.
o The ODBC driver needs to be installed on the client machine.
2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver converts
JDBC method calls into native calls of the database API. It is not written entirely in java.
Advantage:
Disadvantage:
The Network Protocol driver uses middleware (application server) that converts JDBC calls
directly or indirectly into the vendor-specific database protocol. It is fully written in java.
Advantage:
o No client side library is required because of application server that can perform many
tasks like auditing, load balancing, logging etc.
Disadvantages:
The thin driver converts JDBC calls directly into the vendor-specific database protocol.
That is why it is known as thin driver. It is fully written in Java language.
Advantage:
Disadvantage:
1.Establishing a Connection
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
NOTE:Example
Connection c =DriverManager.getConnection("jdbc:odbc:mydsn","bvpr","cs");
Connecting through DSN :
Connection con =
DriverManager.getConnection(“jdbc:odbc:dsnname”);
Connection con =
Connection con =
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@ipaddress:port:hoststring”, “username”,
“password”)
JDBC Statements
Types of Statements:
1. Statement createStatement()
2. PreparedStatement prepareStatement(String sql)
1. stmt.executeUpdate()
2. stmt.executeQuery()
3. stmt.execute()
Statement Methods:Decription
3. boolean execute(String)
2. Prepared Statement: Used to prepare statements with place holders (?) to set the
values at run time.
PreparedStatement: - is used when an application plans to reuse a statement multiple times. The
application prepares the SQL it plans to use. Once prepared, the application can specify values for
parameters in the prepared SQL statement.
Example:
ps.setInt(int, int);
ps.setString(int, String);
……..
ps.setXXX(int, XXX);
ResultSet rs=ps.executeQuery();
int n=ps.executeUpdate();
boolean b=ps.execute();
A CallableStatement is used to call stored procedures that return values. It also has methods for
retrieving the return values of the stored procedure.
Example:
JDBC Exceptions:
1.
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
2.
catch (SQLException e)
{
e.printStackTrace();
}
Note::Querying the Database
The ResultSet object contains the rows retrieved from the database query.
To access each row use ResultSets’s next() method.
This method moves a database cursor sequentially through the rows retrieved from
the query
ResultSet
ResultSet Methods
1. boolean next()
a. activates the next row
b. the first call to next() activates the first row
c. returns false if there are no more rows
2. void close()
a. disposes of the ResultSet
b. allows you to re-use the Statement that created it
c. automatically called by most Statement methods
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
String s = rs.getInt("id");
float n = rs.getString("name");
ResultSetMetaData
ResultSetMetaData: It is Data about Data of ResultSet like field names, no. of Columns etc.
Methods:
DatabaseMetaData
import java.sql.*;
import java.io.*;
class dbscon
{
public static void main(String s1[])
{
ResultSet r=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydn","username","pwd");
Statement st=c.createStatement();
while(r.next())
String cc=r.getString("empno");
System.out.println(cc);
catch(Exception e)
{
System.out.println(e);
}
}
}
// DATABASE conn Update
import java.sql.*;
import java.io.*;
class dbconupdt
ResultSet r=null;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Statement st=c.createStatement();
System.out.println(n);
catch(Exception e)
System.out.println(e);
}
//
import java.sql.*;
import java.io.*;
class dbconins
ResultSet r=null;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydn","uname",”pwd”);
Statement st=c.createStatement();
String id=br.readLine();
String nm=br.readLine();
int n=st.executeUpdate(qr);
System.out.println(n);
catch(Exception e)
{
System.out.println(e);
import java.sql.*;
import java.io.*;
class dbcondel
ResultSet r=null;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydn","username","pwd"");
Statement st=c.createStatement();
System.out.println(n);
catch(Exception e)
System.out.println(e);}
}
}
import java.sql.*;
import java.io.*;
class metdt
ResultSet r=null;
try
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydn","username","pwd");
Statement st=c.createStatement();
ResultSetMetaData mt=r.getMetaData();
int cl=mt.getColumnCount();
for(int i=1;i<=(cl-2);i++)
System.out.print(mt.getColumnName(i)+" ");
System.out.println();
while(r.next())
for(int i=1;i<=(cl-2);i++)
{
System.out.print(r.getString(i)+" ");}
System.out.println();
r.close();st.close();c.close();
catch(Exception e)
System.out.println(e);