0% found this document useful (0 votes)
13 views4 pages

Bookcode-Servlet Code

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

Bookcode-Servlet Code

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

HTML:

<!DOCTYPE html>
<html>
<head>
<title>Book Catalog</title>
</head>
<body>
<h2>Book Catalog</h2>
<form action="http://localhost:8084/book/BookCatalogServlet" method="get">
<button type="submit">View Book Catalog</button>
</form>
<form action="http://localhost:8084/book/BookCatalogServlet1" method="get">
<label for="bname">Enter the Book title to search:</label>
<input type="text" id="bname" name="bname" placeholder="Book title">
<button type="submit">Search</button>
</form>
</body>
</html>

BookCatalogServlet:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BookCatalogServlet")
public class BookCatalogServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Book Catalog</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Book Catalog</h2>");

Connection connection = null;


Statement statement = null;
ResultSet rs = null;

try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connection = DriverManager.getConnection("jdbc:derby://localhost:1527/six", "root", "root");
statement = connection.createStatement();
String queryString = "SELECT * FROM BOOKDB";
rs = statement.executeQuery(queryString);

out.println("<table cellpadding=\"15\" border=\"1\" style=\"background-color: #ffffcc;\">");


while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
out.println("<td>" + rs.getString(3) + "</td>");
out.println("<td>" + rs.getString(4) + "</td>");
out.println("<td>" + rs.getString(5) + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (Exception ex) {
out.println("<font size=\"+3\" color=\"red\"><b>Unable to connect to
database.</b></font>");
ex.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

out.println("<table>");
out.println("<tr>");
out.println("<td><form action=\"index.html\" method=\"get\">");
out.println("<button type=\"submit\"><-- back</button></form></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}

BookCatalogServlet1:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/BookCatalogServlet1")
public class BookCatalogServlet1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Book Catalog</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Book Catalog</h2>");

Connection connection = null;


Statement statement = null;
ResultSet rs = null;

try {
String connectionURL = "jdbc:derby://localhost:1527/six";
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
String ss = request.getParameter("bname");
String queryString = "SELECT * FROM bookdb WHERE bname LIKE '" + ss + "%'";
rs = statement.executeQuery(queryString);

out.println("<table cellpadding=\"15\" border=\"1\" style=\"background-color: #ffffcc;\">");


while (rs.next()) {
out.println("<tr>");
out.println("<td>" + rs.getInt(1) + "</td>");
out.println("<td>" + rs.getString(2) + "</td>");
out.println("<td>" + rs.getString(3) + "</td>");
out.println("<td>" + rs.getString(4) + "</td>");
out.println("<td>" + rs.getString(5) + "</td>");
out.println("</tr>");
}
out.println("</table>");
} catch (Exception ex) {
out.println("<font size=\"+3\" color=\"red\"><b>Unable to connect to
database.</b></font>");
ex.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

out.println("<table>");
out.println("<tr>");
out.println("<td><form action=\"index.html\" method=\"get\">");
out.println("<button type=\"submit\"><-- back</button></form></td>");
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}

You might also like