JSP AND JDBC
JDBC stands for Java Database Connectivity, which is a standard Java API for database connectivity between the Java programming language and a wide range of databases. The JDBC library includes APIs for
- Making a connection to a database.
- Creating query statements.
- Executing queries in the database.
- Viewing & Modifying the resulting records.
The JDBC API provides the following interfaces and classes −
- DriverManager: This class matches connection requests from the java application with the database driver and establishes a database Connection.
- Driver: This interface handles the communications with the database server.
- Connection: This interface has all methods to connect to the database and execute SQL queries
- Statement: interface to create SQL statements. Statement types:
- Static SQL statements (Statement interface),
- precompiled SQL statements (PreparedStatement interface), and
- stored SQL procedures (CallableStatement interface)
- ResultSet: These objects hold data retrieved from a database after executing an SQL query using Statement objects.
- SQLException: This class handles any errors that occur in a database application.
Working of JDBC
HTML, JSP AND JDBC
Steps involved in creating an application to submit information to a JSP page through form, accessing a database and returning the result to client browser as a HTML page.
Steps:
Client Browser
- Create the HTML form to receive the required data
- On submit, send data to appropriate JSP page in server
Server JSP Page
- Receive the data from HTML form
- Construct the SQL Query
- Establish connection to the database and execute the query
- Retrieve result from the database.
- Close database connection.
- Create the HTML response page and send to client browser
Client Browser
- Display the HTML page received from the server.
Create HTML Form to submit data
<!– Simple program to demonstrate JSP and JDBC access –>
<!– Form to submit Department name to JSP page in server –>
<!– html forms –>
<html>
<head>
<title>HTML Forms, JSP and JDBC</title>
<style>
label
{
font-family:vivaldi;
font-size:x-large;
letter-spacing:2px;
line-height:40px;
}
</style>
</head>
<body>
<form method=”GET” action=”JSP_JDBC.jsp”>
<center>
<h2>HTML Forms, JSP and JDBC</h2><br/>
<label>Enter Department Name </label>
<input type=”text” Name=”DN” tabindex=1/>
<br/> <br/>
<input type=”submit” value=”Get Code!!!”/>
</center>
</form>
</body>
</html>
// JSP page
// Receive form data, access database and generate response to client
// JDBC – Database name: XEPDB1
// Table: DC (name varchar(20), code varchar(20))
<!– JSP and JDBC –>
<html>
<body>
<br/><center><h2>
<%@ taglib uri = “http://java.sun.com/jsp/jstl/sql” prefix=”sql” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
<%@page import=”java.io.*”%>
<%@page import=”java.util.*”%>
<%@page import=”java.lang.*”%>
<%@page import=”java.sql.*”%>
<%
String dn = request.getParameter(“DN”).trim();
String qry = “SELECT * FROM DC WHERE name ='”+dn+”‘”;
Connection conn = DriverManager.getConnection(“jdbc:oracle:thin:@//localhost:1521/XEPDB1”, “system”, “Harini97#”);
PreparedStatement ps = conn.prepareStatement(qry);
ResultSet rs = ps.executeQuery();
while(rs.next())
{
%>
Department Name : <%=rs.getString(1)%> <br/>
Department Code : <%=rs.getString(2)%>
<%
}
%>
</h2>
</center>
</body>
</html>
OUTPUT