Keep the data from the form into the OBD system. JavaEE



  • I've got a form from.

    1. <input name="name">
    2. <select name="type"> <option val=1>.......
    3. <input name="latitude">
    4. <input name="longitude">

    On the button submit by POST such parameters as:

    name, type, latitude, longitude

    Here's the survivor code.

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //getting parameters
        String pointName = request.getParameter("name");
    
    String STRpointType = request.getParameter("type");
    int pointType = Integer.parseInt(STRpointType);
    
    String lat = request.getParameter("latitude");
    String lng = request.getParameter("longitude");
    
    String query = "INSERT INTO joker.point (name, type_id, lat, lng) VALUES (" + pointName + "," + pointType + "," + lat + "," + lng + ");";
    
    Connection connection;
    Statement statement;
    String url = "jdbc:mysql://localhost:3306/joker";
    String user = "user";
    String password = "password";
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(url, user, password);
        statement = connection.createStatement();
        statement.execute(query);
    } catch (SQLException |ClassNotFoundException e) {
        e.printStackTrace();
    }
    
    request.setAttribute("point_name", pointName);
    request.setAttribute("point_type", pointType);
    request.setAttribute("point_lat", lat);
    request.setAttribute("point_lng", lng);
    
    
    String page;
    try {
        page = request.getParameter("page");
    } catch (Exception e) {
        page = "error";
    }
    if (page != null) {
        switch (page) {
            case "point_added":
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin_point_added.jsp");
                PrintWriter out = response.getWriter();
                rd.include(request, response);
                break;
            default:
                request.getRequestDispatcher("/admin_point_error.jsp").forward(request, response);
                break;
        }
    }
    

    }

    I'm pushing the button on my button, and I'm throwing it on a page with something. point added But the database doesn't have the data I took from the form. ♪ ♪

    What am I doing wrong? Where's my mistake? <form action="/sqlquery" method=POST> I wrote it. ♪



  • You're right, except for the request you're sending, there's a mistake in the syntaxis:

    //Ваш запрос
    String query = "INSERT INTO joker.point (name, type_id, lat, lng) VALUES (" + pointName + "," + pointType + "," + lat + "," + lng + ");";
    

    Switch to this one down there and compare:

    String query = "INSERT INTO joker.point (name, type_id, lat, lng) VALUES ('"+pointName+ "','"+pointType+"','"+lat+ "','"+lng+"')";
    

    The general code of your survivor will look like this:

    protected void doPost(HtpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    /getting parameters
    String pointName = request.getParameter("name);
    

    String STRpointType = request.getParameter("type)
    int pointType = Integer.parseInt(STRpointType);
    String lat = request.getParameter("latitude)
    String lng = request.getParameter("longitude)
    String query = "INSERT INTO joker.point."

    Connection connection;
    Statement statement;
    String url = "jdbc:mysql://localhost:3306/joker"
    String user = "user"
    String password = password;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(url, user, password);
    statement = connection.createStatement();
    statement.execute(query);
    catch (SQLException (ClassNotFoundException e) {
    e.printStackTrace();

    request.setAttribute("point_name, pointName);
    request.setAttribute('point_type, pointType);
    request.setAttribute("point_lat, lat);
    request.setAttribute("point_lng, lng);

    String page;
    try {
    page = request.getParameter(page)
    catch (Exception e) {
    page = "error."

    if (page!= null) {
    switch (page) {
    case "point_added"
    RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin_point_added.jsp");
    PrintWriter out = response.getWriter();
    rd.include(request, response);
    break;
    default:
    request.getRequestDispatcher("/admin_point_error.jsp."forward(request, response);
    break;






Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2