In this tutorial we will learn how to retrieve / fetch data from database table. Here we have given a simple example which will provide better clarity about retrieving / fetching data from database table.
First of all, in MySQL environment we have created a table named as student_marks in marksdb database.
create database marksdb
create student_marks table:
create table marksdb.student_marks
(
regdno varchar(200) primary key,
name varchar(200) not null,
sub1 int,
sub2 int,
sub3 int,
sub4 int
)
Then we have inserted three records by executing insert statements.
insert into marksdb.student_marks
values(101,'John',50,55,45,52)
insert into marksdb.student_marks
values(101,'John',55,56,46,54)
insert into marksdb.student_marks
values(101,'John',45,57,48,51)
Then coming to Eclipse environment for developing the following files:
marks.jsp
process.jsp
web.xml
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<center><h1>Student Marks Form</h1>
<form action="process.jsp">
<table>
<tr>
<td>regdno</td>
<td><input type="text"name="regdno"></td>
</tr>
<tr>
<td><input type="submit"value="view"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String s1=request.getParameter("regdno");
String s2=request.getParameter("name");
Class.forName("com.mysql.jdbc.Driver");
Connection
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/marksdb
","root","Silan@123");
Statement statement = connection.createStatement();
ResultSet resultset = statement.executeQuery("select * from
student_marks where regdno = '" + s1 + "'");
if(!resultset.next())
{
out.println("Sorry, there is no marks record available");
}
else
{
%>
<TABLE BORDER="1">
<TR>
<TH>regdno</TH>
<TH>name</TH>
<TH>sub1</TH>
<TH>sub2</TH>
<TH>sub3</TH>
<TH>sub4</TH>
</TR>
<TR>
<TD> <%=resultset.getString(1)%> </TD>
<TD> <%=resultset.getString(2)%> </TD>
<TD> <%=resultset.getInt(3)%></TD>
<TD> <%=resultset.getInt(4)%></TD>
<TD> <%=resultset.getInt(5)%></TD>
<TD> <%=resultset.getInt(6)%></TD>
</TR>
</TABLE>
<BR>
<%
}
%>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID"
version="4.0">
<display-name>JSP_Projects</display-name>
<welcome-file-list>
<welcome-file>marks.jsp</welcome-file>
</welcome-file-list>
</web-app>
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc