Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FlexiWare;
- import java.sql.*;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class ConexionSQL {
- Connection co;
- Statement stm;
- Double velo1;
- Double velo2;
- Double velo3;
- Date historico;
- SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
- public ConexionSQL(String maquina, int mes,int año) throws ClassNotFoundException, SQLException{
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
- Statement stm = co.createStatement();
- }
- catch(ClassNotFoundException exc){
- }
- catch(SQLException ex){
- Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
- }
- }
- public void calcularVelocidad(String maquina, int mes,int año) throws ParseException{
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
- Statement stm2 = co.createStatement();
- ResultSet rs2 = stm2.executeQuery("SELECT*\n" +
- "FROM pedidos2\n" +
- "WHERE pedidos2.codMaquina = "+"'"+maquina+"'"+"\n" +
- "AND pedidos2.estado=\"TRM\"\n" +
- "AND MONTH(pedidos2.fechaEntrega)>="+mes+"\n" +
- "AND YEAR(pedidos2.fechaEntrega)>="+año);
- int productosTotal =0;
- double tiempoProcesoTotal=0.0;
- while(rs2.next()){
- productosTotal+=rs2.getInt(5);
- tiempoProcesoTotal+=rs2.getFloat(9);
- }
- double promedio = productosTotal/tiempoProcesoTotal;
- System.out.println(promedio + "productos/hora");
- PreparedStatement agregar = co.prepareStatement("UPDATE maquinas SET maquinas.veloMaquina ="+promedio+" WHERE maquinas.codMaquina ="+"'"+maquina+"'");
- agregar.executeUpdate();
- }
- catch(ClassNotFoundException exc){
- }
- catch(SQLException ex){
- Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
- }
- }
- public void secuenciarPedidos(String maquina,Date fecha, String hora) throws SQLException, ParseException{
- ArrayList<PedidoSQL> noProgramados = new ArrayList<>();
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection co = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
- Connection co2 = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
- Connection co3 = DriverManager.getConnection("jdbc:mysql://localhost/basedatos","root","");
- //stm2 conexion NPG
- Statement stm2 = co.createStatement();
- //Obtener Velocidad Maquina
- Statement stm3 = co2.createStatement();
- //stm4 conexion PRG
- Statement stm4 = co3.createStatement();
- ResultSet rs3 = stm3.executeQuery("SELECT* FROM maquinas WHERE maquinas.codMaquina="+"'"+maquina+"'");
- while(rs3.next()){
- velo1 = rs3.getDouble(2);
- }
- //Obtener pedidos NPR de la maquina
- ResultSet rs2 = stm2.executeQuery("SELECT*\n" +
- "FROM pedidos2\n" +
- "WHERE pedidos2.codMaquina ="+"'"+maquina+"'"+"\n" +
- "AND pedidos2.estado=\"NPR\"");
- //Agregar Pedidos No programados a Lista de Pedidos noProgramados
- while(rs2.next()){
- PedidoSQL agregar = new PedidoSQL();
- agregar.setCodPedido(rs2.getString(1));
- agregar.setFechaEntrega(rs2.getDate(7));
- agregar.setCantidad(rs2.getString(5));
- int producto = Integer.parseInt(agregar.getCantidad());
- Double resultado = producto/velo1;
- agregar.setTiempoEstimado(Double.toString(resultado));
- agregar.setDescripcion(rs2.getString(4));
- noProgramados.add(agregar);
- }
- //Obtener pedidos PRG de la maquina
- ResultSet rs4 = stm4.executeQuery("SELECT*\n" +
- "FROM pedidos2\n" +
- "WHERE pedidos2.codMaquina ="+"'"+maquina+"'"+"\n" +
- "AND pedidos2.estado=\"PRG\"");
- //Agregar Pedidos No programados a Lista de Pedidos noProgramados
- while(rs4.next()){
- PedidoSQL agregar = new PedidoSQL();
- agregar.setCodPedido(rs4.getString(1));
- agregar.setFechaEntrega(rs4.getDate(7));
- agregar.setCantidad(rs4.getString(5));
- int producto = Integer.parseInt(agregar.getCantidad());
- Double resultado = producto/velo1;
- agregar.setTiempoEstimado(Double.toString(resultado));
- agregar.setDescripcion(rs4.getString(4));
- noProgramados.add(agregar);
- }
- //Ordenar Arreglo Via primer Criterio
- PedidoSQL aux3 = null;
- int posi = noProgramados.size();
- for (int i = 1; i < posi; i++){
- for (int j = posi- 1; j >= i; j--){
- Date fecha1= (Date) noProgramados.get(j).getFechaEntrega();
- Date fecha2= (Date) noProgramados.get(j-1).getFechaEntrega();
- if(fecha1.compareTo(fecha2)==-1){
- aux3 = noProgramados.get(j);
- int nuevo = j-1;
- noProgramados.set(j, noProgramados.get(j-1));
- noProgramados.set(j-1, aux3);
- }
- }
- }
- //Ordenar Arreglo via segundo criterio
- PedidoSQL aux4 = null;
- int posi2 = noProgramados.size();
- for (int i = 1; i < posi; i++){
- for (int j = posi- 1; j >= i; j--){
- Date fecha1= (Date) noProgramados.get(j).getFechaEntrega();
- Date fecha2= (Date) noProgramados.get(j-1).getFechaEntrega();
- Double Cantidad1 = Double.parseDouble(noProgramados.get(j).getCantidad());
- Double Cantidad2 = Double.parseDouble(noProgramados.get(j-1).getCantidad());
- if(fecha1.compareTo(fecha2)==0 ){
- if(Cantidad1<Cantidad2){
- aux3 = noProgramados.get(j);
- int nuevo = j-1;
- noProgramados.set(j, noProgramados.get(j-1));
- noProgramados.set(j-1, aux3);
- }
- }
- }
- }
- //Creacion del calendario
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- Calendar calendar = new GregorianCalendar();
- calendar.setTime(fecha);
- int minutos = Integer.parseInt(hora.substring(2, 3));
- int horas = Integer.parseInt(hora.substring(0, 2));
- calendar.add(Calendar.MINUTE,minutos);
- calendar.add(Calendar.HOUR, horas);
- for(int i=0;i<noProgramados.size();i++){
- java.util.Date fechaSalida = calendar.getTime();
- noProgramados.get(i).setFechaInicio(sdf.format(fechaSalida).toString());
- calendar.add(Calendar.MINUTE, noProgramados.get(i).getMinutos());
- calendar.add(Calendar.HOUR, noProgramados.get(i).getHoras());
- }
- for(int i=0;i<noProgramados.size();i++){
- System.out.println(noProgramados.get(i).getCodPedido()+"---"+noProgramados.get(i).getFechaEntrega()+"---"+noProgramados.get(i).getFechaInicio());
- }
- //Agregar a la base de datos
- if(maquina.equals("M1")){
- String query = "INSERT INTO maquina1 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
- PreparedStatement st = co.prepareStatement(query);
- for(int i=0;i<noProgramados.size();i++){
- st.setString (1, noProgramados.get(i).getCodPedido());
- st.setString (2, noProgramados.get(i).getFechaEntrega().toString());
- st.setString (3, noProgramados.get(i).getCantidad());
- st.setDouble (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
- st.setString (5, noProgramados.get(i).getDescripcion());
- st.setString (6, noProgramados.get(i).getFechaInicio());
- st.executeUpdate();
- }
- }
- if(maquina.equals("M2")){
- String query2 = "INSERT INTO maquina2 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
- PreparedStatement st2 = co.prepareStatement(query2);
- for(int i=0;i<noProgramados.size();i++){
- st2.setString (1, noProgramados.get(i).getCodPedido());
- st2.setString (2, noProgramados.get(i).getFechaEntrega().toString());
- st2.setString (3, noProgramados.get(i).getCantidad());
- st2.setDouble (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
- st2.setString (5, noProgramados.get(i).getDescripcion());
- st2.setString (6, noProgramados.get(i).getFechaInicio());
- st2.executeUpdate();
- }
- }
- if(maquina.equals("M3")){
- String query3 = "INSERT INTO maquina3 (codPedido, fechaEntrega, cantidad, tiempoEstimado, descripcion, fechaInicio) values (?, ?, ?, ?, ?, ?)";
- PreparedStatement st3 = co.prepareStatement(query3);
- for(int i=0;i<noProgramados.size();i++){
- st3.setString (1, noProgramados.get(i).getCodPedido());
- st3.setString (2, noProgramados.get(i).getFechaEntrega().toString());
- st3.setString (3, noProgramados.get(i).getCantidad());
- st3.setDouble (4, Double.parseDouble(noProgramados.get(i).getTiempoEstimado()));
- st3.setString (5, noProgramados.get(i).getDescripcion());
- st3.setString (6, noProgramados.get(i).getFechaInicio());
- st3.executeUpdate();
- }
- }
- }
- catch(ClassNotFoundException exc){
- }
- catch(SQLException ex){
- Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE,null,ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement