Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <%@ page import ="java.sql.*" %>
  3. <% Class.forName("com.mysql.jdbc.Driver"); %>
  4. <% String idprod = request.getParameter("idprod");%>
  5. <!DOCTYPE html>
  6. <html>
  7. <head>
  8. <title>JSP Page</title>
  9. </head>
  10. <body>
  11. <%!
  12. public class Actor{
  13. String URL = "jdbc:mysql://localhost:3306/mysql";
  14. String USERNAME = "root";
  15. String PASSWD = "";
  16. String em="";
  17. String pr="";
  18. String tempid="";
  19. Connection connection = null;
  20. PreparedStatement select= null;
  21. PreparedStatement select2= null;
  22. ResultSet resultSet = null;
  23. int a = 0;
  24. public Actor(String b){
  25. try{
  26. tempid=b;
  27. connection = DriverManager.getConnection(URL, USERNAME,PASSWD );
  28. select = connection.prepareStatement("SELECT quantity FROM products where id="+tempid );
  29. select2 = connection.prepareStatement("SELECT price FROM products where id="+tempid );
  30. }
  31. catch (SQLException e){
  32. e.printStackTrace();
  33. }
  34. }
  35. public int check(){
  36. try{
  37. resultSet = select.executeQuery();
  38.  
  39. }
  40. catch (SQLException e){
  41. e.printStackTrace();
  42. }
  43. return a;
  44. }
  45. public String getActors(){
  46.  
  47. try{
  48. resultSet = select.executeQuery();
  49. while (resultSet.next()) {
  50. em = resultSet.getString("quantity");
  51. }
  52. } catch (SQLException e){
  53. e.printStackTrace();
  54. }
  55. return em;
  56. }
  57. public String getPrice(){
  58.  
  59. try{
  60. resultSet = select2.executeQuery();
  61. while (resultSet.next()) {
  62. pr = resultSet.getString("price");
  63. }
  64. } catch (SQLException e){
  65. e.printStackTrace();
  66. }
  67. return pr;
  68. }
  69.  
  70. }
  71. %>
  72. <%
  73.  
  74. Actor actor = new Actor(idprod);
  75. String actors = actor.getActors();
  76.  
  77. int iloscsztuk= Integer.parseInt(actors);
  78. int ilosckupionych =Integer.parseInt(request.getParameter("howmany"));
  79. int wynik=iloscsztuk-ilosckupionych;
  80. actors = actor.getPrice();
  81. double cenajednostkowa=Double.parseDouble(actors);
  82. double cenalaczna=cenajednostkowa*Double.parseDouble(request.getParameter("howmany"));
  83.  
  84.  
  85. Class.forName("com.mysql.jdbc.Driver");
  86. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql",
  87. "root", "");
  88. Statement st = con.createStatement();
  89. //ResultSet rs;
  90. int i = st.executeUpdate("Update products set quantity="+wynik+" where id="+idprod);
  91. if (i > 0) {
  92. //session.setAttribute("userid", user);
  93. out.println("Kupiono "+ilosckupionych+" sztuk produktu za cene ");
  94. %>
  95. <form method="POST" action="index.jsp">
  96. <input type = "submit" value = "Kupuje i place" />
  97. </form>
  98.  
  99. <%
  100. } else {
  101. response.sendRedirect("index.jsp");
  102. }
  103.  
  104. %>
  105.  
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement