package Servlet;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
 
@WebServlet("/uploadServlet")
@MultipartConfig(maxFileSize = 16177215)    // File kan vær opp til 16mb
public class FileUploadDBServlet extends HttpServlet {
     
    // Brukes for å koble til databasen.
    private String dbURL = "jdbc:mysql://localhost:3306/SLIT";
    private String dbUser = "root";
    private String dbPass = "Warstar123";
     
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // Henter verdier fra text linjene i databasen. 
        //String firstName = request.getParameter("firstName");
        //String lastName = request.getParameter("lastName");
        //String fileName = request.getParameter("fileName");
              
        InputStream inputStream = null; // streamen opp til databasen. 
         
        // obtains the upload file part in this multipart request
        Part filePart = request.getPart("file");
        if (filePart != null) {
            // Brukes for å teste opp mot debugging. 
            System.out.println(filePart.getName());
            System.out.println(filePart.getSize());
            System.out.println(filePart.getContentType());
             
            inputStream = filePart.getInputStream();
        }
         
        Connection conn = null; // Forbindelse til databasen. 
        String message = null;  // Melding blir sendt tilbake til klienten. 
         
        try {
            // Forbindelse til databasen. 
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
            
 
            // Lager SQL statement.
            String sql = "INSERT INTO submit (submit_file) values (?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            //statement.setString(1, firstName);
            //statement.setString(2, lastName);
            //statement.setString(3, fileName);
                                         
            if (inputStream != null) {
                // fetches input stream of the upload file for the blob column
                statement.setBlob(1, inputStream);
            }
 
            // Sender en kommando til database serveren hvis det er vellyket. 
            int row = statement.executeUpdate();
            if (row > 0) {
                message = "File uploaded and saved into database";
            }
        } catch (SQLException ex) {
            message = "ERROR: " + ex.getMessage();
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                // Lukker databasen 
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
          
        }
    }
}

@@ -1035,34 +1035,32 @@
 basen. %0A        
-//
 String firstName
@@ -1052,19 +1052,18 @@
 tring fi
-rst
+le
 Name = r
@@ -1085,19 +1085,18 @@
 eter(%22fi
-rst
+le
 Name%22);%0A
@@ -1107,24 +1107,20 @@
     
-//
 String 
-lastNam
+modul
 e = 
@@ -1141,23 +1141,21 @@
 ameter(%22
-lastNam
+modul
 e%22);%0A   
@@ -1163,25 +1163,24 @@
     
-//
 String 
-fileName
+studentID
  = r
@@ -1196,32 +1196,33 @@
 tParameter(%22
-fileName
+studentID
 %22);%0A        
@@ -2207,16 +2207,52 @@
 submit (
+submit_name, module_id, student_id, 
 submit_f
@@ -2264,16 +2264,25 @@
 values (
+?, ?, ?, 
 ?)%22;%0A   
@@ -2352,34 +2352,32 @@
 l);%0A            
-//
 statement.setStr
@@ -2385,19 +2385,18 @@
 ng(1, fi
-rst
+le
 Name);%0A 
@@ -2398,34 +2398,32 @@
 e);%0A            
-//
 statement.setStr
@@ -2433,15 +2433,13 @@
 (2, 
-lastNam
+modul
 e);%0A
@@ -2450,18 +2450,16 @@
         
-//
 statemen
@@ -2477,60 +2477,20 @@
 (3, 
-fileName);%0A                                         
+studentID);%0A
 %0A   
@@ -2638,17 +2638,17 @@
 setBlob(
-1
+4
 , inputS

Update FileUploadDBServlet.java