0% found this document useful (0 votes)
36 views29 pages

Unit V Ajava - Update

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views29 pages

Unit V Ajava - Update

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Unit V

JDBC (Java database Connectivity)


Introduction
JDBC stands for Java Database Connectivity. JDBC is a Java API to
connect and execute the query with the database. It is a specification
from Sun Microsystems that provides a standard abstraction(API or
Protocol) for Java applications to communicate with various databases.
It provides the language with Java database connectivity standards.
The current version of JDBC is JDBC 4.3, released on 21st September
2017.
Components of JDBC
There are generally four main components of JDBC.
1. JDBC API: It provides various methods and interfaces for easy
communication with the database. It provides two packages as follows,
which contain the java SE(standard edition) and Java EE(enterprise
edition) platforms to exhibit WORA(write once run anywhere)
capabilities. The [Link] package contains interfaces and classes of
JDBC API.
• 2. JDBC Driver manager: It loads a database-specific driver in an
application to establish a connection with a database. It is used to
make a database-specific call to the database to process the user
request.

• 3. JDBC Test suite: It is used to test the operation(such as insertion,


deletion, updation) being performed by JDBC Drivers.

• 4. JDBC-ODBC Bridge Drivers: It connects database drivers to the


database. This bridge translates the JDBC method call to the ODBC
(Oracle Database Connectivity ) function call. It makes use of
the [Link] package which includes a native library to access
ODBC characteristics.
Types of JDBC Architecture(2-tier and 3-
tier)
1. Two-tier model: A java application communicates directly to the
data source. The JDBC driver enables the communication between
the application and the data source. When a user sends a query to the
data source, the answers for those queries are sent back to the user in
the form of results.
2. Three-tier model: In this, the user’s queries are sent to middle-tier
services, from which the commands are again sent to the data
source. The results are sent back to the middle tier, and from there to
the user.
This type of model is found very useful by management information
system directors.
Steps to Connect Java Application with
Database

Step 1 – Import the Packages


Step 2 – Load the drivers using the forName() method
Step 3 – Register the drivers using DriverManager
Step 4 – Establish a connection using the Connection class object
Step 5 – Create a statement
Step 6 – Execute the query
Step 7 – Close the connections
Java Database Connectivity
• Step 1: Import the Packages
• Step 2: Loading the drivers
1. 2-A [Link]()

[Link](“[Link]”);
2. 2-B [Link]()
[Link](new
[Link]())
• Step 3: Establish a connection using the Connection class object
Connection con =
[Link](url,user,password)

Step 4: Create a statement


Statement st = [Link]();
Step 5: Execute the query
import [Link].*;
import [Link].*;

class XYZ {
public static void main(String[] args) throws Exception
{
String url
= "jdbc:mysql://localhost:3306/table_name"; // table details
String username = "root"; // MySQL credentials
String password = "123";
String query
= "select *from students"; // query to be run
[Link](
"[Link]"); // Driver name
Connection con = [Link](
url, username, password);
[Link](
"Connection Established successfully");
Statement st = [Link]();
ResultSet rs
= [Link](query); // Execute query
[Link]();
String name
= [Link]("name"); // Retrieve name from
db

[Link](name); // Print result on


console
[Link](); // close statement
[Link](); // close connection
[Link]("Connection Closed....");
}
}
Types of Statements in JDBC

• Create Statement
• Prepared Statement
• Callable Statement
.

Servlets
• Servlet technology is used to create a web application (resides at
server side and generates a dynamic web page).
• Servlet technology is robust and scalable because of java language.
Before Servlet, CGI (Common Gateway Interface) scripting language
was common as a server-side programming language.
There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc
• Servlet is a technology which is used to create a web application.
• Servlet is an API that provides many interfaces and classes including
documentation.
• Servlet is an interface that must be implemented for creating any
Servlet.
• Servlet is a class that extends the capabilities of the servers and
responds to the incoming requests. It can respond to any requests.
• Servlet is a web component that is deployed on the server to create a
dynamic web page.
Advantages of Servlet
[Link] performance: because it creates a thread for each
request, not process.
[Link]: because it uses Java language.
[Link]: JVM manages Servlets, so we don't need to worry about the
memory leak, garbage collection, etc.
[Link]: because it uses java language.
Life Cycle of a Servlet
The entire life cycle of a Servlet is managed by the Servlet
container which uses the [Link] interface to understand
the Servlet object and manage it.
Stages of the Servlet Life Cycle:
• Loading a Servlet.
• Initializing the Servlet.
• Request handling.
• Destroying the Servlet.
Servlet Life Cycle Methods
There are three life cycle methods of a Servlet :
• init()
• service()
• destroy()
GenericServlet class
• GenericServlet class
implements Servlet, ServletConfig and Serializable interfaces. It
provides the implementation of all the methods of these interfaces
except the service method.
• GenericServlet class can handle any type of request so it is protocol-
independent.
Methods of GenericServlet class
[Link] void init(ServletConfig config)
[Link] abstract void service(ServletRequest request,
ServletResponse response)
[Link] void destroy()
[Link] ServletConfig getServletConfig()
[Link] String getServletInfo()
[Link] void init()
import [Link].*;
import [Link].*;
public class First extends GenericServlet{
public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{

[Link]("text/html");

PrintWriter out=[Link]();
[Link]("<html><body>");
[Link]("<b>hello generic servlet</b>");
[Link]("</body></html>");
}
}
Difference between Servlet and JSP
Servlet JSP

Servlet is a java code. JSP is a HTML-based compilation code.

Writing code for servlet is harder than JSP as it is HTML in java. JSP is easy to code as it is java in HTML.

Servlet plays a controller role in the ,MVC approach. JSP is the view in the MVC approach for showing output.

Servlet is faster than JSP. JSP is slower than Servlet because the first step in the JSP lifecycle is the
translation of JSP to java code and then compile.

Servlet can accept all protocol requests. JSP only accepts HTTP requests.

In Servlet, we can override the service() method. In JSP, we cannot override its service() method.

In Servlet by default session management is not enabled, user have to enable it In JSP session management is automatically enabled.
explicitly.
The Lifecycle of a JSP Page
The JSP API
• The JSP API consists of two packages:
[Link]
[Link]
[Link] package
The [Link] package has two interfaces and [Link] two
interfaces are as follows:
[Link]
[Link]
JSP Scripting elements
• scriptlet tag
• expression tag
• declaration tag
File: [Link]
1.<html>
2.<body>
3.<form action="[Link]">
4.<input type="text" name="uname">
5.<input type="submit" value="go"><br/>
6.</form>
7.</body>
8.</html>
File: [Link]
1.<html>
2.<body>
3.<%
[Link] name=[Link]("uname");
[Link]("welcome "+name);
6.%>
7.</form>
8.</body>
9.</html>

You might also like