0% found this document useful (0 votes)
15 views

Unit V Ajava - Update

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit V Ajava - Update

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

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 java.sql 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 sun.jdbc.odbc 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 Class.forName()

Class.forName(“oracle.jdbc.driver.OracleDriver”);
2. 2-B DriverManager.registerDriver()
DriverManager.registerDriver(new
oracle.jdbc.driver.OracleDriver())
• Step 3: Establish a connection using the Connection class object
Connection con =
DriverManager.getConnection(url,user,password)

Step 4: Create a statement


Statement st = con.createStatement();
Step 5: Execute the query
import java.io.*;
import java.sql.*;

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
Class.forName(
"com.mysql.cj.jdbc.Driver"); // Driver name
Connection con = DriverManager.getConnection(
url, username, password);
System.out.println(
"Connection Established successfully");
Statement st = con.createStatement();
ResultSet rs
= st.executeQuery(query); // Execute query
rs.next();
String name
= rs.getString("name"); // Retrieve name from
db

System.out.println(name); // Print result on


console
st.close(); // close statement
con.close(); // close connection
System.out.println("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
1.Better performance: because it creates a thread for each
request, not process.
2.Portability: because it uses Java language.
3.Robust: JVM manages Servlets, so we don't need to worry about the
memory leak, garbage collection, etc.
4.Secure: 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 javax.servlet.Servlet 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
1.public void init(ServletConfig config)
2.public abstract void service(ServletRequest request,
ServletResponse response)
3.public void destroy()
4.public ServletConfig getServletConfig()
5.public String getServletInfo()
6.public void init()
import java.io.*;
import javax.servlet.*;
public class First extends GenericServlet{
public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.print("<html><body>");
out.print("<b>hello generic servlet</b>");
out.print("</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:
1.javax.servlet.jsp
2.javax.servlet.jsp.tagext
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two
interfaces are as follows:
3.JspPage
4.HttpJspPage
JSP Scripting elements
• scriptlet tag
• expression tag
• declaration tag
File: index.html
1.<html>
2.<body>
3.<form action="welcome.jsp">
4.<input type="text" name="uname">
5.<input type="submit" value="go"><br/>
6.</form>
7.</body>
8.</html>
File: welcome.jsp
1.<html>
2.<body>
3.<%
4.String name=request.getParameter("uname");
5.out.print("welcome "+name);
6.%>
7.</form>
8.</body>
9.</html>

You might also like