Advertisement
Guest User

Untitled

a guest
Jul 5th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.51 KB | None | 0 0
  1. package FlexiWare;
  2.  
  3. import java.sql.*;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.ArrayList;
  7. import java.util.Calendar;
  8. import java.util.GregorianCalendar;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. public class ConexionSQL {
  12.     Connection co;
  13.     Statement stm;
  14.     Double velo1;
  15.     Double velo2;
  16.     Double velo3;
  17.     Date historico;
  18.     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  19.     public ConexionSQL(String maquina, int mes,int año) throws ClassNotFoundException, SQLException{
  20.         try{
  21.             Class.forName("com.mysql.jdbc.Driver");
  22.             Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
  23.             Statement stm = co.createStatement();
  24.            
  25.         }
  26.        
  27.         catch(ClassNotFoundException exc){
  28.         }
  29.         catch(SQLException ex){
  30.             Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
  31.            
  32.         }
  33.        
  34.        
  35.        
  36.     }
  37.     public void calcularVelocidad(String maquina, int mes,int año) throws ParseException{
  38.         try{
  39.             Class.forName("com.mysql.jdbc.Driver");
  40.             Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
  41.             Statement stm2 = co.createStatement();
  42.            
  43.            
  44.             ResultSet rs2 = stm2.executeQuery("SELECT*\n" +
  45.                                             "FROM pedidos2\n" +
  46.                                             "WHERE pedidos2.codMaquina = "+"'"+maquina+"'"+"\n" +
  47.                                             "AND pedidos2.estado=\"TRM\"\n" +
  48.                                             "AND MONTH(pedidos2.fechaEntrega)>="+mes+"\n" +
  49.                                             "AND YEAR(pedidos2.fechaEntrega)>="+año);
  50.            
  51.             int productosTotal =0;
  52.             double tiempoProcesoTotal=0.0;
  53.             while(rs2.next()){
  54.                 productosTotal+=rs2.getInt(5);
  55.                 tiempoProcesoTotal+=rs2.getFloat(9);
  56.             }
  57.             double promedio = productosTotal/tiempoProcesoTotal;
  58.             System.out.println(promedio + "productos/hora");
  59.             PreparedStatement agregar = co.prepareStatement("UPDATE maquinas SET maquinas.veloMaquina ="+promedio+" WHERE maquinas.codMaquina ="+"'"+maquina+"'");
  60.             agregar.executeUpdate();
  61.                  
  62.         }
  63.        
  64.         catch(ClassNotFoundException exc){
  65.         }
  66.         catch(SQLException ex){
  67.             Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
  68.            
  69.         }
  70.  
  71.     }
  72.    
  73.    
  74.     public void secuenciarPedidos(String maquina,Date fecha, String hora) throws SQLException, ParseException{
  75.         ArrayList<PedidoSQL> noProgramados = new ArrayList<>();
  76.        
  77.  
  78.         try{
  79.             Class.forName("com.mysql.jdbc.Driver");
  80.             Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
  81.             Connection co2 = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
  82.             Connection co3 = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
  83.  
  84.             //stm2 conexion NPG
  85.             Statement stm2 = co.createStatement();    
  86.             //Obtener Velocidad Maquina
  87.             Statement stm3 = co2.createStatement();
  88.             //stm4 conexion PRG
  89.             Statement stm4 = co3.createStatement();
  90.            
  91.            
  92.             ResultSet rs3 = stm3.executeQuery("SELECT* FROM maquinas WHERE maquinas.codMaquina="+"'"+maquina+"'");
  93.             while(rs3.next()){
  94.                 velo1 = rs3.getDouble(2);
  95.             }
  96.             //Obtener pedidos NPR de la maquina
  97.             ResultSet rs2 = stm2.executeQuery("SELECT*\n" +
  98.                                             "FROM pedidos2\n" +
  99.                                             "WHERE pedidos2.codMaquina ="+"'"+maquina+"'"+"\n" +
  100.                                             "AND pedidos2.estado=\"NPR\"");
  101.             //Agregar Pedidos No programados a Lista de Pedidos noProgramados
  102.             while(rs2.next()){
  103.                 PedidoSQL agregar = new PedidoSQL();
  104.                 agregar.setCodPedido(rs2.getString(1));
  105.                 agregar.setFechaEntrega(rs2.getDate(7));
  106.                 agregar.setCantidad(rs2.getString(5));
  107.                 int producto = Integer.parseInt(agregar.getCantidad());
  108.                 Double resultado = producto/velo1;
  109.                 agregar.setTiempoEstimado(Double.toString(resultado));
  110.                 agregar.setDescripcion(rs2.getString(4));
  111.                 noProgramados.add(agregar);
  112.             }
  113.             //Obtener pedidos PRG de la maquina
  114.             ResultSet rs4 = stm4.executeQuery("SELECT*\n" +
  115.                                             "FROM pedidos2\n" +
  116.                                             "WHERE pedidos2.codMaquina ="+"'"+maquina+"'"+"\n" +
  117.                                             "AND pedidos2.estado=\"PRG\"");
  118.             //Agregar Pedidos No programados a Lista de Pedidos noProgramados
  119.             while(rs4.next()){
  120.                 PedidoSQL agregar = new PedidoSQL();
  121.                 agregar.setCodPedido(rs4.getString(1));
  122.                 agregar.setFechaEntrega(rs4.getDate(7));
  123.                 agregar.setCantidad(rs4.getString(5));
  124.                 int producto = Integer.parseInt(agregar.getCantidad());
  125.                 Double resultado = producto/velo1;
  126.                 agregar.setTiempoEstimado(Double.toString(resultado));
  127.                 agregar.setDescripcion(rs4.getString(4));
  128.                 noProgramados.add(agregar);
  129.             }
  130.                        
  131.             //Ordenar Arreglo Via primer Criterio
  132.             PedidoSQL aux3 = null;
  133.             int posi = noProgramados.size();
  134.             for (int i = 1; i < posi; i++){
  135.                 for (int j = posi- 1; j >= i; j--){
  136.                     Date fecha1= (Date) noProgramados.get(j).getFechaEntrega();
  137.                     Date fecha2= (Date) noProgramados.get(j-1).getFechaEntrega();
  138.                         if(fecha1.compareTo(fecha2)==-1){  
  139.                     aux3 = noProgramados.get(j);
  140.                                         int nuevo = j-1;
  141.                     noProgramados.set(j, noProgramados.get(j-1));
  142.                     noProgramados.set(j-1, aux3);
  143.                         }
  144.                 }
  145.             }
  146.            
  147.             //Ordenar Arreglo via segundo criterio
  148.             PedidoSQL aux4 = null;
  149.             int posi2 = noProgramados.size();
  150.             for (int i = 1; i < posi; i++){
  151.             for (int j = posi- 1; j >= i; j--){
  152.                
  153.                 Date fecha1= (Date) noProgramados.get(j).getFechaEntrega();
  154.                 Date fecha2= (Date) noProgramados.get(j-1).getFechaEntrega();
  155.                 Double Cantidad1 = Double.parseDouble(noProgramados.get(j).getCantidad());
  156.                 Double Cantidad2 = Double.parseDouble(noProgramados.get(j-1).getCantidad());
  157.                 if(fecha1.compareTo(fecha2)==0 ){
  158.                     if(Cantidad1<Cantidad2){
  159.                        
  160.                     aux3 = noProgramados.get(j);
  161.                                         int nuevo = j-1;
  162.                     noProgramados.set(j, noProgramados.get(j-1));
  163.                     noProgramados.set(j-1, aux3);
  164.                 }
  165.             }
  166.         }
  167.             }
  168.            
  169.             //Creacion del calendario
  170.             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  171.             Calendar calendar = new GregorianCalendar();
  172.             calendar.setTime(fecha);
  173.             int minutos = Integer.parseInt(hora.substring(2, 3));
  174.             int horas = Integer.parseInt(hora.substring(0, 2));
  175.             calendar.add(Calendar.MINUTE,minutos);
  176.             calendar.add(Calendar.HOUR, horas);
  177.        
  178.             for(int i=0;i<noProgramados.size();i++){
  179.                 java.util.Date fechaSalida = calendar.getTime();
  180.                 noProgramados.get(i).setFechaInicio(sdf.format(fechaSalida).toString());
  181.                 calendar.add(Calendar.MINUTE, noProgramados.get(i).getMinutos());
  182.                 calendar.add(Calendar.HOUR, noProgramados.get(i).getHoras());            
  183.             }
  184.            
  185.             for(int i=0;i<noProgramados.size();i++){
  186.                 System.out.println(noProgramados.get(i).getCodPedido()+"---"+noProgramados.get(i).getFechaEntrega()+"---"+noProgramados.get(i).getFechaInicio());
  187.             }
  188.             //Agregar a la base de datos
  189.             if(maquina.equals("M1")){
  190.                 String query = "INSERT INTO maquina1 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
  191.                 PreparedStatement st = co.prepareStatement(query);
  192.                 for(int i=0;i<noProgramados.size();i++){
  193.                
  194.                     st.setString    (1, noProgramados.get(i).getCodPedido());
  195.                     st.setString    (2, noProgramados.get(i).getFechaEntrega().toString());
  196.                     st.setString    (3, noProgramados.get(i).getCantidad());
  197.                     st.setDouble    (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
  198.                     st.setString    (5, noProgramados.get(i).getDescripcion());
  199.                     st.setString    (6, noProgramados.get(i).getFechaInicio());
  200.                     st.executeUpdate();
  201.                 }
  202.             }
  203.            
  204.             if(maquina.equals("M2")){
  205.                 String query2 = "INSERT INTO maquina2 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
  206.                 PreparedStatement st2 = co.prepareStatement(query2);
  207.                 for(int i=0;i<noProgramados.size();i++){
  208.                
  209.                     st2.setString    (1, noProgramados.get(i).getCodPedido());
  210.                     st2.setString    (2, noProgramados.get(i).getFechaEntrega().toString());
  211.                     st2.setString    (3, noProgramados.get(i).getCantidad());
  212.                     st2.setDouble    (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
  213.                     st2.setString    (5, noProgramados.get(i).getDescripcion());
  214.                     st2.setString    (6, noProgramados.get(i).getFechaInicio());
  215.                     st2.executeUpdate();
  216.                 }
  217.             }
  218.             if(maquina.equals("M3")){
  219.                 String query3 = "INSERT INTO maquina3 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
  220.                 PreparedStatement st3 = co.prepareStatement(query3);
  221.                 for(int i=0;i<noProgramados.size();i++){
  222.                
  223.                     st3.setString    (1, noProgramados.get(i).getCodPedido());
  224.                     st3.setString    (2, noProgramados.get(i).getFechaEntrega().toString());
  225.                     st3.setString    (3, noProgramados.get(i).getCantidad());
  226.                     st3.setDouble    (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
  227.                     st3.setString    (5, noProgramados.get(i).getDescripcion());
  228.                     st3.setString    (6, noProgramados.get(i).getFechaInicio());
  229.                     st3.executeUpdate();
  230.                 }
  231.                
  232.             }
  233.            
  234.         }
  235.  
  236.        
  237.         catch(ClassNotFoundException exc){
  238.         }
  239.         catch(SQLException ex){
  240.             Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
  241.            
  242.         }
  243.        
  244.        
  245.        
  246.     }
  247.  
  248.    
  249.    
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement