/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;


import java.sql.*;
import javax.swing.JOptionPane;
import java.sql.PreparedStatement;
import java.io.File;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.derby.jdbc.ClientDriver;

/**
 *
 * @author chuen
 */
public class conexion {

  Connection con = null;
  public static boolean ingresado = false;
   public Connection getConexion() {
       
   try{
       
       
       Class.forName("org.apache.derby.jdbc.ClientDriver");
//       con = DriverManager.getConnection("jdbc:derby://localhost:1527/ARQL");
       con = DriverManager.getConnection("jdbc:derby:.\\ARQL");
       

   }catch(Exception ex){
       JOptionPane.showMessageDialog(null, "Error en la conexion: " + ex.getMessage());
   }    
     return con;
   }
   public Connection testConexion() {
       
   try{
       
       
       Class.forName("org.apache.derby.jdbc.ClientDriver");
//       con = DriverManager.getConnection("jdbc:derby://localhost:1527/ARQL");
       con = DriverManager.getConnection("jdbc:derby:.\\ARQL");

       JOptionPane.showMessageDialog(null, "La conexión se ha realizado correctamente");
   }catch(Exception ex){
       JOptionPane.showMessageDialog(null, "Error en la conexion: " + ex.getMessage());
   }    
     return con;
   }
   
public ResultSet consulta (String consulta){
    
    ResultSet rs = null;
    
    try{
        PreparedStatement ps;
        ps =  con.prepareStatement(consulta);
        rs = ps.executeQuery();

        
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, "Error en la consulta: " + ex.getMessage());
    }
    return rs;
}
public int insert (String consulta){
ingresado=false; 
    ResultSet rs = null;
int resp = 0;    
    try{
        PreparedStatement ps;
        ps = con.prepareStatement(consulta);
        

        resp = ps.executeUpdate();
        if (resp > 0) {
            JOptionPane.showMessageDialog(null, "Registro ingresado");
            ingresado=true;
        }else{
            JOptionPane.showMessageDialog(null, "Registro no ingresado. Intente nuevamente.");
            ingresado=false;
        }
        
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, "Error en la consulta: " + ex.getMessage());
    }
    
    return resp;
    }
public void apagar() throws SQLException{
  try{
  DriverManager.getConnection("jdbc:derby:;shutdown=true");
//  DriverManager.getConnection("jdbc:derby:Prueba;shutdown=true");
    }catch(SQLException ex){
//        JOptionPane.showMessageDialog(null, "Error en cerrar sesion: " + ex.getMessage());
    }
//DriverManager.getConnection("jdbc:derby:.\\Prueba;shutdown=true");
}

public void closeconnection() throws SQLException{
     if (con != null) {
    con.close();
  }
  }

public int eliminar_actualizar (String sqleliminar){
    
    ResultSet rs = null;
    int confirm=0;
    
    try{
        PreparedStatement ps;
        ps =  con.prepareStatement(sqleliminar);
        confirm = ps.executeUpdate();
       

        
    }catch(Exception ex){
        JOptionPane.showMessageDialog(null, "Error en la consulta: " + ex.getMessage());
    }
    return confirm;
    
}
    public  void backUpDatabaseautom()throws SQLException
{

try {
//    java.text.SimpleDateFormat todaysDate = 
//        new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy_MM_dd HH_mm_ss");
    String fecha = dtf2.format(LocalDateTime.now());
    System.out.println("fecha"+ fecha);
    String backupdirectory = ".\\Backups\\Syst_" + fecha;
    File folder = new File(backupdirectory);
    if (!folder.exists()){
    folder.mkdirs();
}
    
    
    
    CallableStatement cs = con.prepareCall("CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE(?)"); 
    cs.setString(1, backupdirectory);
    cs.execute(); 
    cs.close();
    JOptionPane.showMessageDialog(null, "Se realizó el backup correctamente en: "+ backupdirectory);
    } catch (Exception e) {
   JOptionPane.showMessageDialog(null, "No se pudo realizar el backup: "+ e);
   
    }
}
}

