jfsd skill 5 question 1 task 1

Published on September 02, 2021
Last updated September 02, 2021

Using JSP Concepts try to execute the following scenario

Scenario

Create a table in the database containing the columns to store book details like book name, authors, description, price, and URL (Uniform Resource Locator) of the book’s cover image. Using JSP (Java Server Pages) and JDBC retrieve the details in the table and display them on the webpage.

Video explaining the code and process in telugu language

Create book table in database

The following sql query will create a table called skill5books which contains our books data

CREATE TABLE skill5books(
    book_name VARCHAR(26) NOT NULL, 
    author VARCHAR(26) NOT NULL, 
    description VARCHAR(255) NOT NULL, 
    price NUMBER NOT NULL, 
    cover_image_url VARCHAR(255) NOT NULL);

After the table has been created in the database insert some books data into the table.

SQL command to insert data into table

INSERT INTO skill5books (book_name, author, description, price, cover_image_url) VALUES ('Harry Potter', 'J K Rowling', 'magic', 226, 'https://anreddy.in/harry.png');

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Skill 5 question 1</title>
<style>
table, th, td {
  border:1px solid black;
}
</style>
</head>
<body>
	<%
	Class.forName("oracle.jdbc.driver.OracleDriver");

	String dbUserName = "system";

	String dbUserPassword = "nikhil4u";

	Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", dbUserName, dbUserPassword);

	Statement stmt = con.createStatement();

	ResultSet rs = stmt.executeQuery("SELECT * FROM skill5books");

	if (!rs.isBeforeFirst()) {
		out.println("No books available please add one");
	} else {
	%>
	<table style="width:100%">
		<tr>
			<th>
				<h2>Book name</h2>
			</th>
			<th>
				<h2>Author</h2>

			</th>
			<th>
				<h2>Description</h2>
			</th>
			<th>
				<h2>Price</h2>
			</th>
			<th>
				<h2>Cover Image url</h2>
			</th>
		</tr>
		<%
		while (rs.next()) {
		%>
		<tr>
			<td><%=rs.getString(1)%></td>

			<td><%=rs.getString(2)%></td>

			<td><%=rs.getString(3)%></td>

			<td><%=rs.getInt(4)%></td>

			<td><%=rs.getString(5)%></td>
			
			<td><a href="#">Add to cart</a></td>
		</tr>
		<%
		}
		%>
	</table>
	<%
	}
	%>
</body>
</html>


Tags :