Raizekas

servletas

Feb 23rd, 2021
56
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. // String dbDriver = "com.mysql.jdbc.Driver";
  32. String dbNuoroda = "jdbc:mysql:// localhost:3306/";
  33. String dbVardas = "prekiu_katalogas";
  34. String dbUsername = "root";
  35. String dbPassword = "";
  36.  
  37. try {
  38. Connection jungtis = DriverManager.getConnection(dbNuoroda + dbVardas, dbUsername, dbPassword);
  39.  
  40. String sqlUzklausa = "INSERT INTO prekes (pav, kaina, aprasas) VALUES (?, ?, ?)";
  41. PreparedStatement prepSt = jungtis.prepareStatement(sqlUzklausa);
  42.  
  43. prepSt.setString(1, prekesPavadinimas);
  44. prepSt.setDouble(2, prekesKaina);
  45. prepSt.setString(3, prekesAprasas);
  46.  
  47. prepSt.executeUpdate();
  48.  
  49. prepSt.close();
  50. jungtis.close();
  51.  
  52. PrintWriter out = resp.getWriter();
  53. out.println("Idejome duomenu baze");
  54. }
  55. catch (Exception e)
  56. {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61.  
RAW Paste Data