JDBC
JDBC
lOMoARcPSD|28883126
• The JDBC library includes APIs for each of the tasks mentioned below
that are commonly associated with database usage.
JDBC Architecture
• The JDBC API supports both two-tier and three-tier processing models for
database access but in general, JDBC Architecture consists of two layers:
• The JDBC driver manager ensures that the correct driver is used to access
each data source. The driver manager is capable of supporting multiple
concurrent drivers connected to multiple heterogeneous databases.
Types of Drivers
• In a Type 1 driver, a JDBC bridge is used to access ODBC drivers installed on each client
machine.
• Using ODBC, requires configuring on your system a Data Source Name (DSN) that
represents the target database.
• In a Type 2 driver, JDBC API calls are converted into native C/C++ API calls, which
are unique to the database.
• These drivers are typically provided by the database vendors and used in the same manner
as the JDBC-ODBC Bridge.
• The JDBC clients use standard network sockets to communicate with a middleware
application server.
• This kind of driver is extremely flexible, since it requires no code installed on the client
and a single driver can actually provide access to multiple databases.
• In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's
database through socket connection. This is the highest performance driver available for
the database.
lOMoARcPSD|28883126
• This kind of driver is extremely flexible, you don't need to install special software on
the client or server.
• This is for making the JDBC API classes immediately available to the application
program.
• Syntax
import java.sql.*;
• The forName() method of Class class is used to register the driver class.
• throws SQLException
lOMoARcPSD|28883126
• Statement stmt=con.createStatement();
• This method returns the object of ResultSet that can be used to get all the records of a
table.
• To process the result return from ResultSet object we use while loop with ResultSet
method next() as
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2));
• con.close(); lOMoARcPSD|28883126
• Example to close statement
• St.close();
PreparedStatement Interface
Method Description
public void setInt(int paramIndex, sets the integer value to the given
int value) parameter index.
public void setString(int sets the String value to the given
paramIndex, String value) parameter index.
public void setFloat(int sets the float value to the given
paramIndex, float value) parameter index.
public void setDouble(int sets the double value to the given
paramIndex, double value) parameter index.
public int executeUpdate() executes the query. It is used for create,
drop, insert, update, delete etc.
public ResultSet executeQuery() executes the select query. It returns an
instance of ResultSet.
lOMoARcPSD|28883126
import java.sql.*;
class InsertPrepared{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost/db1",
"root","root");
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
}
}
lOMoARcPSD|28883126
5.Write a short note on
a. ResultSet Object
b. ResultSetMetaData Object.
• Java ResultSetMetaData Interface
• The metadata means data about data i.e. we can get further information
from the data.
• Method • Description
import java.sql.*;
class Rsmd{
try{
Class.forName("com.mysql.jdbc.Driver ");
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
con.close();
lOMoARcPSD|28883126
ResultSet interface
• But we can make this object to move forward and backward direction by
passing either TYPE_SCROLL_INSENSITIVE or
TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well
as we can make this object as updatable by:
ResultSet.CONCUR_UPDATABLE);
1) public boolean next(): • is used to move the cursor to the one row
next from the current position.
3) public boolean first(): • is used to move the cursor to the first row
in result set object.
4) public boolean last(): • is used to move the cursor to the last row
in result set object.
lOMoARcPSD|28883126
columnName): column name of the current row as int.
import java.sql.*;
class FetchRecord{
Class.forName("com.mysql.jdbc.Driver ");
Connection con=DriverManager.getConnection(""jdbc:mysql://localhost/db
1","root","root");
Statement stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
E,ResultSet.CONCUR_UPDATABLE);
rs.absolute(3);
con.close();
}}