pharmacy.Add.java Source code

Java tutorial

Introduction

Here is the source code for pharmacy.Add.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pharmacy;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.sun.xml.rpc.processor.util.StringUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import javax.sql.DataSource;

/**
 *
 * @author dominik
 */
public class Add extends HttpServlet {

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    String name;
    int price;
    int illnesscode;
    String substances;
    String sideeffects;
    List<String> testList = new ArrayList<>();

    private void test() {
        for (int i = 0; i < 10; i++) {
            testList.add("Test" + (i + 1));
        }
        String s = Joiner.on(", ").join(testList);

        List<Medicine> medicines = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            Medicine m = new Medicine();
            m.setIllnessCode(i);
            m.setName("Medicine " + (i + 1));
            m.setPrice(100);
            medicines.add(m);
        }

        Iterable<Medicine> onlyMod2 = Iterables.filter(medicines, new Predicate<Medicine>() {

            @Override
            public boolean apply(Medicine t) {
                return t.getIllnessCode() % 2 == 0;
            }
        });

        List<Medicine> filtered = ImmutableList.copyOf(onlyMod2);

        Iterable<String> names = Iterables.transform(medicines, new Function<Medicine, String>() {

            @Override
            public String apply(Medicine f) {
                return f.getName();
            }
        });

        String namesStr = Joiner.on(", ").join(names);

    }

    public String join(String separator, Iterable iterable) {
        StringBuilder sb = new StringBuilder();
        Iterator it = iterable.iterator();
        while (it.hasNext()) {
            sb.append(it.next());
            if (it.hasNext()) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
            if (request.getParameter("name") != null) {
                name = request.getParameter("name");
            } else {
                out.println(1);
                return;
            }
            try {
                price = Integer.parseInt(request.getParameter("price"));
            } catch (NumberFormatException e) {
                out.println(2);
                return;
            }
            try {
                illnesscode = Integer.parseInt(request.getParameter("illnescode"));
            } catch (NumberFormatException e) {
                out.println(3);
                return;
            }
            //out.println(price);
            String substancesUrl = request.getParameter("substances");
            String sideeffectsUrl = request.getParameter("sideeffects");

            if (substances != null) {
                substances = substancesUrl;
            } else {
                substances = "";
            }
            if (sideeffects != null) {
                sideeffects = sideeffectsUrl;
            } else {
                sideeffects = "";
            }
        } catch (Exception e) {
        }

        Medicine m = new Medicine();
        m.setName(name);
        m.setPrice(price);
        m.setIllnessCode(illnesscode);
        m.setSideEffects(sideeffects);
        m.setSubstances(substances);
        //out.println(m.toString());

        try {

            InitialContext ic = new InitialContext();
            DataSource ds = (DataSource) ic.lookup("jdbc/sample");
            conn = ds.getConnection();
            Statement stmt = conn.createStatement();

            String sql = "INSERT INTO app.medicines  VALUES ('" + m.getName() + "'," + m.getIllnessCode() + ",'"
                    + m.getSubstances() + "','" + m.getSideeffects() + "'," + m.getPrice() + ")";
            stmt.executeUpdate(sql);
            try {
                out.println("SIKERESEN BE LETT ILLESZTVE");
                Thread.sleep(3000);
                response.sendRedirect("lista.jsp");
            } catch (InterruptedException e) {

            }

            //response.sendRedirect(sql);
        } catch (Exception ex) {
            out.println("Hiba trtnt mentskor");

            try {
                Thread.sleep(3000);
                response.sendRedirect("index.jsp");
            } catch (InterruptedException e) {

            }
            //            response.sendRedirect("index.jsp?sorok="+sorok);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception ex1) {
                }
            }
            if (pstmt != null) {
                try {
                    pstmt.close();
                } catch (Exception ex1) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception ex1) {
                }
            }
            out.close();
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }

}