0% found this document useful (0 votes)
58 views7 pages

Following Steps Demonstrate How A Program Interacts With A Database

The document describes how a program interacts with a database in 4 steps: 1) setting up the database, 2) connecting to the database by loading the driver and creating a connection object, 3) creating and executing a JDBC statement to send SQL statements, and 4) retrieving data from the result set. It then provides examples of Java programs that access a database table to retrieve and display records, and use transactions to delete related records from multiple tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

Following Steps Demonstrate How A Program Interacts With A Database

The document describes how a program interacts with a database in 4 steps: 1) setting up the database, 2) connecting to the database by loading the driver and creating a connection object, 3) creating and executing a JDBC statement to send SQL statements, and 4) retrieving data from the result set. It then provides examples of Java programs that access a database table to retrieve and display records, and use transactions to delete related records from multiple tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Following steps demonstrate how a program interacts

with a database:
step1: setting up a database
step2: connecting to a database
to connect to the database the two main steps are
a) Loading the driver if you are using the JDBC-ODBC bridge driver,
use the following statement to load it.
class.forName( sun.jdbc.odbc.JdbcOdbeDriver)
b) Making the connection
The second step is to get the driver connected to the database, which is
done by creating a connection object and initializing it with connection
obtained from Driver Manager
Connection con =DriverManager.getConnection
(jdbc:odbc:datsource, log, pwd);
* The connection returned by the Method DriverManager.getConnection
is an open connection, which can be used to create jdbc statement that
pass, SQL statements to the database.
Step3: creating & Executing a jdbc statement. A statement object is used
to send the SQL statement to the database.
Statement st = con.createStatement();
rs = st.executeQuery(sql);
step 4: Retrieving data from the Resultsets

Program: write a java program to access to Access a database which has a


table named student whose columns are (ID) and name. Access all the
records in this table and display on the screen, both the ID and name
formatted properly
import java.sql.*;
class RetriveMeta
{
public static void main(String a [])
{
String sql = select*from student ;
String url = jdbc:odbc:stud;
try
{
class.forName(sun.jdbc.odbc.JdbcOdbcDriver)
Connection con = DriverManager.getconnection
(url, , );
Statement st= con.createStatement();
Resultset rs= st.executeQuery(sql);
displayAll(rs);
st.close();
con.close();
}
catch (SQLException e) {}
}
public static void displayAll(Resultset rs)
{
try
{
while(rs.next())
{
String n = rs.getString (ID)
String name = rs.getString (Name)
System.out.println(n + + name)
}
}
}
}

Program 2 using Transaction :


Consider Table
Dept (No, Name)
Emp(Empno, Name, deptno)
write a java program to del given deptno from dept, then all the entries of
Emp of deptno which deleted from dept table should also be deleted,
otherwise it will lead to inconsistency in the database
import java.sql.*;
public class DeleteTran
{
public static void main(String args[])
{
Connection con=null;
PreparedStatement deldeptno, delempdeptno;
try
{
Class.forName(sun.jdbc.odbc.JdbsOdbcDriver);
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
try
{
con=DriverManager(jdbc:odbc:emp, , );
String sql=Delete from Dept where No=?;
String sql2=Delete from Emp where DeptNo=?;
deldeptno=con.prepareStatement(sql);
delempdeptno=conprepareStatement(sql2);
con.setAutoCommit(false);
deldeptno.setInt(1,3);
delempdeptno.setInt(1,3);
deldeptno.executeUpdate();
delempdeptno.executeUpdate();
con.commit();
con.setAutoCommit(true);

deldeptno.close();
delempdeptno.close();
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
if(con!=null)
{
try
{
System.err.print(Transaction is being rolled
back);
con.rollback();
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}
}

Enhancements in JDBC 2.0 API


1) Java.sql package 2) javax.sql package In the previous version the
result set object could move the cursor to only the next row, but now
you can move you cursor backward, forward, one row or n rows
forward and backwards. They are also called as scrollable resultsets.
To create a scrollable resultset, you have to specify 2 parameters at the
time of statements creation.
*Statement st= con.create statement (
Resultset.TYPE_SCROLL_SENSITIVE,
Resultset_CONCUR_READ_ONLY);

The first argument is field constant of the Resultset class and it indicates
the type of a Resultset object.
1) TYPE_ FORWARD_ONLY - traversed in forward and can only use
next() method.
2) TYPE_SCROLL_INSENSITIVE - any changes made to the resultset
will not be updated online, i.e. you will have to close the resultset and
reopen it again to see the changes.
3) TYPE_SCROLLSESITIVE- any changes made to the resultset will
automatically be displayed.
The second argument:1)CONCUR_READ_ONLY- Indicates that the resultset can only be read
and not updated.
2) CONCUR_UPDATABLE- Indicates that the resultset can be updated.
Statement st =con createStatement (
Resultset.TYPE_SCROLL_INSENSITIVE
Resultset_CONCUR_READ_ONLY);
Resultset rs = st.executeQuery (select * from, dept);
rs.last();
while (rs.previous())
{
String name = rs.getString (Name);
System.out.println(name);
}
Methods:
next()
previons()
last().
absolute(H)- The method absolute will more the cursor to the row
number indicated in the argument passed to it.
rs.absolute(4);
* if negative values are used then the cursor move the given number from
the end. for rs.absolute(-1) takes it to the last row.getRow() gets the
number of the current row.

int n;
r.absolute(2);
n=rs.getRow()(n=2)
rs.relative(3):
n=rs.getRow(); //n=5/

Scrollable & Updatable Result Sets


Methods :
bollean absolute(int row)
-moves the cursor to the given row number in the result set.
void afterLast()
-moves the cursor to the end of the resultset, just before the last
row
void beforeFirst()
-moves the cursor to the first of the resultset, just before the
first row
void cancelRowUpdate()
-cancel the updates made to a row
void deleteRow()
-Deletes the current row from to the reultset
void first()
-moves the cursor to the first row in
int getRow()
-retrieves the current row nos
int insertRow()
-insert the content of the insert row into the resultset & the
database

isAfterLast(), isBeforeLast(),
rowInserted(), rowUpdated()

isFirst(),

isLast(),

rowSelected(),

void last()
-moves the cursor to the last row in the resultset
void next()
-moves the cursor down one row from its current position
void previous()
-moves the cursor to the previous row in the resutset.

You might also like