0

So I've been tying to set up a database for an imaginary flight company. I'm supposed to have a JSP page that allows admins to enter data like flight number, flight model, capacity, seat layout etc into a database. I managed to allow the admin to enter data such as flight model and number into the database. However, I do not know how to let admins add an image of the seating layout into the database. More specifically, I need to let the admins browse their computers and upload an image file from their computer into my database, and store it there so that whenever user search for a particular plane mode, such as Boeing-747, the image of the seat layout for the Boeing-747 will be returned. Any ideas on how I can do that? Any and all help would be appreciated.

I've been trying to look for a solution on Google, but I have no idea how to add any of the codes that I have found into my codes. In case anyone is wondering what I've already done, this is what I have so far:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@page import="java.sql.*"%>
<%@page import="java.io.*" %>
<%
String Model = request.getParameter("Model");
String Flight_number = request.getParameter("Flight_number");
Statement theStatement = null;
String capacity = request.getParameter("Capacity");
boolean x=true;
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost/testing";
String username="root";
String password="password";
Connection conn=DriverManager.getConnection(url,username,password);
theStatement =(Statement)conn.createStatement();
theStatement.executeUpdate("INSERT INTO testing.aircraft_data ( aircraft_model, flight_no , passenger_capacity)"+ "VALUES ('"+Model+"' , '"+Flight_number+"' , '"+capacity+"')");
//Process Result

theStatement.close();
conn.close();
}

catch(Exception e){
    out.println("Exception occured! "+e.getMessage()+" "+e.getStackTrace());
    x=false;
    out.println(e);
    System.out.println(e);
    response.sendRedirect("Homescreen_Admin.jsp?correct="+x+e);
}

//response.sendRedirect("Homescreen_Admin.jsp?correct="+x);

%>

</body>
</html>
Noob
  • 25
  • 3
  • 8
  • Take a look at http://stackoverflow.com/questions/36408805/how-to-receive-a-file-type-parameter-from-html-jsp-into-a-servlet – Juan May 20 '17 at 18:08
  • @Juan Thanks. I took a look at the question and the solution stated was to write a servlet. Does that mean that I have to write a servlet and somehow link it into the jsp so that I can get the image from the user? – Noob May 20 '17 at 18:15
  • If I were doing your assignment I would use a servlet to receive the input from the form, and then from the servlet dispatch the jsp page to show the output. To do that look into the Servlet's request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response); About the storage of the image, @Nicolas Azrak is right. – Juan May 20 '17 at 19:17

1 Answers1

0

As a good practice you should never store an image in a database. Databases are not optimized for that kind of usage, it will slow everything down.

A better idea is to store your image somewhere else (save it the server filesystem, Amazon S3, google cloud storage, etc...) and save the link to that instead as a string.

If you are completely sure that you want to save the image you must use a blob column type in mysql and store in there the image bytes.

Note: if your problem is how to upload the image from the html to the server what you are looking is an html form with multipart/form-data and a file upload https://www.w3schools.com/tags/att_input_accept.asp

The SWE
  • 404
  • 7
  • 14
  • Thanks for the answer. I do not understand what you meant by uploading an image from the html to the server. What I had in mind was to allow users to directly upload an image from their computer, i.e. C: drive, into the database so that it will be stored. I would like to know if it is possible, and if so, how? Any help would be appreciated. – Noob May 20 '17 at 18:19
  • Actually, I doubt that a seating plan is going to be much more than 100k, so it'd probably be fine (if a little unusual) to store it directly in the database. – Strawberry May 20 '17 at 18:23
  • From the code you have shared it seems it is a web application serving html, so in order to being able to upload an image you should create a form in which you will add a file input (check http://html.com/input-type-file/ it is the one you always see in pages to upload a file). Then that form, with the selected image will be sent to your http server, where you receive the image. Then you can do whatever you want with the image. Your – The SWE May 20 '17 at 18:35
  • So if I'm understanding what you're saying correctly, it means that I have to create a form, allow users to upload the image to the form, and then link the form back to my database? If that is so, could you perhaps give me an example code and maybe tell me where to insert it into my code? I have been to the site that you linked, but I do not see how to send the image back into my database. Am I supposed to change the form action into the name of my jsp page or something? Thanks in advance for the help – Noob May 20 '17 at 18:48
  • Kind of, you need the html and the database, what you are missing is the server you code in java which is in the middle. The server sends the html to the user browser, the browser uploads the file to the java server and then the server stores the image and save the info in the database. I don't know a lot about jsp but it is the same in every language. Check if http://stackoverflow.com/a/19842732/2913715 helps – The SWE May 20 '17 at 18:58