Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@page import="java.sql.Connection"%>
- <%@page import="databaseManagement.DBConnection"%>
- <%@page import="java.sql.ResultSet" %>
- <%@page import="java.sql.SQLException"%>
- <%@page import="java.sql.Connection"%>
- <%@page import="java.sql.PreparedStatement"%>
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
- <title>Search Our Products</title>
- </head>
- <body>
- <form method="post">
- Search:<input type="text" name="Search">
- <input type="submit" value="Go">
- <table border="2">
- <tr>
- <td>ID</td>
- <td>NAME</td>
- <td>DESCRIPTION</td>
- <td>PRICE</td>
- </tr>
- <%
- try
- {
- DBConnection db = new DBConnection();
- Connection con = db.getConnection();
- PreparedStatement ps = con.prepareStatement("select * from products");
- ResultSet rs = ps.executeQuery();
- while(rs.next())
- {
- %>
- <tr>
- <td><%=rs.getInt("ID") %></td>
- <td><%=rs.getString("NAME") %></td>
- <td><%=rs.getString("DESCRIPTION") %></td>
- <td><%=rs.getString("PRICE") %></td>
- </tr>
- <%
- }
- %>
- </table>
- <%
- rs.close();
- con.close();
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- %>
- </form>
- </body>
- </html>
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.io.PrintWriter;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import databaseManagement.DBConnection;
- @WebServlet("/ProductSearch")
- public class ProductSearch extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public ProductSearch() {
- super();
- // TODO Auto-generated constructor stub
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- // TODO Auto-generated method stub
- String searchTerm = request.getParameter("Search");
- try {
- DBConnection db = new DBConnection();
- Connection con = db.getConnection();
- PreparedStatement ps = con.prepareStatement("select * from products where name like %?%");
- ps.setString(1, searchTerm);
- ResultSet rs = ps.executeQuery();
- return;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- package databaseManagement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DBConnection {
- public Connection getConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- // TODO: finish
- //CHANGE USERNAME AND PASSWORD WHEN IMPLIMENTING ON VM
- Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/applicationdata", "root", "safepassword");
- return con;
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- return null;
- } catch (SQLException e) {
- e.printStackTrace();
- return null;
- }
- }
- }
Add Comment
Please, Sign In to add comment