Raizekas

PrekesPaieskaServlet

Feb 23rd, 2021
66
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.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14.  
  15. @WebServlet(
  16. name = "prekesPaieskaServlet",
  17. urlPatterns = "/PrekesPaieska"
  18. )
  19. public class PrekesPaieskaServlet extends HttpServlet
  20. {
  21. @Override
  22. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
  23. {
  24. PrintWriter out = resp.getWriter();
  25.  
  26. String ieskomaPreke = req.getParameter("ieskoma-preke");
  27.  
  28. try
  29. {
  30. Connection jungtis = DuombazesSukurimas.sukurtiDuomenuBaze();
  31.  
  32. // String sqlUzklausa = "SELECT * FROM prekes"; // testine uzklausa, su kuria isvesime viska
  33.  
  34. String sqlUzklausa = "SELECT * FROM prekes WHERE pav=?";
  35.  
  36. PreparedStatement prepSt = jungtis.prepareStatement(sqlUzklausa);
  37. prepSt.setString(1, ieskomaPreke);
  38.  
  39. ResultSet rezultatai = prepSt.executeQuery();
  40.  
  41. while (rezultatai.next())
  42. {
  43. String pav = rezultatai.getString("pav");
  44. double kaina = rezultatai.getDouble("kaina");
  45. String aprasas = rezultatai.getString("aprasas");
  46.  
  47. out.println(pav);
  48. out.println(kaina);
  49. out.println(aprasas);
  50. }
  51.  
  52. // Testas - isvesti visus lenteles irasus bei taip pat NEOPTIMALUS paieskos metodas - parsiunciant visus lenteles duomenis ir juose ieksant per Java
  53. /* while (rezultatai.next())
  54. {
  55. String pav = rezultatai.getString("pav");
  56. double kaina = rezultatai.getDouble("kaina");
  57. String aprasas = rezultatai.getString("aprasas");
  58.  
  59. if (ieskomaPreke.equals(pav))
  60. {
  61. out.println(pav);
  62. out.println(kaina);
  63. out.println(aprasas);
  64. }
  65. }*/
  66. }
  67. catch (SQLException e)
  68. {
  69. e.printStackTrace();
  70. }
  71.  
  72. out.println(ieskomaPreke);
  73.  
  74.  
  75.  
  76. }
  77. }
  78.  
RAW Paste Data