Advertisement
Guest User

Untitled

a guest
May 17th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.66 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package admin;
  6.  
  7. import db_info.Info;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.servlet.RequestDispatcher;
  18. import javax.servlet.ServletContext;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import javax.servlet.http.HttpSession;
  24. import login.Login;
  25.  
  26. /**
  27. *
  28. * @author KOMP
  29. */
  30.  
  31. class MyClass4{
  32.  
  33. public static int down(int index){
  34. if(index>1) index--;
  35.  
  36. return index;
  37. }
  38.  
  39. public static int upp(int index,int a){
  40. if(index<a) index++;
  41.  
  42. return index;
  43. }
  44. }
  45.  
  46. public class OrderA extends HttpServlet {
  47.  
  48. void delete_amount(String string_products, int which_contract) throws SQLException{
  49. Connection con =null;
  50. Statement statement = null;
  51. Statement statement2;
  52.  
  53. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+Info.database_name, "root", "root");
  54. statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  55. statement2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  56.  
  57. ResultSet products = statement.executeQuery("SELECT * FROM product");
  58.  
  59. String[] tab = string_products.split(",");
  60.  
  61. int length = tab.length;
  62.  
  63. while(products.next()){
  64. for(int i=0;i<length;i++){
  65. if(products.getString("id_product").equals(tab[i])){
  66. int amount = Integer.parseInt(products.getString("amount"))-1;
  67.  
  68. statement2.executeUpdate("UPDATE product "
  69. + "SET amount="+amount+""
  70. + "WHERE id_product like "+tab[i]+";");
  71.  
  72. }
  73. }
  74. }
  75.  
  76.  
  77. }
  78.  
  79. /**
  80. * Processes requests for both HTTP
  81. * <code>GET</code> and
  82. * <code>POST</code> methods.
  83. *
  84. * @param request servlet request
  85. * @param response servlet response
  86. * @throws ServletException if a servlet-specific error occurs
  87. * @throws IOException if an I/O error occurs
  88. */
  89. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  90. throws ServletException, IOException {
  91. response.setContentType("text/html;charset=UTF-8");
  92.  
  93. PrintWriter out = response.getWriter();
  94.  
  95. try {
  96. Class.forName("com.mysql.jdbc.Driver");
  97. } catch (ClassNotFoundException ex) {
  98. Logger.getLogger(OrderA.class.getName()).log(Level.SEVERE, null, ex);
  99. }
  100.  
  101. String table_name = "contractdone";
  102.  
  103. try {
  104. Connection con =null;
  105. Statement statement = null;
  106. Statement statement2;
  107.  
  108. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+Info.database_name, "root", "root");
  109. statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  110. ResultSet result = statement.executeQuery("SELECT * FROM "+table_name);
  111.  
  112. ServletContext ctx = this.getServletContext();
  113. RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
  114.  
  115. request.setAttribute("result_order",result);
  116. request.setAttribute("pointer",1);
  117.  
  118. disp.forward(request, response);
  119.  
  120. } catch (SQLException ex) {
  121. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  122. }
  123. }
  124.  
  125. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  126. /**
  127. * Handles the HTTP
  128. * <code>GET</code> method.
  129. *
  130. * @param request servlet request
  131. * @param response servlet response
  132. * @throws ServletException if a servlet-specific error occurs
  133. * @throws IOException if an I/O error occurs
  134. */
  135. @Override
  136. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  137. throws ServletException, IOException {
  138. processRequest(request, response);
  139. }
  140.  
  141. /**
  142. * Handles the HTTP
  143. * <code>POST</code> method.
  144. *
  145. * @param request servlet request
  146. * @param response servlet response
  147. * @throws ServletException if a servlet-specific error occurs
  148. * @throws IOException if an I/O error occurs
  149. */
  150. @Override
  151. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  152. throws ServletException, IOException {
  153. if(request.getParameter("down")!=null){
  154. try {
  155. Connection con =null;
  156. Statement statement = null;
  157. Statement statement2;
  158.  
  159. Class.forName("com.mysql.jdbc.Driver");
  160. HttpSession ses = request.getSession();
  161. int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
  162.  
  163. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
  164. statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  165.  
  166. ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
  167.  
  168. request.setAttribute("result_order",result);
  169. request.setAttribute("pointer",MyClass.down(index_back));
  170.  
  171. ServletContext ctx = this.getServletContext();
  172. RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
  173.  
  174. disp.forward(request, response);
  175.  
  176. } catch (SQLException ex) {
  177. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  178. } catch (ClassNotFoundException ex) {
  179. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  180. }
  181.  
  182.  
  183. }
  184. if(request.getParameter("up")!=null){
  185. try {
  186. Connection con =null;
  187. Statement statement = null;
  188. Statement statement2;
  189.  
  190. Class.forName("com.mysql.jdbc.Driver");
  191. HttpSession ses = request.getSession();
  192. int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
  193.  
  194. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
  195. statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  196.  
  197. ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
  198. ResultSet rs = result;
  199.  
  200. rs.last();
  201. int a = rs.getRow();
  202.  
  203. request.setAttribute("result_order",result);
  204. request.setAttribute("pointer",MyClass.upp(index_back,a));
  205.  
  206. ServletContext ctx = this.getServletContext();
  207. RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
  208.  
  209. disp.forward(request, response);
  210.  
  211. } catch (SQLException ex) {
  212. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  213. } catch (ClassNotFoundException ex) {
  214. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  215. }
  216. }
  217. if(request.getParameter("edit")!=null){
  218. try {
  219. Connection con =null;
  220. Statement statement = null;
  221. Statement statement2;
  222.  
  223. Class.forName("com.mysql.jdbc.Driver");
  224. HttpSession ses = request.getSession();
  225. int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
  226.  
  227. int which = Integer.parseInt(request.getParameter("id_contr"));
  228.  
  229. int id_user = Integer.parseInt(request.getParameter("id_user"));
  230. Double suma = Double.parseDouble(request.getParameter("sum"));
  231. String status = request.getParameter("status");
  232. String id_prod = request.getParameter("id_prod");
  233. String products = request.getParameter("id_prod");
  234.  
  235. con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
  236. statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  237. statement2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
  238.  
  239. int i = statement.executeUpdate("UPDATE contractdone "
  240. + "SET id_user="+id_user+", suma="+suma+", status='"+status+"', products="+id_prod+""
  241. + "WHERE id_contract like "+which+";");
  242.  
  243. if(i!=0) System.out.println("Zedytowano!");
  244. else System.out.println("Cos nie tak!");
  245.  
  246. if(status.equals("Zrealizowano")){
  247. delete_amount(products,which);
  248. }
  249.  
  250.  
  251. ServletContext ctx = this.getServletContext();
  252. RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
  253.  
  254. ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
  255.  
  256. request.setAttribute("result_order",result);
  257. request.setAttribute("pointer",index_back);
  258.  
  259. disp.forward(request, response);
  260.  
  261. } catch (SQLException ex) {
  262. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  263. } catch (ClassNotFoundException ex) {
  264. Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
  265. }
  266. }
  267. }
  268.  
  269. /**
  270. * Returns a short description of the servlet.
  271. *
  272. * @return a String containing servlet description
  273. */
  274. @Override
  275. public String getServletInfo() {
  276. return "Short description";
  277. }// </editor-fold>
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement