Guest User

Untitled

a guest
Mar 3rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. import java.io.*;               // needed for I/O processing
  2. import java.sql.*;              // needed for SQL query
  3. import javax.servlet.*;         // Generic Servlet
  4. import javax.servlet.http.*;    // HttpServlet
  5.  
  6. public class MainServlet extends HttpServlet {
  7.  
  8.     private Connection connection;
  9.  
  10.     // init() runs when servlet is loaded into the server
  11.     public void init() throws ServletException {
  12.         try {
  13.             connection = DriverManager.getConnection(
  14.                 "jdbc:mysql://localhost:3306/estoredb", "root", "");
  15.         } catch (SQLException exception) {
  16.             exception.printStackTrace();
  17.         }
  18.     }
  19.  
  20.     // doGet() run once per HTTP GET request to the server
  21.     public void doGet(HttpServletRequest request,
  22.                 HttpServletResponse response)
  23.                 throws ServletException, IOException {
  24.         // Set the MIME type for the response message
  25.         response.setContentType("text/html");
  26.         PrintWriter out = response.getWriter();
  27.  
  28.         Statement statement = null;
  29.         try {
  30.            
  31.             statement = connection.createStatement();
  32.  
  33.             String sqlStr = "select * from categories";
  34.             ResultSet categorySet = statement.executeQuery(sqlStr);
  35.            
  36.             int categorySum = categorySet.getFetchSize();
  37.             int     categoryIds[]   = new int[categorySum];
  38.             String  categoryNames[] = new String[categorySum];
  39.  
  40.             int i = 0;
  41.             while (categorySet.next()) {
  42.                 categoryIds[i] = categorySet.getInt("id");
  43.                 categoryNames[i] = categorySet.getString("name");
  44.                 i++;
  45.             }
  46.            
  47.             request.setAttribute ("categoryIds", categoryIds);
  48.             request.setAttribute ("categoryNames", categoryNames);
  49.            
  50.             String apple="appleshop";
  51.             request.setAttribute("fruit", apple);
  52.            
  53.             getServletConfig().getServletContext().getRequestDispatcher(
  54.                    "/jsp/main.jsp").forward(request, response);
  55.         } catch (SQLException exception) {
  56.             exception.printStackTrace();
  57.         } finally {
  58.             try {
  59.                 if (statement != null) statement.close();
  60.             } catch (SQLException exception) {
  61.                 exception.printStackTrace();
  62.             }
  63.         }
  64.     }
  65.  
  66.     // destroy() runs when the servlet is unloaded from the server
  67.     public void destroy() {
  68.         try {
  69.             connection.close();
  70.         } catch (SQLException exception) {
  71.             exception.printStackTrace();
  72.         }
  73.     }
  74. }
Add Comment
Please, Sign In to add comment