How to implement jdbc in JSP?

 

Now we will see the implementation of jdbc in JSP. This is very one of the important point for project development. So I hope the following example will give a better clarity.
Required files:

  • attendance.jsp
  • attendanceprocess.jsp
  • welcome.jsp
  • web.xml

 

let we have Oracle10gXE database and we are using Type-4(Thin) driver for JDBC.
Table:

Example

create table attendance3
(
slno varchar2(40),
noofstudents varchar2(20),
present number(6),
absent number(5)
);

attendance.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="attendanceprocess.jsp">
slno<input type="text" name="slno">
no.of students<input type="text" name="students">
present<input type="text" name="present">
absent<input type="text" name="absent">
<input type="submit" value="submit">
</form>
</body>
</html>

attendanceprocess.jsp:

<%@page import="java.sql.*" %>
<%@page import="oracle.jdbc.driver.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String s1=request.getParameter("slno");
int i=Integer.parseInt(s1);
String s2=request.getParameter("students");
int j=Integer.parseInt(s2);
String s3=request.getParameter("present");
int x=Integer.parseInt(s3);
String s4=request.getParameter("absent");
int y=Integer.parseInt(s4);
int k=0;
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:xe","SYSTEM","oracle");
PreparedStatement ps=con.prepareStatement ("insert into attendance3 values(?,?,?,?)");
ps.setInt(1,i);
ps.setInt(2,j);
ps.setInt(3,x);
ps.setInt(4,y);
k=ps.executeUpdate();
if(k>0)
{
response.sendRedirect("welcome.jsp");
}
con.close();
}
catch(Exception e)
{
}
%>
</body>
</html>

welcome.jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.print("data is successfully inserted");
%>
</body>
</html>

web.xml:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>implement jdbc in jsp</display-name>
<welcome-file-list>
<welcome-file>attendance.jsp</welcome-file>
</welcome-file-list>
</web-app>

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment