0

It is so much confusing information on the internet and here on stack overflow that I have no clue what to do and how to approach this problem.

I am trying for 2 days to find a way to store images into a MySQL DB and most of the stuff I tried is not working. I tried storing image as byte, I tried using InputStream and then FileReader and I couldn't make it work because every time I encountered different errors which I was not able to solve by myself.

Now I am trying to store it as a Part since I found some tutorial but now this tutorial is using another servlet method which I have never seen before called processRequest or something like that which confuses the hell out of me. Which is the easier or and better method? Storing as Blob or as reference? I would like to get some help on choosing a method to store this and get guidance to just finish this task.

This is what I have so far

<form method="POST" action="AddTripServlet" enctype="multipart/form-data">
    <label for="tripname">Trip name</label>
    <input type="text" name="tripname" id="tripname">
    <br>
    <label for="startdate">Start date</label>
    <input type="date" name="startdate" id="startdate">
    <br>
    <label for="enddate">End date</label>
    <input type="date" name="enddate" id="enddate">
    <br>
    <label for="impressions">Impressions</label>
    <textarea name="impressions" id="impressions" rows="5" cols="30">Add your impressions here</textarea>
    <br>
    <label for="photoone">Add first photo</label>
    <input type="file" id="photoone" name="photoone">
    <br>
    <label for="phototwo">Add second photo</label>
    <input type="file" id="phototwo" name="phototwo">
    <br>
    <label for="location">Location</label>
    <input type="text" id="location" name="location">
    <br><br>
    <input type="SUBMIT" value="Add trip">

    <div id="map">
        <p>Potential google map location if i can implement it</p>
    </div>

</form>
public class AddTripServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html");
        String name = request.getParameter("tripname");
        String startDate = request.getParameter("startdate");
        String endDate = request.getParameter("enddate");
        String impressions = request.getParameter("impressions");
        Part photoOne = request.getPart("photoone");
        Part photoTwo = request.getPart("phototwo");
        PrintWriter out = response.getWriter();
        out.print("<html><body>" + startDate + "<br><h1>hello</h1>" + endDate + "<br>"+photoOne+"</body></html>"+ name);


    }
}
create table trips(
    trip_id INT NOT NULL AUTO_INCREMENT,
    user_id INT,
    trip_name VARCHAR(255) NOT NULL,
    trip_start_date DATE,
    trip_end_date DATE,
    trip_impressions VARCHAR(255) NOT NULL,
    photo_one mediumblob, 
    photo_two mediumblob,
    trip_location_coordinates VARCHAR(255) NOT NULL,
        primary key(trip_id),
        foreign key(user_id) references users(user_id)
        );

The printwriter is for test purpose. Help me choose the best and easiest method to save a file between reference and blob, and give me some code example to do it. If my variant is good guide me to be able to understand what I have to do next to save the file as Blob into the database. If my variant is bad, help me add the images as reference by giving me some guidance.

I also add my model here where I have 2 Blob references now which I am not sure if they are correct or not for what I want to do.

public class Trip {

    private String name;
    private String startDate;
    private String endDate;
    private String impressions;
    private Blob photoOne;
    private Blob photoTwo;
    private String location;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public String getImpressions() {
        return impressions;
    }

    public void setImpressions(String impressions) {
        this.impressions = impressions;
    }

    public Blob getPhotoOne() {
        return photoOne;
    }

    public void setPhotoOne(Blob photoOne) {
        this.photoOne = photoOne;
    }

    public Blob getPhotoTwo() {
        return photoTwo;
    }

    public void setPhotoTwo(Blob photoTwo) {
        this.photoTwo = photoTwo;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
helloApp
  • 449
  • 1
  • 4
  • 21
  • Does this answer your question? [Display BLOB (image) through JSP](https://stackoverflow.com/questions/11192020/display-blob-image-through-jsp) – nbk Feb 20 '20 at 21:17
  • bob are bytes so you have to enter a byte array , but you didnd't wrote that that seems to work, the rest is displaying it – nbk Feb 20 '20 at 21:18
  • the answer does not help me because i need to get images into the DB from the form not get them in the jsp from the DB. I also added my Model wich in wich i have Blob reference type, should i change it to byte instead?Could you guys help me with the code by correcting what i already have and adding what i have to do next ?I created and deleted this code so many times and started over so many times in this 2 days because everytime i encountered an error i searched for something different and than everything from beginning all over again, so i would like to be pushed in the right direction pleas – helloApp Feb 20 '20 at 21:29
  • Then show us your code to insert the data, but you can take a look at https://stackoverflow.com/a/41369271/5193536 , that is wirking just fine, and the other link shows how to daspla it. – nbk Feb 20 '20 at 21:48

0 Answers0