|
|
@ -1,6 +1,7 @@ |
|
|
|
package mx.gob.jumapacelaya.services; |
|
|
|
|
|
|
|
import java.sql.*; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
@ -18,14 +19,14 @@ public class DatabaseService { |
|
|
|
// Método para obtener los tipos de mantenimientos |
|
|
|
public List<String> getTiposDeMantenimientos() { |
|
|
|
List<String> tiposDeMantenimientos = new ArrayList<>(); |
|
|
|
String query = "SELECT * FROM TIPOMANT"; |
|
|
|
String query = "SELECT tipomantid FROM TIPOMANT"; |
|
|
|
|
|
|
|
try (Connection connection = DriverManager.getConnection("jdbc:mysql://mhdb.jumapacelaya.gob.mx:33006/Mantenimientos", "root", "mantenimientos"); |
|
|
|
try (Connection connection = getConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
|
ResultSet resultSet = statement.executeQuery(query)) { |
|
|
|
|
|
|
|
while (resultSet.next()) { |
|
|
|
tiposDeMantenimientos.add(resultSet.getString("NOMBRE")); |
|
|
|
tiposDeMantenimientos.add(resultSet.getString("TIPOMANTID")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -36,7 +37,7 @@ public class DatabaseService { |
|
|
|
// Método para obtener las nomenclaturas |
|
|
|
public String getNomenclatura(String tipoMantenimiento ) { |
|
|
|
String nomenclatura = ""; |
|
|
|
String query = "SELECT nomenclatura FROM TIPOMANT where NOMBRE = ?"; |
|
|
|
String query = "SELECT nomenclatura FROM TIPOMANT where TIPOMANTID = ?"; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
@ -58,14 +59,14 @@ public class DatabaseService { |
|
|
|
/* -------------- Metodo para obtener a los usuarios ---------------- */ |
|
|
|
public List<String> getUsuarios() { |
|
|
|
List<String> usuarios = new ArrayList<>(); |
|
|
|
String query = "select USUARIOID from USUARIOSFINAN"; |
|
|
|
String query = "select EMPLEADOID from USUARIOSFINAN"; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
|
ResultSet resultSet = statement.executeQuery(query)) { |
|
|
|
|
|
|
|
while (resultSet.next()) { |
|
|
|
usuarios.add(resultSet.getString("usuarioid")); |
|
|
|
usuarios.add(resultSet.getString("EMPLEADOID")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -77,14 +78,14 @@ public class DatabaseService { |
|
|
|
/* -------------- Metodo para obtener los departamentos ---------------- */ |
|
|
|
public List<String> getDepartamentos() { |
|
|
|
List<String> departamentos = new ArrayList<>(); |
|
|
|
String query = "select DESCRIPCION from DEPARTAMENTOSFINAN"; |
|
|
|
String query = "select DEPARTAMENTOID from DEPARTAMENTOSFINAN"; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
|
ResultSet resultSet = statement.executeQuery(query)) { |
|
|
|
|
|
|
|
while (resultSet.next()) { |
|
|
|
departamentos.add(resultSet.getString("descripcion")); |
|
|
|
departamentos.add(resultSet.getString("DEPARTAMENTOID")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -97,14 +98,14 @@ public class DatabaseService { |
|
|
|
/* -------------- Metodo para obtener los tipos de hardware ---------------- */ |
|
|
|
public List<String> getTiposHardware() { |
|
|
|
List<String> tiposHardware = new ArrayList<>(); |
|
|
|
String query = "select NOMBRETIPO from TIPOSHARDWARE"; |
|
|
|
String query = "select TIPOHARDWAREID from TIPOSHARDWARE"; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
Statement statement = connection.createStatement(); |
|
|
|
ResultSet resultSet = statement.executeQuery(query)) { |
|
|
|
|
|
|
|
while (resultSet.next()) { |
|
|
|
tiposHardware.add(resultSet.getString("nombretipo")); |
|
|
|
tiposHardware.add(resultSet.getString("TIPOHARDWAREID")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
@ -133,4 +134,73 @@ public class DatabaseService { |
|
|
|
} |
|
|
|
return nombreUsuario; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------- Metodos para insetar en la BD ---------------- */ |
|
|
|
// INSERTAR EN TABLA: MANTENIMINETOS |
|
|
|
public int insertarMantenimiento(LocalDate fecha, String tipoMantId, String departamentoId, String empleadoId) { |
|
|
|
String query = "INSERT INTO MANTENIMIENTOS (fecha, tipoMantId, departamentoId, empleadoId) VALUES (?, ?, ?, ?)"; |
|
|
|
int nuevoId = -1; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { |
|
|
|
|
|
|
|
preparedStatement.setDate(1, Date.valueOf(fecha)); |
|
|
|
preparedStatement.setString(2, tipoMantId); |
|
|
|
preparedStatement.setString(3, departamentoId); |
|
|
|
preparedStatement.setString(4, empleadoId); |
|
|
|
|
|
|
|
int rowsAffected = preparedStatement.executeUpdate(); |
|
|
|
if (rowsAffected > 0) { |
|
|
|
try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { |
|
|
|
if (generatedKeys.next()) { |
|
|
|
nuevoId = generatedKeys.getInt(1); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
System.err.println("Error al insertar mantenimiento: " + e.getMessage()); |
|
|
|
} |
|
|
|
return nuevoId; |
|
|
|
} |
|
|
|
|
|
|
|
// INSERTAR EN LA TABLA: HARDWAREDET |
|
|
|
public boolean insertarHardware(String tipoHardwareId, int numSerie, String modelo, int placa, int mantenimientoId) { |
|
|
|
String query = "INSERT INTO HARDWAREDET (tipoHardwareId, numSerie, modelo, placa, mantenimientoId) VALUES (?, ?, ?, ?, ?)"; |
|
|
|
boolean isInserted = false; |
|
|
|
|
|
|
|
try (Connection connection = getConnection(); |
|
|
|
PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
|
|
|
|
|
preparedStatement.setString(1, tipoHardwareId); |
|
|
|
preparedStatement.setInt(2, numSerie); |
|
|
|
preparedStatement.setString(3, modelo); |
|
|
|
preparedStatement.setInt(4, placa); |
|
|
|
preparedStatement.setInt(5, mantenimientoId); |
|
|
|
|
|
|
|
int rowsAffected = preparedStatement.executeUpdate(); |
|
|
|
isInserted = rowsAffected > 0; |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return isInserted; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getUltimoMantenimientoId() { |
|
|
|
int ultimoId = -1; |
|
|
|
try (Connection connection = getConnection()) { |
|
|
|
String query = "SELECT MAX(mantenimientoid) FROM MANTENIMIENTOS"; |
|
|
|
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { |
|
|
|
ResultSet resultSet = preparedStatement.executeQuery(); |
|
|
|
if (resultSet.next()) { |
|
|
|
ultimoId = resultSet.getInt(1); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return ultimoId; |
|
|
|
} |
|
|
|
} |