MySQL - Problema con un SELECT

 
Vista:
sin imagen de perfil
Val: 13
Ha disminuido su posición en 3 puestos en MySQL (en relación al último mes)
Gráfica de MySQL

Problema con un SELECT

Publicado por Juan (8 intervenciones) el 20/08/2018 18:26:10
Hola a todos. Tengo una función que me devuelve una lista.
Le tengo que mandar a la función el tipo de filtro y un String.
El caso es que ahora quiero hacer una consulta pero le tengo que mandar un Integer.
Hay alguna forma de convertir el String que le envió en un Integer para cuando haga la consulta?Seria para FILTRAR_IDENTIFICADOR.El Identificador en la BD es un integer y si lo mando como un String no me encuentra nada.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
private static final String INSERT = "INSERT INTO ALQUILER(NIF,IDENTIFICADOR,FECHA) VALUES (?,?,?)";
private static final String DELETE = "DELETE FROM ALQUILER WHERE IDENTIFICADOR = ?";
private static final String QUERY = "SELECT ALQUILER.NIF, ALQUILER.IDENTIFICADOR, ALQUILER.FECHA, "
        + "ARTICULOS.IDENTIFICADOR, ARTICULOS.TITULO, ARTICULOS.ADULTOS, ARTICULOS.ALQUILADO, "
        + "CLIENTES.NIF, CLIENTES.NOMBRE, CLIENTES.APELLIDOS, CLIENTES.NACIMIENTO "
        + "FROM ALQUILER "
        + "INNER JOIN ARTICULOS ON ALQUILER.IDENTIFICADOR = ARTICULOS.IDENTIFICADOR "
        + "INNER JOIN CLIENTES ON ALQUILER.NIF = CLIENTES.NIF";
 
public static final int FILTRAT_NOMBRE = 0;
public static final int FILTRAT_APELLIDOS = 1;
public static final int FILTRAT_DNI = 2;
public static final int FILTRAR_IDENTIFICADOR = 3;
public static final int FILTRAR_FUERA_PLAZO = 4;
 
private static final String[] QUERY_FILTROS = {
    QUERY + " WHERE CLIENTES.NOMBRE = ?",
    QUERY + " WHERE CLIENTES.APELLIDOS = ?",
    QUERY + " WHERE CLIENTES.NIF = ?",
    QUERY + " WHERE ALQUILER.IDENTIFICADOR = ?"
    QUERY + " WHERE DATEDIFF(CURDATE(),ALQUILER.FECHA) > 3"
};
 
public static HashMap<String, Alquiler> filtrarAlquileres(int tipoFiltro, String valor) throws Exception {
    DataSource data = null;
    ResultSet rs = null;
    PreparedStatement st;
    st = null;
 
    HashMap<String, Alquiler> hashAlquileres = new HashMap<String, Alquiler>();
 
    try {
        data = new DataSource();
        st = data.getStatement(QUERY_FILTROS[tipoFiltro]);
        st.setString(1, valor);
        rs = data.ejecutarSelect(st);
        while (rs.next()) {
            int identificador = rs.getInt(4);
            String titulo = rs.getString(5);
            boolean mas18 = rs.getBoolean(6);
            boolean alquilado = rs.getBoolean(7);
            Articulo objArticulo = new Articulo(identificador, titulo, mas18,alquilado);
 
            String nif = rs.getString(8);
            String nombre = rs.getString(9);
            String apellido = rs.getString(10);
            Date nacimiento = rs.getDate(11);
            Cliente objCliente =  new Cliente(nif,nombre,apellido, nacimiento);
            Date fechaAlquiler = rs.getDate(3);
            Alquiler objAlquiler = new Alquiler(objCliente,objArticulo, fechaAlquiler);
            hashAlquileres.put(objAlquiler.getCliente().getNif(), objAlquiler);
        }
        rs.close();
        return hashAlquileres;
    } catch (SQLException ex) {
        System.err.println(ex.getMessage());
        throw new Exception(ex.getMessage());
    } finally {
        if (rs != null) {
            rs.close();
        }
        if (st != null) {
            st.close();
        }
        if (data != null) {
            data.cerrarConexion();
        }
    }
}

Muchas Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 21
Ha mantenido su posición en MySQL (en relación al último mes)
Gráfica de MySQL

Problema con un SELECT

Publicado por Fabricio (7 intervenciones) el 09/10/2018 16:48:35
Buenas: Hace un tiempo vi algo de base de datos con java.
No se si te funcionará, pero para convertir a entero un string tienes Integer.parseInt(myString) Por otro lado, en la clase PreparedStatement tienes el método setInt. Por lo que te quedaría algo así

....
st.setInt(1, Integer.parseInt(valor));
.....

(mr google tiene las respuestas, saludos)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar