Unit V - Interacting-With-Database
Unit V - Interacting-With-Database
Interacting with
Database
ODBC
ODBC stands for Open Database Connectivity
It is a standard Java API for connecting programs written in Java to the data in
relational databases.
JDBC works with Java on a variety of platforms, such as Windows, Mac OS, and
the various versions of UNIX.
Disadvantages:
Performance degraded because JDBC method call is converted into the
ODBC function calls.
The ODBC driver needs to be installed on the client machine.
Disadvantage:
The Native driver needs to be installed on the each client machine.
The Vendor client library needs to be installed on client machine.
Disadvantages:
Network support is required on client machine.
Requires database-specific coding to be done in the middle tier.
Maintenance of Network Protocol driver becomes costly because it requires
database-specific coding to be done in the middle tier.
Disadvantage:
Drivers depend on the Database.
DriverManager Class
Driver Interface
Connection Interface
Statement Interface
ResultSet Interface
It keeps track of the drivers that are available and handles establishing a
connection between a database and the appropriate driver.
Method Description
public static void registerDriver(Driver driver); is used to register the given driver
with DriverManager.
public static Connection getConnection(String url, String is used to establish the connection
userName, String password); with the specified url, username and
password.
public void rollback(); Drops all changes made since the previous
commit/rollback.
public boolean execute(String sql); used to execute queries that may return
multiple results.
public void setDouble(int paramIndex, double sets the double value to the given parameter
value) index.
public int executeUpdate() executes the query. It is used for create, drop,
insert, update, delete etc.
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.
is used to move the cursor to the last row in result set
public boolean last(); 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 getInt(String is used to return the data of specified column name of the
columnName); current row as int.
public String getString(int is used to return the data of specified column index of the
columnIndex); current row as String.
public String getString(String is used to return the data of specified column name of the
columnName); current row as String.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sun","root",“123
"); //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) {
System.out.println(e);
}
} }//C:\Program Files\Java\jre1.6.0\lib\ext
AJP- Unit-V Mrs.Chavan P.P. Department of Computer
35 Engineering
Prepared Statement Example
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:3306/sun","root",“123");
preparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)");
stmt.setInt(1,101); //1 specifies the first parameter in the query
stmt.setString(2,"Ratan"); //2 specifies the second parameter in the query
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
}catch(Exception e){ System.out.println(e);}
}
}