Interacting With Databse
Interacting With Databse
2.Establish Connection –
A connection to the database is established using the static method
getConnection(databaseUrl) of the DriverManager class.It is the class
for managing JDBC drivers.
The database URL takes the following shape
jdbc:subprotocol:subname.
If any problem occurs during accessing the database ,an
SQLexception is generated,else a connection object is returned which
refers to a connection to a database.
Connection is actually an interface in java.sql package
Connection con =DriverManager.getConnection(databaseURL)
3.Create statement:
The connection after being established is used to send SQL
statements to the database .There are three interfaces in java.sql
package used for sending SQL statements to database ,namely –
Statement and its two subinterfaces-
PreparedStatement and callablestatement.
Three methods of the Connection object are used to return objects of
theses three statements.
A Statement object is used to send a simple SQL statement to the
database with no parameters .Its object are returned by using
createStatement() of the connection object.
4.Execute Query:
The SQL statements are executed with the help of three methods
provided by the Statement interface.
ResultSet executeQuery(String sqlQuery ) throws SQLException
Int executeUpdate(String sqlQuery) throws SQLException
Boolean execute(String sqlQuery) throws SQLException
ps.setInt(1,10000);
ps.setString(2,”Emp001”);
ps.executeUpdate();
Now ,how do we know what is the type of data in the first /second
columns so that the appropriate methods can be used , and how
many columns are returned in the ResultSet?
All the these details can be obtained using a ResultSetMetaData
object. This object is used to obtain metadata about ResultSet that
include number of columns ,types of columns etc.
The method getMetaData() is used to return the ResultSetMetaData
Object.
The method getColumnCount() is used to return the number of
columns in the ResultSet.
The method getColumnTypeName(int columnIndex) returns the type
of data the column holds.
Output:Row inserted:1
Number of columns in result set: 3
EmpId Name salary
Emp001 Peter 10000
Scrollable ResultSet:
The ResultSet ,before JDBC 2.1 could be scrolled in the forward
direction only.Jdbc 2.1 introduced the concept of moving the cursor
in the backward direction also. We can even position the cursor of
ResultSet object on specific row .
But in order to use scrollable result set methods ,the scrollable
resultset must be obtained .this is specified at the statement creation
time by passing the following ResultSet Types in the
createStatement() method.
Following are the methods of ResultSet interface used for scrolling
through it.
first() Moves the cursor to the first row
of the ResultSet.
last() Moves the cursor to the last row
of the ResultSet
previous() Moves the cursor to the previous
row of the ResultSet
absolute(int row) Moves the cursor to the
specified row of the ResultSet
relative(int row) Moves the cursor to the relative
to the current row of the
ResultSet.A negative value can
be specified to move backward
and positive value to move
forward.
getRow() Returns current row number.