0% found this document useful (0 votes)
12 views3 pages

Assignment 5

The document provides a sample program demonstrating the use of a Servlet to display data from a MySQL database table named 'ebookshop'. It includes SQL commands for creating the database and table, inserting sample data, and the Java Servlet code to retrieve and display the data in an HTML format. Additionally, it outlines the steps required to run the servlet in a web server environment like Apache Tomcat.

Uploaded by

shreyadeore2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Assignment 5

The document provides a sample program demonstrating the use of a Servlet to display data from a MySQL database table named 'ebookshop'. It includes SQL commands for creating the database and table, inserting sample data, and the Java Servlet code to retrieve and display the data in an HTML format. Additionally, it outlines the steps required to run the servlet in a web server environment like Apache Tomcat.

Uploaded by

shreyadeore2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5.

Implement the sample program demonstrating the use of Servlet.


e.g., Create a database table ebookshop (book_id, book_title, book_author,
book_price, quantity) using database like Oracle/MySQL etc. and display (use SQL
select query) the table content using servlet.
Database Setup (MySQL):

CREATE DATABASE bookdb;

USE bookdb;

CREATE TABLE ebookshop (


book_id INT PRIMARY KEY,
book_title VARCHAR(100),
book_author VARCHAR(100),
book_price FLOAT,
quantity INT
);

INSERT INTO ebookshop VALUES


(1, 'Java Programming', 'James Gosling', 450.50, 10),
(2, 'Data Structures', 'Mark Allen', 300.00, 5),
(3, 'Servlets and JSP', 'Kathy Sierra', 500.00, 8);

Servlet Code: BookServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class BookServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Database credentials
String jdbcURL = "jdbc:mysql://localhost:3306/bookdb";
String dbUser = "root";
String dbPassword = "your_password"; // Replace with your MySQL password

try {
Class.forName("com.mysql.cj.jdbc.Driver"); // Load MySQL JDBC driver
Connection conn = DriverManager.getConnection(jdbcURL, dbUser, dbPassword);

Statement stmt = conn.createStatement();


ResultSet rs = stmt.executeQuery("SELECT * FROM ebookshop");
out.println("<html><body>");
out.println("<h2>Book List from ebookshop</h2>");
out.println("<table border='1'>");

out.println("<tr><th>ID</th><th>Title</th><th>Author</th><th>Price</th><th>Quantity</th></tr
>");

while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt("book_id") + "</td>");
out.println("<td>" + rs.getString("book_title") + "</td>");
out.println("<td>" + rs.getString("book_author") + "</td>");
out.println("<td>" + rs.getFloat("book_price") + "</td>");
out.println("<td>" + rs.getInt("quantity") + "</td>");
out.println("</tr>");
}

out.println("</table>");
out.println("</body></html>");

rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
out.println("<h3>Error: " + e.getMessage() + "</h3>");
} finally {
out.close();
}
}
}

Web.xml Deployment Descriptor


<web-app>
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/viewbooks</url-pattern>
</servlet-mapping>
</web-app>
Steps to Run

1. Place the servlet code in your servlet container (e.g., Apache Tomcat) inside the
src folder.
2. Compile it using a Java compiler.
3. Add the MySQL Connector JAR (mysql-connector-java-8.x.x.jar) to your
lib folder.
4. Deploy the project on your server.
5. Access via browser:
http://localhost:8080/your_project_name/viewbooks

You might also like