-1

I am trying to insert the following in database:
image,
image name, price
but I have no idea to extract data from filesystem and pass to database

        String name = null;
        String value = null;

        if (ServletFileUpload.isMultipartContent(request)) {
            try {
                List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                HashMap<String, String> hmap = new HashMap<>();
                for (FileItem item : multiparts) {
                    if (!item.isFormField()) {
                        System.out.println(item.getName());
                        name = new File(item.getName()).getName();
                        System.out.print(name);
                        out.print(name);
                        item.write(new File("D:/projectdatas" + File.separator + name));
                    } else {

                        String fieldName = item.getFieldName();
                        value = item.getString();

                        hmap.put(fieldName, value);
                        out.println(value);

                    }
                }
                //File uploaded successfully
                out.print("Image successfully uploaded");
                request.setAttribute("message", "File Uploaded Successfully");
            } catch (Exception ex) {
                request.setAttribute("message", "File Upload Failed due to " + ex);
            }
iamrajshah
  • 963
  • 1
  • 11
  • 23
Niviz
  • 1
  • 2

1 Answers1

0

In general, storing the images into the database is not recommended since it is not efficient compared to file storage on the server. Scaling of your application will have more challenges.

Best approach is to store the Image into the file-system of your server and then persist the metadata of image and other details into the Database. So that you can always render efficiently with your mapped details.

Still - if you need to store for small utility purposes then you can
store the IMAGE data in BLOB type, it is supported in most of the
databases.

For more understanding check this post.

  • image name and its price already stored in DB but I want to store one more field to DB.that two values are in the "value " variable. how to extract the values and store ? – Niviz Jun 12 '19 at 06:50
  • do you have trouble in getting the correct value for 'value' variable? – Sunil Gollapinni Jun 12 '19 at 09:14
  • there are two values actually in "value " variable . i want to store those values into db – Niviz Jun 12 '19 at 09:28
  • ok.. then split the value variable based on the regular expression (ex.:_ or space) however it is being passed from the form. Then make a jdbc connection and retrieve the respective image details row and UPDATE with these fields. Correct me if I understand your requirement or not? – Sunil Gollapinni Jun 12 '19 at 09:33
  • public static final String INSERT_QUERY = "INSERT INTO your_table_name values (, , )"; // insert as per your columns designed public static void main(String[] args) { String host = "jdbc:mysql://localhost:1527/"; String uName = "root"; String uPass = "password"; try (Connection conn = DriverManager.getConnection(host, uName, uPass); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeUpdate(INSERT_QUERY)) { } catch (SQLException e) { e.printStackTrace(); } } – Sunil Gollapinni Jun 17 '19 at 06:58
  • you can refer example: https://www.tutorialspoint.com/jdbc/jdbc-insert-records.htm – Sunil Gollapinni Jun 17 '19 at 06:59