Chapter 4 Interacting With Database
Chapter 4 Interacting With Database
Advantages:
◦ It is simple in design.
◦ Client-side scripting offloads work onto the client
Drawbacks:
◦ Fat client.
◦ It is inflexible.
Although the two-tiered architecture is
common, another design is starting to appear
more frequently.
Drawbacks:
◦ Higher complexity
◦ Higher maintenance
◦ Lower network efficiency
◦ More parts to configure (and buy)
What is JDBC?
The JDBC stands for Java Database
Connectivity.
4. Execute a query
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connect to the DBMS
After loading the driver the application must
get connected to DBMS.
he DriverManager.getConncetion() method is
passed the URL of the database and user ID and
password required by the database.
The URL is the string object that contains the
driver name that is being accessed by the Java
program.
<protocol>:<subprotocol>:<dsn-name>
The ‘protocol’ is a JDBC protocol that is
used to read the URL.
jdbc:odbc:customer
here, ‘customer’ is an example of DSN name
given to our database.
For example:
For example:
Statement st = con.createStatement();
Execute the query
The executeQuery() method of Statement
object is used execute and process the query
which returns the ResultSet object.
For example:
For example:
con.close();
For example:
Signature is as follows:
For example:
if(st.execute())
rs = st.getResultSet();
PreparedStatement object
A SQL query must be compiled before the DBMS processes the query.
However, a question mark is given on the place for the value that is
inserted into the query after it is compiled. It is the value that changes
each time the query is executed.
For doing this process, we need to construct the query
with question
marks such as,
For example:
PreparedStatement ps = prepareStatement(query);
Once the PreparedStatement object is obtained, the setXxx( )
methods of it can be used to replace question mark with the value
passed to setXxx() method.
ps.setInt(1, 100000);
Such as,
ResultSet rs = ps.executeQuery();
Disadvantage:
◦ The Native driver needs to be installed on the
each client machine.
◦ The Vendor client library needs to be installed on
client machine.
3) Network Protocol driver
The Network Protocol driver uses middleware (application
server) that converts JDBC calls directly or indirectly into
the vendor-specific database protocol.
Disadvantage:
◦ Drivers depends on the Database.
5 Steps to connect to the
database in java
There are 5 steps to connect any java
application with the database in java using
JDBC. They are as follows:
Statement stmt=con.createStatement();
4) Execute the query
The executeQuery() method of Statement
interface is used to execute queries to the
database.
2) public static void is used to deregister the given driver (drop the
deregisterDriver(Driver driver): driver from the list) with DriverManager.
3) public boolean first(): is used to move the cursor to the first row in result set object.
4) public boolean last(): is used to move the cursor to the last row in result set object.
public void setInt(int paramIndex, int sets the integer value to the given
value) parameter index.
public void setString(int paramIndex, sets the String value to the given
String value) parameter index.
public void setFloat(int paramIndex, sets the float value to the given parameter
float value) index.
stmt.setString(1,"Sonoo");
//1 specifies the first parameter in the query
i.e. name stmt.setInt(2,101);
int i=stmt.executeUpdate();
System.out.println(i+" records updated");
PreparedStatement interface that deletes the
record
PreparedStatement stmt=con.prepareState
ment("delete from emp where id=?");
stmt.setInt(1,101);
int i=stmt.executeUpdate();