How to save an image in Database by URL

1

I have a database in Postgresql and using pgadmin4, there I have a table called products with 5 columns that contain the following types of data: Id, name, description, price, type. Now I want to add a column where I can save an image from its url to show in my project a list of products where I have all an image and descriptions of the product. This I am working on eclipse, I want to know what kind of data would be the image and how to insert it in the database. thanks

my code:

@RequestMapping ("/sakuraproductos")
    static public String listadoProductos(Model template) throws SQLException {

    Connection connection;

    connection = DriverManager.getConnection(
            "jdbc:postgresql://localhost:5432/blabla", "blablas",
            "blabla");

    PreparedStatement ps = connection
            .prepareStatement(

            "SELECT * FROM productos");

    ResultSet result = ps.executeQuery();

    ArrayList<Producto> listaProductos;

    listaProductos = new ArrayList<Producto>();

    while (result.next()) {

        Producto a = new Producto(result.getString("nombre"),
                result.getString("descripcion"),
                result.getDouble("precio"), result.getString("tipo")

        );

        listaProductos.add (a);

    }

    template.addAttribute("productos", listaProductos);

    connection.close();
    return "sakuraproductos";
} 
    
asked by vanesa 15.09.2017 в 22:15
source

1 answer

1

I understand that the application you want to develop is web (war) type. Preferably the images can be PNG because they are not heavy, you must tell your component that only supports that type of image. The idea of saving only the URL in a column of the database is correct.

With URL I mean you must have something like that in the field:

http://[nombre-de-tu-host]/[directorio-donde-esta-la-img]/imagen.png

For example in your base in the respective table, in the address field there should be a value like this:

(1) http://localhost/imagenes/imagen1.png
ó
(2) http://192.168.1.105/imagenes/producto1/imagen-p1.png

The physical image must be stored on a server (in the same or another place), to which you can install an Apache so that your images can be accessed from a browser and thus call the images by their URL.

Now independent of the framework for the front-end you are using, it must be able to generate on the page where the list is, the tag:

<img>
Which receives as an argument the URL that is stored in the database and that value goes in its src attribute.

What you need to do in java would be to generate the CRUD where the method to save the image will be, there are several examples on the web of persisting data. Now to show each image for each product it would be that you have in the web page (HTML) a table component with an iterator which represents the object that maps your entity to the database. And in the column where the image goes, you put its address attribute.

    
answered by 15.09.2017 в 23:19