Raizekas

PrekesIvedimoServlet

Feb 23rd, 2021
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.db.veiksmai;
  2.  
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.PreparedStatement;
  13.  
  14. @WebServlet(
  15. name = "prekesIvedimoServlet",
  16. urlPatterns = "/PrekesIvedimas" )
  17. public class PrekesIvedimoServlet extends HttpServlet
  18. {
  19. @Override
  20. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  21. {
  22. /*------------------------Gauname duomenis is HTML formos*----------------------------*/
  23. String prekesPavadinimas = req.getParameter("pav");
  24. double prekesKaina = Double.parseDouble(req.getParameter("kaina"));
  25. String prekesAprasas = req.getParameter("aprasas");
  26. System.out.println(prekesPavadinimas);
  27. System.out.println(prekesKaina + 1);
  28. System.out.println(prekesAprasas);
  29.  
  30. /*------------------------Veiksmai su Duomenu baze prasideda*----------------------*/
  31.  
  32. try {
  33. Connection jungtis = DuombazesSukurimas.sukurtiDuomenuBaze();
  34.  
  35. String sqlUzklausa = "INSERT INTO prekes (pav, kaina, aprasas) VALUES (?, ?, ?)";
  36. PreparedStatement prepSt = jungtis.prepareStatement(sqlUzklausa);
  37.  
  38. prepSt.setString(1, prekesPavadinimas);
  39. prepSt.setDouble(2, prekesKaina);
  40. prepSt.setString(3, prekesAprasas);
  41.  
  42. prepSt.executeUpdate();
  43.  
  44. prepSt.close();
  45. jungtis.close();
  46.  
  47. PrintWriter out = resp.getWriter();
  48. out.println("Idejome duomenu baze");
  49. }
  50. catch (Exception e)
  51. {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56.  
RAW Paste Data