daily pastebin goal
52%
SHARE
TWEET

Honda Civic Type-R

a guest Mar 20th, 2017 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.annotation.WebServlet;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23.  
  24.  
  25. @WebServlet(urlPatterns = {"/ProductController"})
  26. public class ProductController extends HttpServlet {
  27.  
  28.     /**
  29.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  30.      * methods.
  31.      *
  32.      * @throws java.sql.SQLException
  33.      */
  34.    
  35.     public void showProducts() throws SQLException{
  36.     try{
  37.        
  38.         String url="jdbc:derby://localhost:1527/SiteDB";
  39.         Connection con=DriverManager.getConnection(url,"a","a");
  40.         String sql="SELECT * FROM app.products";
  41.         Statement instr = con.createStatement();
  42.         ResultSet rs= instr.executeQuery(sql);
  43.         while(rs.next())
  44.         {
  45.             String name=rs.getString("name");
  46.         }
  47.         } catch (SQLException ex)
  48.         {
  49.                 ex.printStackTrace();
  50.         }  
  51.     }
  52.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  53.             throws ServletException, IOException, SQLException {
  54.         response.setContentType("text/html;charset=UTF-8");
  55.         showProducts();
  56.         try (PrintWriter out = response.getWriter()) {
  57.             /* TODO output your page here. You may use following sample code. */
  58.             out.println("<!DOCTYPE html>");
  59.             out.println("<html>");
  60.             out.println("<head>");
  61.             out.println("<title>Servlet ProductController</title>");
  62.             out.println("</head>");
  63.             out.println("<body>");
  64.             out.println("<h1>Servlet ProductController at " + request.getContextPath() + "</h1>");
  65.             out.println("</body>");
  66.             out.println("</html>");
  67.         }
  68.     }
  69.  
  70.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  71.     /**
  72.      * Handles the HTTP <code>GET</code> method.
  73.      *
  74.      * @param request servlet request
  75.      * @param response servlet response
  76.      * @throws ServletException if a servlet-specific error occurs
  77.      * @throws IOException if an I/O error occurs
  78.      */
  79.     @Override
  80.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  81.             throws ServletException, IOException {
  82.         try {
  83.             processRequest(request, response);
  84.         } catch (SQLException ex) {
  85.             Logger.getLogger(ProductController.class.getName()).log(Level.SEVERE, null, ex);
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Handles the HTTP <code>POST</code> method.
  91.      *
  92.      * @param request servlet request
  93.      * @param response servlet response
  94.      * @throws ServletException if a servlet-specific error occurs
  95.      * @throws IOException if an I/O error occurs
  96.      */
  97.     @Override
  98.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  99.             throws ServletException, IOException {
  100.         List<Product> products = null;
  101.         try {
  102.  
  103.             String url = "jdbc:derby://localhost:1527/SiteDB";
  104.             Connection con = DriverManager.getConnection(url, "a", "a");
  105.             String sql = "SELECT * FROM A.products";
  106.             Statement instr = con.createStatement();
  107.             ResultSet rs = instr.executeQuery(sql);
  108.             products = new ArrayList<Product>();
  109.  
  110.             while (rs.next()) {
  111.                 String name = rs.getString("name");
  112.                 double price = rs.getDouble("price");
  113.                 products.add(new Product(name,price));
  114.             }
  115.         } catch (SQLException ex) {
  116.             ex.printStackTrace();
  117.         }
  118.         request.setAttribute("products", products);
  119.         //processRequest(request, response);
  120.         request.getRequestDispatcher("ProductView").forward(request, response);
  121.     }
  122.  
  123.     /**
  124.      * Returns a short description of the servlet.
  125.      *
  126.      * @return a String containing servlet description
  127.      */
  128.     @Override
  129.     public String getServletInfo() {
  130.         return "Short description";
  131.     }// </editor-fold>
  132.  
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top