Following Steps Demonstrate How A Program Interacts With A Database
Following Steps Demonstrate How A Program Interacts With A Database
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
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);
}
}
}
}
}
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/
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.