Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package admin;
- import db_info.Info;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import login.Login;
- /**
- *
- * @author KOMP
- */
- class MyClass4{
- public static int down(int index){
- if(index>1) index--;
- return index;
- }
- public static int upp(int index,int a){
- if(index<a) index++;
- return index;
- }
- }
- public class OrderA extends HttpServlet {
- void delete_amount(String string_products, int which_contract) throws SQLException{
- Connection con =null;
- Statement statement = null;
- Statement statement2;
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+Info.database_name, "root", "root");
- statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- statement2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- ResultSet products = statement.executeQuery("SELECT * FROM product");
- String[] tab = string_products.split(",");
- int length = tab.length;
- while(products.next()){
- for(int i=0;i<length;i++){
- if(products.getString("id_product").equals(tab[i])){
- int amount = Integer.parseInt(products.getString("amount"))-1;
- statement2.executeUpdate("UPDATE product "
- + "SET amount="+amount+""
- + "WHERE id_product like "+tab[i]+";");
- }
- }
- }
- }
- /**
- * Processes requests for both HTTP
- * <code>GET</code> and
- * <code>POST</code> methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(OrderA.class.getName()).log(Level.SEVERE, null, ex);
- }
- String table_name = "contractdone";
- try {
- Connection con =null;
- Statement statement = null;
- Statement statement2;
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+Info.database_name, "root", "root");
- statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- ResultSet result = statement.executeQuery("SELECT * FROM "+table_name);
- ServletContext ctx = this.getServletContext();
- RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
- request.setAttribute("result_order",result);
- request.setAttribute("pointer",1);
- disp.forward(request, response);
- } catch (SQLException ex) {
- Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP
- * <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP
- * <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- if(request.getParameter("down")!=null){
- try {
- Connection con =null;
- Statement statement = null;
- Statement statement2;
- Class.forName("com.mysql.jdbc.Driver");
- HttpSession ses = request.getSession();
- int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
- statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
- request.setAttribute("result_order",result);
- request.setAttribute("pointer",MyClass.down(index_back));
- ServletContext ctx = this.getServletContext();
- RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
- disp.forward(request, response);
- } catch (SQLException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- if(request.getParameter("up")!=null){
- try {
- Connection con =null;
- Statement statement = null;
- Statement statement2;
- Class.forName("com.mysql.jdbc.Driver");
- HttpSession ses = request.getSession();
- int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
- statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
- ResultSet rs = result;
- rs.last();
- int a = rs.getRow();
- request.setAttribute("result_order",result);
- request.setAttribute("pointer",MyClass.upp(index_back,a));
- ServletContext ctx = this.getServletContext();
- RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
- disp.forward(request, response);
- } catch (SQLException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- if(request.getParameter("edit")!=null){
- try {
- Connection con =null;
- Statement statement = null;
- Statement statement2;
- Class.forName("com.mysql.jdbc.Driver");
- HttpSession ses = request.getSession();
- int index_back = Integer.parseInt(ses.getAttribute("ind").toString());
- int which = Integer.parseInt(request.getParameter("id_contr"));
- int id_user = Integer.parseInt(request.getParameter("id_user"));
- Double suma = Double.parseDouble(request.getParameter("sum"));
- String status = request.getParameter("status");
- String id_prod = request.getParameter("id_prod");
- String products = request.getParameter("id_prod");
- con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "root");
- statement = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- statement2 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
- int i = statement.executeUpdate("UPDATE contractdone "
- + "SET id_user="+id_user+", suma="+suma+", status='"+status+"', products="+id_prod+""
- + "WHERE id_contract like "+which+";");
- if(i!=0) System.out.println("Zedytowano!");
- else System.out.println("Cos nie tak!");
- if(status.equals("Zrealizowano")){
- delete_amount(products,which);
- }
- ServletContext ctx = this.getServletContext();
- RequestDispatcher disp = ctx.getRequestDispatcher("/admin.jsp");
- ResultSet result = statement.executeQuery("SELECT * FROM contractdone");
- request.setAttribute("result_order",result);
- request.setAttribute("pointer",index_back);
- disp.forward(request, response);
- } catch (SQLException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Products.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement