Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- import java.util.*;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.sql.*;
- public class ServPost extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String mode = request.getParameter( "mode" );
- if(mode == null)
- mode = "read";
- switch(mode){
- case "create":
- create(request, response);
- break;
- case "read":
- read(request, response);
- break;
- case "update":
- update(request, response);
- break;
- case "delete":
- delete(request, response);
- break;
- default: System.out.println("Invalid Param");
- }
- }
- protected void delete(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException{
- response.setContentType("text/xml");
- String driver = "com.mysql.jdbc.Driver";
- Connection con = null;
- Statement stmt = null;
- try {
- Class.forName(driver).newInstance();
- String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
- con = DriverManager.getConnection(url);
- stmt = con.createStatement();
- stmt.executeUpdate("Delete From produkt where produkt_id > 0;");
- System.out.println("Database cleared");
- }
- catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Exception");
- }
- }
- protected void create(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException{
- response.setContentType("text/xml");
- String name = request.getParameter( "name" );
- String sugar = request.getParameter( "sugar" );
- String shelflife = request.getParameter( "shelflife" );
- String driver = "com.mysql.jdbc.Driver";
- Connection con = null;
- Statement stmt = null;
- try {
- Class.forName(driver).newInstance();
- String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
- con = DriverManager.getConnection(url);
- stmt = con.createStatement();
- if (name == null | sugar == null | shelflife == null){
- System.out.println("Dont worked:" + name + " " + shelflife + " " + sugar);
- }
- else if (!sugar.equals("") && !shelflife.equals("")) {
- stmt.executeUpdate("INSERT INTO produkt (name, mhd, zucker) VALUES ('"+ name + "','" + shelflife + "','" + sugar + "')");
- System.out.println("Worked: all");
- }
- else if (sugar.equals("") && shelflife.equals("")) {
- stmt.executeUpdate("INSERT INTO produkt (name) VALUES ('"+ name + "')");
- System.out.println("Worked: name");
- }
- else if (!sugar.equals("") && shelflife.equals("")) {
- stmt.executeUpdate("INSERT INTO produkt (name, zucker) VALUES ('"+ name + "','" + sugar + "')");
- System.out.println("Worked: name+sugar");
- }
- else if (sugar.equals("") && !shelflife.equals("")) {
- stmt.executeUpdate("INSERT INTO produkt (name, mhd) VALUES ('"+ name + "','" + shelflife + "')");
- System.out.println("Worked: name+shelf life");
- }
- stmt.close();
- con.close();
- }
- catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Exception");
- }
- }
- protected void read(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException{
- PrintWriter out = response.getWriter();
- response.setContentType("text/xml");
- String driver = "com.mysql.jdbc.Driver";
- Connection con = null;
- ResultSet rst = null;
- Statement stmt = null;
- try {
- Class.forName(driver).newInstance();
- String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
- con = DriverManager.getConnection(url);
- stmt = con.createStatement();
- rst = stmt.executeQuery("SELECT * FROM produkt;");
- String name;
- String sugar;
- String shelflife;
- String id;
- out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>");
- out.println("<storage>");
- while(rst.next()){
- name = rst.getString("name");
- shelflife = rst.getString("mhd");
- sugar = rst.getString("zucker");
- id = rst.getString("produkt_id");
- out.println("<product>");
- out.println("<id>" + id +"</id>");
- out.println("<name>" + name +"</name>");
- out.println("<sugarcontent>" + sugar + "</sugarcontent>");
- out.println("<shelflife>" + shelflife + "</shelflife>");
- out.println("</product>");
- }
- }
- catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Exception");
- }
- out.println("</storage>");
- }
- protected void update(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException{
- response.setContentType("text/xml");
- String id = request.getParameter( "id" );
- String name = request.getParameter( "name" );
- String sugar = request.getParameter( "sugar" );
- String shelflife = request.getParameter( "shelflife" );
- String driver = "com.mysql.jdbc.Driver";
- Connection con = null;
- Statement stmt = null;
- try {
- Class.forName(driver).newInstance();
- String url = "jdbc:mysql://localhost/coke_db?user=root&password=root";
- con = DriverManager.getConnection(url);
- stmt = con.createStatement();
- if (name == null | sugar == null | shelflife == null){
- System.out.println("Dont worked:" + name + " " + shelflife + " " + sugar);
- name = ""; sugar = ""; shelflife = "";
- }
- else
- if ( name != "" & sugar != "" & shelflife != "" ){
- stmt.executeUpdate("UPDATE produkt SET name='" + name + "', mhd='" + shelflife + "',zucker='" + sugar + "' Where produkt_id='" + id + "'");
- System.out.println("Updated: all");
- }
- else{
- if ( name != "" ){
- stmt.executeUpdate("UPDATE produkt SET name='" + name + "' Where produkt_id='" + id + "'");
- System.out.println("Updated: name");
- }
- if ( sugar != "" ){
- stmt.executeUpdate("UPDATE produkt SET zucker='" + sugar + "' Where produkt_id='" + id + "'");
- System.out.println("Updated: sugar");
- }
- if ( shelflife != "" ){
- stmt.executeUpdate("UPDATE produkt SET mhd='" + shelflife + "' Where produkt_id='" + id + "'");
- System.out.println("Updated: shelflife");
- }
- }
- stmt.close();
- con.close();
- }
- catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
- e.printStackTrace();
- System.out.println("Exception");
- }
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
Add Comment
Please, Sign In to add comment