Guest User

Untitled

a guest
Sep 7th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;
  5. import java.util.*;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. import java.sql.*;
  15.  
  16. public class ServPost extends HttpServlet {
  17.  
  18.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  19.             throws ServletException, IOException {
  20.        
  21.         String mode = request.getParameter( "mode" );
  22.  
  23.         if(mode == null)
  24.             mode = "read";
  25.        
  26.             switch(mode){
  27.                 case "create": 
  28.                                 create(request, response);
  29.                                 break;
  30.            
  31.                 case "read":   
  32.                                 read(request, response);
  33.                                 break;
  34.                    
  35.                 case "update":
  36.                                 update(request, response);
  37.                                 break;
  38.                    
  39.                 case "delete":
  40.                                 delete(request, response);
  41.                                 break;
  42.                
  43.                 default:    System.out.println("Invalid Param");
  44.             }
  45.     }
  46.            
  47.     protected void delete(HttpServletRequest request, HttpServletResponse response)
  48.             throws ServletException, IOException{
  49.         response.setContentType("text/xml");
  50.        
  51.         String driver = "com.mysql.jdbc.Driver";
  52.         Connection con = null;
  53.         Statement stmt = null;
  54.        
  55.         try {
  56.             Class.forName(driver).newInstance();
  57.             String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
  58.             con = DriverManager.getConnection(url);
  59.             stmt = con.createStatement();
  60.            
  61.             stmt.executeUpdate("Delete From produkt where produkt_id > 0;");
  62.             System.out.println("Database cleared");
  63.            
  64.         }  
  65.         catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  66.             e.printStackTrace();
  67.             System.out.println("Exception");
  68.         }  
  69.     }
  70.  
  71.     protected void create(HttpServletRequest request, HttpServletResponse response)
  72.             throws ServletException, IOException{
  73.        
  74.         response.setContentType("text/xml");
  75.         String name = request.getParameter( "name" );
  76.         String sugar = request.getParameter( "sugar" );
  77.         String shelflife = request.getParameter( "shelflife" );
  78.                
  79.         String driver = "com.mysql.jdbc.Driver";
  80.         Connection con = null;
  81.         Statement stmt = null;
  82.    
  83.         try {
  84.             Class.forName(driver).newInstance();
  85.             String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
  86.             con = DriverManager.getConnection(url);
  87.             stmt = con.createStatement();
  88.            
  89.             if (name == null | sugar == null | shelflife == null){
  90.                 System.out.println("Dont worked:" + name + " " + shelflife + " " + sugar);
  91.             }
  92.             else if (!sugar.equals("") && !shelflife.equals("")) {
  93.                 stmt.executeUpdate("INSERT INTO produkt (name, mhd, zucker) VALUES ('"+ name + "','" + shelflife + "','" + sugar + "')");
  94.                 System.out.println("Worked: all");
  95.             }
  96.             else if (sugar.equals("") && shelflife.equals("")) {
  97.                 stmt.executeUpdate("INSERT INTO produkt (name) VALUES ('"+ name + "')");
  98.                 System.out.println("Worked: name");
  99.             }
  100.             else if (!sugar.equals("") && shelflife.equals("")) {
  101.                 stmt.executeUpdate("INSERT INTO produkt (name, zucker) VALUES ('"+ name + "','" + sugar + "')");
  102.                 System.out.println("Worked: name+sugar");
  103.             }
  104.             else if (sugar.equals("") && !shelflife.equals("")) {
  105.                 stmt.executeUpdate("INSERT INTO produkt (name, mhd) VALUES ('"+ name + "','" + shelflife + "')");
  106.                 System.out.println("Worked: name+shelf life");
  107.             }
  108.            
  109.             stmt.close();
  110.             con.close();
  111.            
  112.         }
  113.         catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  114.             e.printStackTrace();
  115.         System.out.println("Exception");
  116.         }  
  117.  
  118.     }
  119.    
  120.     protected void read(HttpServletRequest request, HttpServletResponse response)
  121.             throws ServletException, IOException{
  122.        
  123.         PrintWriter out = response.getWriter();
  124.         response.setContentType("text/xml");
  125.  
  126.         String driver = "com.mysql.jdbc.Driver";
  127.         Connection con = null;
  128.         ResultSet rst = null;
  129.         Statement stmt = null;
  130.        
  131.         try {
  132.         Class.forName(driver).newInstance();
  133.         String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
  134.         con = DriverManager.getConnection(url);
  135.         stmt = con.createStatement();
  136.         rst = stmt.executeQuery("SELECT * FROM produkt;");
  137.        
  138.         String name;
  139.         String sugar;
  140.         String shelflife;
  141.         String id;
  142.  
  143.         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
  144.         out.println("<storage>");
  145.        
  146.         while(rst.next()){
  147.             name = rst.getString("name");
  148.             shelflife = rst.getString("mhd");
  149.             sugar = rst.getString("zucker");
  150.             id = rst.getString("produkt_id");
  151.                    
  152.             out.println("<product>");
  153.                 out.println("<id>" + id +"</id>");
  154.                 out.println("<name>" + name +"</name>");
  155.                 out.println("<sugarcontent>" + sugar + "</sugarcontent>");
  156.                 out.println("<shelflife>" + shelflife + "</shelflife>");
  157.             out.println("</product>");
  158.         }
  159.         }
  160.         catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  161.             e.printStackTrace();
  162.         System.out.println("Exception");
  163.         }  
  164.         out.println("</storage>");
  165.     }
  166.    
  167.     protected void update(HttpServletRequest request, HttpServletResponse response)
  168.             throws ServletException, IOException{
  169.         response.setContentType("text/xml");
  170.         String id = request.getParameter( "id" );
  171.         String name = request.getParameter( "name" );
  172.         String sugar = request.getParameter( "sugar" );
  173.         String shelflife = request.getParameter( "shelflife" );
  174.    
  175.         String driver = "com.mysql.jdbc.Driver";
  176.         Connection con = null;
  177.         Statement stmt = null;
  178.    
  179.         try {
  180.             Class.forName(driver).newInstance();
  181.             String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
  182.             con = DriverManager.getConnection(url);
  183.             stmt = con.createStatement();
  184.            
  185.             if (name == null | sugar == null | shelflife == null){
  186.                 System.out.println("Dont worked:" + name + " " + shelflife + " " + sugar);
  187.                 name = ""; sugar = ""; shelflife = "";
  188.             }
  189.             else
  190.             if ( name != "" & sugar != "" & shelflife != "" ){
  191.                 stmt.executeUpdate("UPDATE produkt SET name='" + name + "', mhd='" + shelflife + "',zucker='" + sugar + "' Where produkt_id='" + id + "'");
  192.                 System.out.println("Updated: all");
  193.             }
  194.             else{
  195.                 if ( name != "" ){
  196.                     stmt.executeUpdate("UPDATE produkt SET name='" + name + "' Where produkt_id='" + id + "'");
  197.                     System.out.println("Updated: name");
  198.                 }
  199.                 if ( sugar != "" ){
  200.                     stmt.executeUpdate("UPDATE produkt SET zucker='" + sugar + "' Where produkt_id='" + id + "'");
  201.                     System.out.println("Updated: sugar");
  202.                 }
  203.                 if ( shelflife != "" ){
  204.                     stmt.executeUpdate("UPDATE produkt SET mhd='" + shelflife + "' Where produkt_id='" + id + "'");
  205.                     System.out.println("Updated: shelflife");
  206.                 }
  207.             }
  208.  
  209.             stmt.close();
  210.             con.close();
  211.            
  212.         }
  213.         catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  214.             e.printStackTrace();
  215.         System.out.println("Exception");
  216.         }  
  217.  
  218.     }
  219.        
  220.  
  221.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  222.             throws ServletException, IOException {
  223.         doGet(request, response);
  224.        
  225.     }
  226. }
Add Comment
Please, Sign In to add comment