Question

I got a Servlet that can handle a Post form and save all the defined variables to MYSQL.

I'm now attempting to write another Servlet that will read all the parameters and their values with req.getParameterMap() and then send them to the database, in case a parameter that was not specified gets logged as well.

The code I have for the first servlet is bellow:

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 transId = req.getParameter("transId") == null ? "" : req.getParameter("transId");
        String instId = req.getParameter("instId") == null ? "" : req.getParameter("instId");
        String cartId = req.getParameter("cartId") == null ? "" : req.getParameter("cartId");
        String desc = req.getParameter("desc") == null ? "" : req.getParameter("desc");
        String cost = req.getParameter("cost") == null ? "" : req.getParameter("cost");
        String amount = req.getParameter("amount") == null ? "" : req.getParameter("amount");
        String currency = req.getParameter("currency") == null ? "" : req.getParameter("currency");
        String name = req.getParameter("name") == null ? "" : req.getParameter("name");
        String transStatus = req.getParameter("transStatus") == null ? "" : req.getParameter("transStatus");
        String transTime = req.getParameter("transTime") == null ? "" : req.getParameter("transTime");
        String cardType = req.getParameter("cardType") == null ? "" : 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();
        }
    }
}

For the second Servlet, I managed to get it to be able to read all the parameters and their values and display them on the webpage but I have no clue on how I can then get them to the database, either by creating the columns for the parameters or having them created already and just creating row values.

Could you provide me some guidance or examples to follow?

Bellow is the code for the second Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryServlet extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        PrintWriter pw=res.getWriter();
        res.setContentType("text/html");

        Map m=req.getParameterMap();
        Set s = m.entrySet();
        Iterator it = s.iterator();

            while(it.hasNext()){

                Map.Entry<String,String[]> entry = (Map.Entry<String,String[]>)it.next();

                String key             = entry.getKey();
                String[] value         = entry.getValue();

                pw.println("Key is "+key+"<br>");

                    if(value.length>1){    
                        for (int i = 0; i < value.length; i++) {
                            pw.println("<li>" + value[i].toString() + "</li><br>");
                        }
                    }else
                            pw.println("Value is "+value[0].toString()+"<br>");

                    pw.println("-------------------<br>");
            }

        pw.close();    
    }
}

Any help will be greatly appreciated.

Cheers

Était-ce utile?

La solution

Well first you need to start specifying columns when doing inserts, unless you want your code to break later when you add a column to the table. The way you're doing it you always have to include all the columns and in the defined order.

If you specify the columns, then you can use only the ones you want, and in any order you want.

 insert into tablename (col1, col2) values('col1val', 'col2val');
 insert into tablename (col2, col1, col5) values('col2val', 'col1val', 'col5val');

From there you should be able to figure out how to modify the second servlet to insert using the Key and Value variables, and creating the guts to go inside the column list and value list parts of the SQL statement.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top