Could you help me find the error ?, marks me sql error [closed]

1
 /**
     * Descripcion: metodo para buscar un producto Recibe: recibe un objheto de
     * tipo producto Devuelve: un registro de tipo producto
     */
    public static Almacen buscarAlmacen(Almacen almac) {
        ResultSet rs;
        Usuarios usu = new Usuarios();
        Productos prod = new Productos();

        if (con != null) {
            try {
// consulta con condición 
                String sentecia = "SELECT alamcen.idAlamcen,\n"
                        + "                                          usuarios.nombre, \n"
                        + "                                          productos.nombreProducto,\n"
                        + "                                          alamcen.codigo\n"
                        + "                                          FROM alamcen\n"
                        + "                            INNER JOIN productos on productos.idProductos = alamcen.Productos_idProductos\n"
                        + "                            INNER JOIN usuarios on usuarios.idUsuarios = alamcen.Usuarios_idUsuarios"
                        //where condicion mediante el ccodigo recibido del objeto producto
                        + "WHERE codigo = " + almac.getCodigo() + "";
                PreparedStatement pst2;
                pst2 = con.prepareStatement(sentecia);
                rs = pst2.executeQuery();
// condicion: si la consulta arroja un registro entonces me llena el objeto
                if (rs.next()) {

                    almac.setIdAlmacen(rs.getInt(1));

                    usu.setNombre(rs.getString(2));
                    almac.setIdUsuario(usu);

                    prod.setNombreProducto(rs.getString(3));
                    almac.setIdProductos(prod);

                    almac.setCodigo(rs.getInt(4));
                }

            } catch (SQLException sqle) {
                System.out.println("Error al Buscar" + sqle.getMessage());
            }
        }
//OBJETO RESULTANTE

        return almac;

    }

    public static ArrayList<Almacen> getBuscarAlmacens(Almacen almac) {
        ArrayList<Almacen> listaAlmacen = new ArrayList<>();

        ResultSet rs;
        Statement st;
        Connection con = ConexionBD.obtenerConexion();
        if (con != null) {
            try {
                st = con.createStatement();
                rs = st.executeQuery("SELECT alamcen.idAlamcen,\n"
                        + "                                usuarios.nombre, \n"
                        + "                                productos.nombreProducto,\n"
                        + "                                alamcen.codigo\n"
                        + "                 FROM alamcen\n"
                        + "               INNER JOIN productos on productos.idProductos = alamcen.Productos_idProductos\n"
                        + "               INNER JOIN usuarios on usuarios.idUsuarios = alamcen.Usuarios_idUsuarios"
                        //where condicion mediante el ccodigo recibido del objeto producto
                        + "WHERE codigo = " + almac.getCodigo() + "");

                int i = 0;
                while (rs.next()) {
                    Almacen alma = new Almacen();
                    Usuarios usu = new Usuarios();
                    Productos prod = new Productos();

                    alma.setIdAlmacen(rs.getInt(1));

                    usu.setNombre(rs.getString(2));
                    alma.setIdUsuario(usu);

                    prod.setNombreProducto(rs.getString(3));
                    alma.setIdProductos(prod);

                    alma.setCodigo(rs.getInt(4));

                    listaAlmacen.add(i, alma);
                    i++;

                }
                st.close();
                rs.close();
            } catch (SQLException sqle) {
                System.out.println("Error" + sqle.getMessage());
            }
        }

        return listaAlmacen;
    }
    
asked by Uli Baeza 19.08.2017 в 03:37
source

1 answer

0

The database manager is not a text editor for you to place line breaks \n . You must substitute \n for a space where necessary and eliminate all \n .

You also use a prepared query, because in the current mode you are wasting one of the strengths of preparedStatement, which is to protect our SQL Injection queries. If where the code goes, someone writes this: X100; DROP TABLE cualquier-tabla; -- that way any malicious user could erase your tables and send much more dangerous queries.

To protect the query, in sentencia you substitute the value for which you want to filter by a question mark ? and then pass that value using the preparedStatement, using setString, setInt, setDate... according to the data type of that column.

            String sentecia = "SELECT alamcen.idAlamcen, "
                    + "usuarios.nombre, "
                    + "productos.nombreProducto, "
                    + "alamcen.codigo "
                    + "FROM alamcen "
                    + "INNER JOIN productos on productos.idProductos = alamcen.Productos_idProductos "
                    + "INNER JOIN usuarios on usuarios.idUsuarios = alamcen.Usuarios_idUsuarios "
                    //where condicion mediante el ccodigo recibido del objeto producto
                    + "WHERE codigo = ?";

Then, you pass the parameter:

            PreparedStatement pst2;
            pst2 = con.prepareStatement(sentecia);
            pst2.setString(1,almac.getCodigo());
            // Si codigo fuese un entero: pst2.setInt(1,almac.getCodigo());
            rs = pst2.executeQuery();

... resto del código.

On the correct use of PreparedStatemente, consult the Java documentation .

p>

And as a curious note, is the table called alamcen or almacen ?

    
answered by 19.08.2017 в 03:49