CHAPTER 05 JDBC-ODBC (1)final copy
CHAPTER 05 JDBC-ODBC (1)final copy
Prepared By,
Suwarna Thakre
Two tier architecture:
⚫In this type, there is another layer between the client and the server.
The client does not directly communicate with the server. Instead, it
interacts with an application server which further communicates with
the database system and then the query processing and transaction
management takes place. This intermediate layer acts as a medium
for exchange of partially processed data between server and client.
This type of architecture is used in case of large web applications.
Difference between two tier and three tier
It is a Client-Server Architecture. It is a Web-based application.
Two-tier architecture consists of two layers : Client Three-tier architecture consists of three layers : Client
Tier and Database (Data Tier). Layer, Business Layer and Data Layer.
It is less secured as client can communicate with It is secured as client is not allowed to communicate
database directly. with database directly.
⚫ Driver interface: Represent database driver. All JDBC driver classes must
implement the Driver interface
⚫SQLException class: Provide information about the exceptions that occur while
interacting with databases
Loading Driver
Class.forName(“<driver-name”);
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connecting to Database
<protocol>:<subprotocol>:<subname>
⚫Protocol name: Indicates the name of the protocol that is used to access a database In
JDBC ,the name of access protocol is always jdbc.
⚫Subprotocol name:-Indicates the mechanism to retrieve data from database for eg.if we
use JDBC-ODBC Bridge driver to access a database,then the name of subpritocol id odbc.
⚫Subname: Indicates Data Source Name(DSN) that contain the database information,such
as name of database,location of database,server,username and password to access a
database server.
getConnection() Method
⚫Connection getConnection(String
<url>,String<username>,String <password>)
⚫Accept the JDBC url of a database.It also accepts the user name
and password of the authorized database user.
Creating and Executing JDBC Statement
We need to create a Statement object to send requests to and retrieve
results from a database.The Connection object provides the
createStatement() method to create a Statement object.we can use following
syntax to create a Statement object.
Example:
String url=”jdbc:odbc:mydatasource”;
Connection con=DriverManager.getConnection(url);
Statement st=con.createStatement();
Continue….
⚫We can use SQL Statement to send requests to database.
The SQL statement that do not contain run time parameters
are called static SQL statements we can send SQL
statement to a database using the Statement object, The
statement interface contains following methods to send
static SQL statements to a database.
Statement interface method
⚫ResultSet executeQuery(String str):Execute SQL statement and
return a single object of the type ResultSet. This object provides method to
access the data from resultSet. The executeQuery() method should be use
when we need to retrieve datafrom database table using SELECT statement.
Statement st=con.createStatement();
Statement st=con.createStatement();
In the syntax,the executeUpdate() executes an SQL statement and number of rows affected in
database is stored in count
Statement interface method
⚫boolean execute(String str) :-Executes an SQL statement and returns
a Boolean value .we can use this method when SQL statement passed as
parameter is not known or when the statement being executed return a
result set or an update count. The execute() method returns true if the
result of the SQL statement is an object of ResultSet and false if it is an
update count
The syntax to use executeUpdate() method
Statement st=con.createStatement();
st.execute(<sql statement>);
write a program to create a
table using statement interface
import java.sql.*; String s="create table stud_record (age
class create12 int, add char)";
{ st.executeUpdate(s);
public static void main(String ar[]) System.out.println("table created");
{ con.close();
try st.close();
{ }
Class.forName("sun.jdbc.odbc.Jdbc catch(Exception e)
OdbcDriver"); {}
System.out.println("driver loaded"); }}
String url="jdbc:odbc:even";
Connection con=
DriverManager.getConnection(url);
System.out.println("connection
created");
Statement
st=con.createStatement();
Wap to insert data into table
using statement interface
import java.sql.*; Statement st=con.createStatement();
class data_create String str="insert into IF4G
{ values(112,'suw')";
public static void main(String ar[]) st.executeUpdate(str);
{ System.out.println("data inserted"+i1);
try st.close();
{ con.close();
Class.forName("sun.jdbc.odbc.JdbcOdbcDri }
ver"); catch(Exception e)
System.out.println("driver loaded"); {
String url="jdbc:odbc:god"; }
Connection }
con=DriverManager.getConnection(url);
System.out.println("connection created");
Retrieve data from table
boolean isFirst() Determine whether the result set cursor points to the first row of the result
set
boolean beforeFirst() Shift the control of result set cursor before the first row of the result set
boolean isBeforeFirst() Determine whether the result set cursor points before the first row of the
result set
boolean last() Shift the control of result set cursor to the last row of the result set
boolean isLast() Determine whether the result set cursor points to the last row of the result
set
boolean afterLast() Shift the control of result set cursor after the last row of the result set
boolean isAfterLast() Determine whether the result set cursor points to the first row of the result
set
boolean previous() Shift the control of result set cursor to the previous row of the result set
boolean absolute(int i) Shift the control of result set cursor to the row number that we specify as a
parameter.
PreparedStatement interface
run time
Example:
PreparedStatement st=con.prepareStatement(url);
St.setString(1,”1001”);
ResultSet rs=st.executeQuery()
Void setByte(int index,byte value) Set the java byte type value for the parameter
Corresponding to index passed as a parameter
Void setBytes(int index,byte[] value) Set the java byte type array for the parameter
Corresponding to index passed as a parameter
Void setBoolean(int index, boolean val) Set the java Boolean type value for the parameter
Corresponding to index passed as a parameter
Void setDouble(int index, double val) Set the java double type value for the parameter
Corresponding to index passed as a parameter
Void setInt(int index, int val) Set the java int type value for the parameter
Corresponding to index passed as a parameter
Void setLong(int index, long val) Set the java long type value for the parameter
Corresponding to index passed as a parameter
Void setFloat(int index, float val) Set the java float type value for the parameter
Corresponding to index passed as a parameter
Void setShort(int index, short val) Set the java short type value for the parameter
Corresponding to index passed as a parameter
Void setString(int index, String val) Set the java String type value for the parameter
Select Data From Table Using ps
import java.sql.*;
class select1_ps ResultSet rs=st.executeQuery();
while(rs.next())
{public static void main(String ar[])
{
{try System.out.println("bookid"+rs.getInt(1));
{Class.forName("sun.jdbc.odbc.JdbcOdbcDri System.out.println("bookname”+rs.getString
ver"); (2));
System.out.println("driver loaded"); }
st.close();
String url="jdbc:odbc:god1";
con.close();
Connection }
con=DriverManager.getConnection(url); catch(Exception e)
bookid bookname
System.out.println("connection created"); {
11 PIC
String str="select * from author "; }}} 12 JAVA
PreparedStatement 13 PHP
st=con.prepareStatement(str);
Update table using prepared
statement interface
import java.sql.*;
String str="update author set
class update123 name=‘JAVA' where bookid=?";
{ PreparedStatement
public static void main(String ar[]) ps=con.prepareStatement(str);
{ ps.setInt(1,12);
try ps.executeUpdate();
{
Class.forName("sun.jdbc.odbc.JdbcO con.close();
dbcDriver"); st.close();
System.out.println("driver loaded"); }
String url="jdbc:odbc:dsn"; catch(Exception e) bookid bookname
Connection {
con=DriverManager.getConnection(
url); }}} 11 PIC
12 JAVA
System.out.println("connection
created"); 13 PHP
Delete row from table using prepared statement
interface
PreparedStatement
import java.sql.*; st=con.prepareStatement(str);
class update1_ps
{ int bookid=111;
public static void main(String ar[]) st.setInt(1,bookid);
{ st.executeUpdate();
try System.out.println(“row deleted");
{ st.close();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con.close();
System.out.println("driver loaded");
String url="jdbc:odbc:god1"; }
Connection catch(Exception e)
con=DriverManager.getConnection(url); {
System.out.println("connection created"); }}}
String str=“delete from author where bookid=?”
Insert data into table using ps
int bookid=111;
import java.sql.*; st.setInt(1,bookid);
class ps_insert st.setString(2,"sss");
{ st.executeUpdate();
public static void main(String ar[]) System.out.println("data inserted");
{try st.close();
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") con.close();
; }catch(Exception e)
System.out.println("driver loaded"); {}}}
String url="jdbc:odbc:god1";
Connection
con=DriverManager.getConnection(url);
System.out.println("connection created");
String str="insert into studentty values (?,?)";
PreparedStatement
st=con.prepareStatement(str);