Frage

Got this piece of code that handles some parameters and writes it to a database:

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import java.sql.*;

public class QueryServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        String instId = req.getParameter("instId");
        String cartId = req.getParameter("cartId");
        String desc = req.getParameter("desc");
        String cost = req.getParameter("cost");
        String amount = req.getParameter("amount");
        String currency = req.getParameter("currency");
        String name = req.getParameter("name");
        String transId = req.getParameter("transId");
        String transStatus = req.getParameter("transStatus");
        String transTime = req.getParameter("transTime");
        String cardType = req.getParameter("cardType");
        Connection conn = null;
        PrintWriter out = res.getWriter();
        try {
            conn = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/orders", "root", "root");

            String sqlStr = "insert into orderdetails "
                + "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            try (PreparedStatement stmt = conn.prepareStatement(sqlStr)) {
                stmt.setString(1, transId);
                stmt.setString(2, instId);
                stmt.setString(3, cartId);
                stmt.setString(4, desc);
                stmt.setString(5, cost);
                stmt.setString(6, amount);
                stmt.setString(7, currency);
                stmt.setString(8, name);
                stmt.setString(9, transStatus);
                stmt.setString(10, transTime);
                stmt.setString(11, cardType);
                int updateCount = stmt.executeUpdate();
                for (Enumeration<String> en = req.getParameterNames(); en.hasMoreElements();) {
                    String paramName = en.nextElement();
                    String paramValue = req.getParameter(paramName);
                }
            }
    } catch (SQLException ex) {
        ex.printStackTrace();
        }
    }
}

If I pass all the parameters on the post message the database is updated just fine. However, if I don't pass one of the parameters nothing is written to the database.

Could you help me to change it so that, if a parameter is not in the post message it will set that value as 0 or null in the database?

Thanks for all the help so far.

War es hilfreich?

Lösung

For your parameters value of type String, you can set empty String or null if the parameter is not in POST method. Try using with ternary operator for each field value,

  String instId = req.getParameter("instId") == null ? "" : req.getParameter("instId");

Andere Tipps

If the parameter is not present in the request, java will use default values for the variables. In your case, you have all string variable which are getting the values from request. So if the parameter is not found, the default value of all the Strings will be "null";

You can do something like:

 String instId ="";
if(null != req.getParameter("instId")) {
instId = req.getParameter("instId")
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top