UNIT-III Final
UNIT-III Final
JDBC stands for Java Database Connectivity which allows developers to connect, query and
update a database using the Structured Query Language(SQL).
It is java API that defines interfaces and classes for database connection.
JDBC allows to send SQL to any RDBMS. Thus it allows database access by embedding SQL
statement with java code.
In short JDBC helps the programmers to write java applications that manage these three
programming activities:
Connect to a datasource, like a database.
Send queries and update statements to the database.
Retrieve and process the results received from the database in answer to your
query.
JDBC architecture
Que:
1. Write a short note on JDBC Architecture.
2. Write a brief note on Three-Tier Database Design.
3. Explain general architecture of JDBC.
4. Explain JDBC architecture.
Ans:
The JDBC API supports both two-tier and three-tier processing models for database access but
in general,:
JDBC Architecture consists of two layers −
JDBC API:
1. Client Tier:
2. Business Tier:
This tier contains the business logic like validation of data, calculations, data processing
etc. So, it provides the business services.
This acts as an interface between Client layer and Data Access Layer.
This layer is also called the intermediary layer helps to make communication faster between
client and data layer.
The benefit of having a centralized business tier is that same business logic can support
different types of clients.
3. Data Tier:
This layer is the external resource such as a database, ERP system, Mainframe system etc.
responsible for storing the data.
Advantages:
1. Enhanced scalability:
Disadvantages:
1. Increase Complexity/Effort
Que:
1. List advantages of using JDBC API?
2. Explain advantages and disadvantages of JDBC.
3. List out advantages of JDBC.
Ans:
Advantages
Disadvantage
DriverManager:
Connection:
Statement:
You use objects created from this interface to submit the SQL statements to the database.
Some derived interfaces accept parameters in addition to executing stored procedures.
A statement is created with Connection.createStatement().
ResultSet:
These objects hold data retrieved from a database after you execute an SQL query using
Statement objects.
It acts as an iterator to allow you to move through its data.
SQLException:
JDBC Drivers
Que:
1. List out types of JDBC drivers.
2. List JDBC Drivers. Explain JDBC-Net Pure Java Drivers (Type-3)
3. List JDBC Drivers. Explain JDBC-To-ODBC Bridge Driver (Type-1)
4. Discuss all types of JDBC Drivers.
5. List different categories of JDBC driver explain any one.
6. List out types of JDBC drivers. Explain any one type of driver in detail.
Ans:
JDBC Driver is a software component that enables java application to interact with the
database.
There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
Advantages:
Easy to use.
Can be easily connected to any database.
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.
Advantage:
Performance upgraded than JDBC-ODBC bridge driver.
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.
Advantage:
Better performance than all other drivers.
No software is required at client side or server side.
Disadvantage:
Drivers depend on the Database.
The forName() method of Class class is used to register the driver class. This method is used to
dynamically load the driver class.
Syntax of forName() method:
Example:
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
Example:
Que:
1.Write the steps to develop a JDBC application with proper example
Ans:
connecting java application with the mysql database, you need to follow 5 steps to perform
database connectivity.
In this example we are using MySql as the database. So we need to know following
informations for
1.Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is
running, we may also use IP address, 3306 is the port number and test is the database name.
We may use any database, in such case, you need to replace the test with your database name.
Password: Password is given by the user at the time of installing the mysql database.
In this example, we are going to use “ ”(blank)as the password.
Let's first create a table in the mysql database, but before creating table, we need to create
database first.
Example :
In this example, test is the database name, root is the username and password is blank.
import java.sql.*;
public class dbconnect {
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st=c.createStatement();
ResultSetrs=st.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}
catch(Exception e)
{
System.out.println(e);
}
}
The above example will fetch all the records of emp table
Que:
1. Explain in brief: JDBC Prepared Statement
Ans:
JDBC PreparedStatement can be used when you plan to use the same SQL statement many
times. It is used to handle precompiled query.
If we want to execute same query with different values for more than one time then
precompiled queries will reduce the number of compilations
As you can see, we are passing parameter (?) for the values. Its value will be set by calling the
setter methods of PreparedStatement.
Example:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","o
racle");
PreparedStatement stmt=con.prepareStatement("insert into Emp values(?,?)");
stmt.setInt(1,101); //1 specifies the first parameter in the query
stmt.setString(2,"Ratan");
Que:
1. Develop a JDBC application that uses any JDBC driver to fetch record. (Assume suitable
database)
Ans:
import java.sql.*;
public class dbconnect {
System.out.println("hello");
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st=c.createStatement();
ResultSetrs=st.executeQuery("select * from emp");
while(rs.next())
{
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
}
catch(Exception e)
{
System.out.println(e);
}
import java.sql.*;
public class dbconnectds {
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st=c.createStatement();
String str="insert into emp VALUES(30,'PR')";
st.executeUpdate(str);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
System.out.println("hello");
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/database","root","");
Statement st=c.createStatement();
String str="update emp set eid=40 where eid=30";
st.executeUpdate(str);
ResultSet rs=st.executeQuery("select * from emp");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Delete Record from Database
Que:
1. Develop a JDBC application that uses any JDBC driver to delete a record. (Assume suitable
database)
Ans:
import java.sql.*;
try
Class.forName("com.mysql.jdbc.Driver");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
Statement st=c.createStatement();
st.executeUpdate(str);
catch(Exception e)
System.out.println(e);
}}}
Questions (Asked in GTU):
1. CRUD operations
2. List four common JDBC exceptions.
3. Explain different types of ResultSet in JDBC.
4. Give full name : JDBC, AWT
5. Which package is used to perform almost all JDBC operations?
6. Which package is used for JDBC application.
7. Write a program using servlets to reading database and displaying them.
Type of ResultSet
The possible RSType are given below. If you do not specify any ResultSet type, you will
automatically get one that is TYPE_FORWARD_ONLY.
Type Description
ResultSet.TYPE_FORWARD_ONLY The cursor can only move forward in the result set.
ResultSet.TYPE_SCROLL_INSENSITIV The cursor can scroll forward and backward, and the
E result set is not sensitive to changes made by others
to the database that occur after the result set was
created.
ResultSet.TYPE_SCROLL_SENSITIVE The cursor can scroll forward and backward, and the
result set is sensitive to changes made by others to
the database that occur after the result set was
created.
java.sql.DataTruncation when a data values is unexpectedly truncated for reasons other than
its having exceeded MaxFieldSize.