HTML Form, JSP and Database

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 −

  1. DriverManager: This class matches connection requests from the java application with the database driver and establishes a database Connection.
  2. Driver: This interface handles the communications with the database server.
  3. Connection: This interface has all methods to connect to the database and execute SQL queries
  4. Statement: interface to create SQL statements. Statement types:
    1. Static SQL statements (Statement interface),
    2. precompiled SQL statements (PreparedStatement interface), and
    3. stored SQL procedures (CallableStatement interface)
  5. ResultSet: These objects hold data retrieved from a database after executing an SQL query using Statement objects.
  6. 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
  1. Create the HTML form to receive the required data
  2. On submit, send data to appropriate JSP page in server
Server JSP Page
  1. Receive the data from HTML form
  2. Construct the SQL Query
  3. Establish connection to the database and execute the query
  4. Retrieve result from the database.
  5. Close database connection.
  6. Create the HTML response page and send to client browser
Client Browser
  1. 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