CVE-2014-125047 (published 2023-01-06T12:15Z)

A vulnerability classified as critical has been found in tbezman school-store. This affects an unknown part. The manipulation leads to sql injection. The identifier of the patch is 2957fc97054216d3a393f1775efd01ae2b072001. It is recommended to apply a patch to fix this issue. The identifier VDB-217557 was assigned to this vulnerability.

CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-89
cwe_name: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
description: The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
extended_description: 
            
            
         
         

Fix information:
repo_url: https://github.com/tbezman/school-store
commit_details: repo_url: https://github.com/tbezman/school-store
author: Terence Bezman
author_date: 2014-11-23 19:18:29-05:00
msg: SQL Injection

Changed all of the SQL Queries to be safe from SQL Injeection
repository: repo_url: https://github.com/tbezman/school-store
repo_name: tbezman/school-store
repo_language: CSS
owner: tbezman
file_changes: filename: DailySubmission.java
change_type: ModificationType.MODIFY
diff: @@ -4,6 +4,7 @@ import com.bezman.servlet.IndexServlet;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
@@ -116,7 +117,11 @@ public class DailySubmission {
 
         String sales = "";
 
-        ResultSet salesSet = IndexServlet.execQuery("select * from sales where date='" + date + "'");
+        PreparedStatement statement = IndexServlet.connection.prepareStatement("select * from sales where date=?");
+        statement.setTimestamp(1, date);
+
+        ResultSet salesSet = statement.executeQuery();
+
         while(salesSet.next()){
             sales += salesSet.getString("sale") + ",";
         }

programming_language: Java
filename: Attendance.java
change_type: ModificationType.MODIFY
diff: @@ -8,8 +8,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
@@ -45,7 +45,12 @@ public class Attendance {
         System.out.println(month + "/ " + day + "/ " + year);
 
         try {
-            ResultSet resultSet = IndexServlet.execQuery("select * from daily where MONTH(date)='" + month + "' and DAY(date)='" + day + "' and YEAR(date)='" + year + "' order by period");
+            PreparedStatement statement = IndexServlet.connection.prepareStatement("select * from daily where MONTH(date)=? and DAY(date)=? and YEAR(date)=? order by period");
+            statement.setString(1, month);
+            statement.setString(2, day);
+            statement.setString(3, year);
+
+            ResultSet resultSet = statement.executeQuery();
 
             while(resultSet.next()){
                 jsonArray.add(resultSet.getString("names") + ";" + resultSet.getString("period"));
@@ -61,27 +66,7 @@ public class Attendance {
     @RequestMapping(value = "/attendance", method = {RequestMethod.GET})
     public String getAttendance(Model model, HttpServletRequest request, @RequestParam(value = "month", required = false) String month, @RequestParam(value = "day", required = false) String day, @RequestParam(value = "year", required = false) String year) {
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                System.out.println("Username : " + username);
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         model.addAttribute("namesJSON", getAttendanceJSON(model, request, month, day, year));
 

programming_language: Java
filename: DailyServlet.java
change_type: ModificationType.MODIFY
diff: @@ -10,8 +10,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Comparator;
@@ -26,40 +26,24 @@ public class DailyServlet {
     @RequestMapping(value = "/daily", method = RequestMethod.GET)
     public String getDaily(Model model, HttpServletRequest request){
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
+        IndexServlet.servletLoginCheck(model, request);
 
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                System.out.println("Username : " + username);
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-
-                ResultSet itemSet = IndexServlet.execQuery("select * from items");
-                JSONArray jsonArray = new JSONArray();
+        try {
+            ResultSet itemSet = IndexServlet.execQuery("select * from items");
+            JSONArray jsonArray = new JSONArray();
 
-                while(itemSet.next()){
-                    JSONObject jsonObject = new JSONObject();
+            while (itemSet.next()) {
+                JSONObject jsonObject = new JSONObject();
 
-                    jsonObject.put("itemName", itemSet.getString("name"));
-                    jsonObject.put("priceOfItem", itemSet.getDouble("price"));
+                jsonObject.put("itemName", itemSet.getString("name"));
+                jsonObject.put("priceOfItem", itemSet.getDouble("price"));
 
-                    jsonArray.add(jsonObject);
-                }
-
-                model.addAttribute("itemNames", StringEscapeUtils.escapeJavaScript(jsonArray.toJSONString()));
-            } catch (SQLException e) {
-                e.printStackTrace();
+                jsonArray.add(jsonObject);
             }
+
+            model.addAttribute("itemNames", StringEscapeUtils.escapeJavaScript(jsonArray.toJSONString()));
+        }catch (SQLException e){
+            e.printStackTrace();
         }
 
         return "daily";
@@ -72,8 +56,16 @@ public class DailyServlet {
         JSONArray jsonArray = new JSONArray();
 
         try {
-            String query = period == null ? "select * from students" : "select * from students where period='" + period + "' ORDER BY period ASC";
-            ResultSet resultSet = IndexServlet.execQuery(query);
+            PreparedStatement statement;
+
+            if (period == null){
+                statement = IndexServlet.connection.prepareStatement("SELECT * FROM students");
+            }else{
+                statement = IndexServlet.connection.prepareStatement("SELECT * FROM students WHERE period=? ORDER BY period ASC");
+                statement.setString(1, period);
+            }
+
+            ResultSet resultSet = statement.executeQuery();
 
             while(resultSet.next()){
                 JSONObject jsonObject = new JSONObject();

programming_language: Java
filename: IndexServlet.java
change_type: ModificationType.MODIFY
diff: @@ -28,26 +28,7 @@ public class IndexServlet {
     @RequestMapping(value = "/",method = RequestMethod.GET)
     public String processWelcome(Model model, HttpServletRequest request){
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         model.addAttribute("motd", StringEscapeUtils.escapeHtml(Reference.motd).replace("\n", "<br/>"));
 
@@ -74,7 +55,10 @@ public class IndexServlet {
 
     public static boolean isSessionAdmin(String sessionID){
         try {
-            ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + sessionID + "'");
+            PreparedStatement sessionStatement = IndexServlet.connection.prepareStatement("SELECT * from sessions WHERE sessionID=?");
+            sessionStatement.setString(1, sessionID);
+
+            ResultSet resultSet = sessionStatement.executeQuery();
 
             String username = null;
 
@@ -85,7 +69,10 @@ public class IndexServlet {
             if(username == null)
                 return false;
 
-            ResultSet accountsSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
+            PreparedStatement roleStatement = IndexServlet.connection.prepareStatement("SELECT  * from accounts where username=?");
+            roleStatement.setString(1, username);
+
+            ResultSet accountsSet = roleStatement.executeQuery();
 
             while(accountsSet.next()){
                 if (accountsSet.getString("role").equals("admin"))
@@ -148,4 +135,33 @@ public class IndexServlet {
 
         return new Cookie("12", "12");
     }
+
+    public static void servletLoginCheck(Model model, HttpServletRequest request){
+        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
+        if (cookie != null){
+            try {
+                PreparedStatement statement = IndexServlet.connection.prepareStatement("SELECT  * from sessions where sessionID=?");
+                statement.setString(1, cookie.getValue());
+
+                ResultSet resultSet = statement.executeQuery();
+                String username = null;
+
+                while(resultSet.next()){
+                    model.addAttribute("username", resultSet.getString("username"));
+                    username = resultSet.getString("username");
+                }
+
+                PreparedStatement roleStatement = IndexServlet.connection.prepareStatement("SELECT * from accounts where username=?");
+                roleStatement.setString(1, username);
+
+                ResultSet accountSet = roleStatement.executeQuery();
+
+                while(accountSet.next()){
+                    model.addAttribute("role", accountSet.getString("role"));
+                }
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
+        }
+    }
 }

programming_language: Java
filename: ItemRecapServlet.java
change_type: ModificationType.MODIFY
diff: @@ -10,14 +10,12 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
-import java.util.ArrayList;
-import java.util.Comparator;
-import java.util.stream.Collectors;
+import java.util.*;
 
 /**
  * Created by Terence on 11/16/2014.
@@ -29,26 +27,7 @@ public class ItemRecapServlet {
     @RequestMapping(value = "/itemrecap", method = {RequestMethod.GET, RequestMethod.POST})
     public String getItemRecap(Model model, HttpServletRequest request){
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         return "itemrecap";
     }
@@ -63,26 +42,61 @@ public class ItemRecapServlet {
         ArrayList params = new ArrayList();
         ArrayList<ItemSale> items = new ArrayList<>();
 
-        if (month != null)
-            params.add("MONTH(date)='" + month + "' ");
+        HashMap<Integer, String> valueMap = new HashMap<>();
 
-        if (day != null)
-            params.add("DAY(date)='" + day + "' ");
+        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
 
-        if (year != null)
-            params.add("YEAR(date)='" + year + "' ");
+        int predCount = 1;
 
-        if (month == null && day == null && year == null)
-            query = "select * from sales";
-        else query += params.stream().collect(Collectors.joining(" and "));
+        if (month != null) {
+            query += "MONTH(date)=?";
+
+            valueMap.put(predCount, month);
+
+            predCount++;
+        }
+
+        if (day != null) {
+
+            if (predCount > 1)
+                query += " and ";
+
+            query += "DAY(date)=?";
+
+            valueMap.put(predCount, day);
 
-        if (order == null)
-            order = "";
+            predCount++;
+        }
+
+        if (year != null) {
+
+            if (predCount > 1)
+                query += " and ";
+
+            query += "YEAR(date)=?";
+
+            valueMap.put(predCount, year);
+
+            predCount++;
+        }
+
+        if (month == null && day == null && year == null)
+            query = "SELECT * from sales";
 
         System.out.println(query);
 
         try {
-            ResultSet resultSet = IndexServlet.execQuery(query);
+
+            PreparedStatement statement = IndexServlet.connection.prepareStatement(query);
+
+            for(Integer integer : valueMap.keySet()){
+                statement.setString(integer, valueMap.get(integer));
+            }
+
+            System.out.println(statement.toString());
+
+
+            ResultSet resultSet = statement.executeQuery();
 
             while(resultSet.next()){
                 String sale = resultSet.getString("sale");

programming_language: Java
filename: ItemSettings.java
change_type: ModificationType.MODIFY
diff: @@ -10,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -26,27 +25,7 @@ public class ItemSettings {
     @RequestMapping(value = "/itemsettings", method = {RequestMethod.GET})
     public String ItemSettings(Model model, HttpServletRequest request){
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                System.out.println("Username : " + username);
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         JSONArray jsonArray = new JSONArray();
 

programming_language: Java
filename: Login.java
change_type: ModificationType.MODIFY
diff: @@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.Random;
@@ -27,7 +28,11 @@ public class Login {
         JSONObject jsonObject = new JSONObject();
 
         try {
-            ResultSet resultSet = IndexServlet.execQuery("select * from accounts where username='" + username + "' and password='" + password + "'");
+            PreparedStatement statement = IndexServlet.connection.prepareStatement("select * from accounts where username=? and password=?");
+            statement.setString(1, username);
+            statement.setString(2, password);
+
+            ResultSet resultSet = statement.executeQuery();
 
             boolean foundAccount = false;
 
@@ -38,8 +43,16 @@ public class Login {
             if (foundAccount){
                 String sessionID = getRandomSessionID();
 
-                IndexServlet.execUpdate("delete from sessions where username='" + username + "'");
-                IndexServlet.execUpdate("insert into sessions values('" + username + "', '" + sessionID + "')");
+                PreparedStatement deleteSessions = IndexServlet.connection.prepareStatement("DELETE from sessions WHERE username=?");
+                deleteSessions.setString(1, username);
+
+                deleteSessions.executeUpdate();
+
+                PreparedStatement insertSession = IndexServlet.connection.prepareStatement("insert into sessions VALUES(?, ?)");
+                insertSession.setString(1, username);
+                insertSession.setString(2, sessionID);
+
+                insertSession.executeUpdate();
 
                 jsonObject.put("success", sessionID);
                 response.addCookie(new Cookie("sessionID", sessionID));

programming_language: Java
filename: MonthlyRecapServlet.java
change_type: ModificationType.MODIFY
diff: @@ -12,11 +12,13 @@ import org.springframework.web.bind.annotation.ResponseBody;
 
 import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
+import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -28,27 +30,7 @@ public class MonthlyRecapServlet {
 
     @RequestMapping(value = "/monthlyrecap", method = {RequestMethod.GET})
     public String getMonthlyPage(Model model, HttpServletRequest request){
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                System.out.println("Username : " + username);
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         return "monthlyrecap";
     }
@@ -61,18 +43,45 @@ public class MonthlyRecapServlet {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(new Date());
 
+        HashMap<Integer, String> valueMap = new HashMap<>();
+
         String query = "select * from daily where ";
 
         ArrayList params = new ArrayList();
 
-        if(month != null)
-            params.add("MONTH(date)='" + month + "' ");
+        int predCount = 1;
+
+        if (month != null) {
+            query += "MONTH(date)=?";
+
+            valueMap.put(predCount, month);
+
+            predCount++;
+        }
+
+        if (day != null) {
+
+            if (predCount > 1)
+                query += " and ";
+
+            query += "DAY(date)=?";
+
+            valueMap.put(predCount, day);
+
+            predCount++;
+        }
+
+        if (year != null) {
+
+            if (predCount > 1)
+                query += " and ";
 
-        if(day != null)
-            params.add("DAY(date)='" + day + "' ");
+            query += "YEAR(date)=?";
 
-        if(year != null)
-           params.add("YEAR(date)='" + year +"' ");
+            valueMap.put(predCount, year);
+
+            predCount++;
+        }
 
         if (month == null && day == null && year == null)
             query = "select * from daily";
@@ -83,17 +92,18 @@ public class MonthlyRecapServlet {
 
         query += (" order by date " + order);
 
-        System.out.println(query);
+        try {
 
-        ResultSet resultSet = null;
+            PreparedStatement statement = IndexServlet.connection.prepareStatement(query);
 
-        try {
-            resultSet = IndexServlet.execQuery(query);
-        } catch (SQLException e) {
-            e.printStackTrace();
-        }
+            for(Integer integer : valueMap.keySet()){
+                statement.setString(integer, valueMap.get(integer));
+            }
+
+            System.out.println(statement);
+
+            ResultSet resultSet = statement.executeQuery();
 
-        try {
             while(resultSet.next()){
                 DailySubmission submission = DailySubmission.submissionFromRow(resultSet);
 
@@ -111,8 +121,15 @@ public class MonthlyRecapServlet {
     public String deletePeriodFromDate(@RequestParam("date") String date){
         JSONObject returnJSON = new JSONObject();
         try {
-            IndexServlet.execUpdate("delete from daily where date='" + date + "'");
-            IndexServlet.execUpdate("delete from sales where date='" + date + "'");
+            PreparedStatement deleteDaily = IndexServlet.connection.prepareStatement("DELETE from daily where date=?");
+            deleteDaily.setString(1, date);
+
+            deleteDaily.executeUpdate();
+
+            PreparedStatement deleteSales = IndexServlet.connection.prepareStatement("DELETE from sales where date=?");
+            deleteSales.setString(1, date);
+
+            deleteSales.executeUpdate();
             returnJSON.put("success", "true");
         } catch (SQLException e) {
             e.printStackTrace();

programming_language: Java
filename: StudentSettingsServlet.java
change_type: ModificationType.MODIFY
diff: @@ -9,9 +9,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
-import java.sql.ResultSet;
+import java.sql.PreparedStatement;
 import java.sql.SQLException;
 
 /**
@@ -24,27 +23,7 @@ public class StudentSettingsServlet {
     @RequestMapping(value = "/studentsettings", method = {RequestMethod.GET, RequestMethod.POST})
     public String studentSettings(Model model, HttpServletRequest request){
 
-        Cookie cookie = IndexServlet.getCookie(request.getCookies(), "sessionID");
-        if (cookie != null){
-            try {
-                ResultSet resultSet = IndexServlet.execQuery("select * from sessions where sessionID='" + cookie.getValue() + "'");
-                String username = null;
-
-                while(resultSet.next()){
-                    model.addAttribute("username", resultSet.getString("username"));
-                    username = resultSet.getString("username");
-                }
-
-                System.out.println("Username : " + username);
-                ResultSet accountSet = IndexServlet.execQuery("select * from accounts where username='" + username + "'");
-
-                while(accountSet.next()){
-                    model.addAttribute("role", accountSet.getString("role"));
-                }
-            } catch (SQLException e) {
-                e.printStackTrace();
-            }
-        }
+        IndexServlet.servletLoginCheck(model, request);
 
         return "studentsettings";
     }
@@ -56,7 +35,10 @@ public class StudentSettingsServlet {
         JSONObject jsonObject = new JSONObject();
 
         try {
-            IndexServlet.execUpdate("delete from students where name='" + name + "'");
+            PreparedStatement statement = IndexServlet.connection.prepareStatement("DELETE  from students where name=?");
+            statement.setString(1, name);
+
+            statement.executeUpdate();
             jsonObject.put("success", "true");
         } catch (SQLException e) {
             e.printStackTrace();
@@ -73,7 +55,11 @@ public class StudentSettingsServlet {
         JSONObject jsonObject = new JSONObject();
 
         try {
-            IndexServlet.execUpdate("insert into students values('" + StringEscapeUtils.escapeHtml(name) + "', '" + StringEscapeUtils.escapeHtml(period) + "')");
+            PreparedStatement statement = IndexServlet.connection.prepareStatement("insert into students VALUES(?, ?)");
+            statement.setString(1, StringEscapeUtils.escapeHtml(name));
+            statement.setString(2, StringEscapeUtils.escapeHtml(period));
+
+            statement.executeUpdate();
             jsonObject.put("success", "true");
         } catch (SQLException e) {
             e.printStackTrace();
@@ -90,7 +76,13 @@ public class StudentSettingsServlet {
         JSONObject jsonObject = new JSONObject();
 
         try {
-            IndexServlet.execUpdate("update students set name='" + StringEscapeUtils.escapeHtml(newName) + "',period='" + StringEscapeUtils.escapeHtml(newPeriod) + "' where name='" + oldName + "' and period='" + oldPeriod + "'");
+            PreparedStatement statement = IndexServlet.connection.prepareStatement("update students set name=?, period=? where name=? and period=?");
+            statement.setString(1, newName);
+            statement.setString(2, newPeriod);
+            statement.setString(3, oldName);
+            statement.setString(4, oldPeriod);
+
+            statement.executeUpdate();
             jsonObject.put("success", "true");
         } catch (SQLException e) {
             e.printStackTrace();

programming_language: Java
filename: SubmitDaily.java
change_type: ModificationType.MODIFY
diff: @@ -8,12 +8,12 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 
+import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.Timestamp;
-import java.util.Calendar;
-import java.util.Map;
-import java.util.TimeZone;
+import java.util.*;
 import java.util.stream.Collectors;
+import java.util.stream.IntStream;
 
 /**
  * Created by Terence on 11/9/2014.
@@ -25,30 +25,53 @@ public class SubmitDaily {
     @RequestMapping(value = "/dailysubmit", method = {RequestMethod.GET, RequestMethod.POST})
     @ResponseBody
     public String submit(@RequestParam Map allParams, Model model){
-        String query = "insert into daily (date, ";
-
         JSONObject jsonObject = new JSONObject();
 
         String sales = (String) allParams.get("sale");
         allParams.remove("sale");
 
+        HashMap<Integer, String> keyMap = new HashMap<>();
+        HashMap<Integer, String> valueMap = new HashMap<>();
+
+        int i = 1;
+        for(Object obj : allParams.keySet()){
+            keyMap.put(i, (String) obj);
+            valueMap.put(i, (String) allParams.get(obj));
+            i++;
+        }
+
         Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
         Timestamp timestamp = new Timestamp(calendar.getTime().getTime());
 
-        String columnsNames = (String) allParams.keySet().stream().collect(Collectors.joining(", "));
-        query += columnsNames;
-        query += ") values('" + timestamp + "', " + (String) allParams.keySet().stream().map(a -> allParams.get(a)).collect(Collectors.joining("', '", "'", "'")) + ")";
+        String columnNames = (String) keyMap.values().stream().collect(Collectors.joining(", "));
+
+        ArrayList questionMarks = new ArrayList();
+        IntStream.range(0, allParams.size()).forEach(a -> questionMarks.add("?"));
+
+        String query = "insert into daily (date, " + columnNames + ") VALUES('" + timestamp + "', " + questionMarks.stream().collect(Collectors.joining(", ")) + ")";
 
         System.out.println("query : " + query);
 
         try {
-            IndexServlet.execUpdate(query);
+            PreparedStatement statement = IndexServlet.connection.prepareStatement(query);
+
+            for(Integer integer: valueMap.keySet()){
+                statement.setString(integer, valueMap.get(integer));
+            }
+
+            System.out.println(statement);
+
+            statement.executeUpdate();
 
             System.out.println(sales);
             for(String currentSale : sales.split(",")) {
-                query = "insert into sales values('" + timestamp + "', '" + currentSale + "')";
-                System.out.println(query);
-                IndexServlet.execUpdate(query);
+                query = "insert into sales values(?, ?)";
+
+                PreparedStatement salesStatement = IndexServlet.connection.prepareStatement(query);
+                salesStatement.setTimestamp(1, timestamp);
+                salesStatement.setString(2, currentSale);
+
+                salesStatement.executeUpdate();
             }
 
             jsonObject.put("success", "true");

programming_language: Java