Interacting With Database
Interacting With Database
• Permanent Storage
• Databases can store very large numbers of records
efficiently (they take up little space).
• It is very quick and easy to find information .
• It is easy to add new data and to edit or delete old data.
ODBC
▰ ODBC is procedural
▰ Platform dependent
Drawbacks:
▰ Not suitable for high transaction environment
▰ Complete java command set is not supported by Type 1 driver
Type-2 Driver: Native API driver, Part-Java
driver
▰ The JDBC type 2 driver, also known as the 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.
When to use
Drawbacks:
▰ Type 2 drivers don’t use the full Java API
▰ Platform specific
▰ Developers require to perform additional configuration
Type-3 Driver: JDBC-Net pure Java Driver
▰ The JDBC type 3 driver, also known as the Pure Java driver for
database middleware. It is a database driver implementation
which makes use of a middle tier between the calling program and
the database.
▰ The middle-tier (application server) converts JDBC calls directly or
indirectly into a vendor-specific database protocol. It is fully
written in java.
When to use
▰ java.sql.*;
▰ The JDBC API provides the following interfaces and classes −
▰ DriverManager Class
▰ Driver Interface
▰ Connection Interface
▰ Statement Interface
▰ ResultSet Interface
Common JDBC Components
DriverManager Class
▰ Driver Interface
This interface handles the communications with the database
server. You will very rarely interact directly with Driver
objects. Instead, you use DriverManager objects, which
manages objects of this type. It also abstracts the details
associated with working with Driver objects.
Common JDBC Components (cont’d..)
▰ Connection Interface
A Connection is the session between java application and
database. The Connection interface is a factory of Statement,
PreparedStatement, and DatabaseMetaData. The Connection
interface provide many methods for transaction management
like commit(),rollback() etc.
Commonly used methods of Connection
interface
Method Description
▰ Statement Interface
The Statement interface provides methods to execute queries
with the database. It provides factory method to get the
object of ResultSet.
Commonly used methods of Statement interface
Method Description
public boolean execute(String sql); used to execute queries that may return
multiple results.
▰ ResultSet Interface
The object of ResultSet maintains a cursor pointing to a
particular row of data. Initially, cursor points to before the first
row.
“Select * from Table where Percentage>70”
Table: ResultSet
Roll Name Percentage
No Roll Name Percentage
No
1 abc 70
2 efg 75
2 efg 75
3 pqr 82
3 pqr 82
4 mnp 50
5 uvw 67
Commonly used methods of ResultSet interface
Method Description
public boolean next(); is used to move the cursor to the one row next
from the current position.
public boolean previous(); is used to move the cursor to the one row previous
from the current position.
public boolean first(); is used to move the cursor to the first row in result
set object.
public boolean last(); is used to move the cursor to the last row in result
set object.
public boolean absolute(int row); is used to move the cursor to the specified row
number in the ResultSet object.
public int getInt(int columnIndex); is used to return the data of specified column index
of the current row as int.
public int getString(String columnName); is used to return the data of specified column name
of the current row as int.
public String getString(int columnIndex); is used to return the data of specified column index
of the current row as String.
public String getString(String is used to return the data of specified column name
columnName); of the current row as String.
Connecting to Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
2. Creating connection
▰ The DriverManager .getConnection() method is used to establish
connection with the database.
con.close();
Example to Connect Java
Application with database
import java.sql.*;
class MysqlCon{
public static void main(String args[])
{
Try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sun","root","root");
//here sun is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}
catch(Exception e)
{
▰ DSN
▰ Database
Java Application Table