Home>

After connecting to the DB, I would like to write to the table of the specified input data.

However, I am in a situation where I am wandering around without knowing where to write the process. (Since this time only writing to the DB, I would like to omit the details of the project etc.)

Currently, the try catch statement is used to describe the processing to the DB in the doPost of the servlet. (Since processing has a bad word, please interpret it as connection only)

However, it feels a little strange to describe the process to DB in doPost, and since it is used only for passing the character string to JSP, an error will occur unless it is a Try Catch statement.

Do I have to create a new Java project file?

Please advise someone, where to describe the process related to the connection to the DB, and how to describe the data update to the DB. (The DB file name and source code are listed below.)

Also, if you have any necessary information, please let us know in the comments. I would like to disclose as much as possible.

DB source file name: Darabase3.accdb

Source code

package Form1;
import java.io.IOException;
import java.sql. *;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/ **
 * Servlet implementation class form1
 * /
@WebServlet ("/ form1")
public class form1 extends HttpServlet {
    private static final long serialVersionUID = 1L;
   /**
     * @see HttpServlet # HttpServlet ()
     * /
    public form1 () {
        super ();
        // TODO Auto-generated constructor stub
    }
   /**
     * @see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response)
     * /
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println ("Test1");
         String view = "/WEB-INF/view/NewForm.jsp";
            RequestDispatcher dispatcher = request.getRequestDispatcher (view);
            dispatcher.forward (request, response);
    }
   /**
     * @see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response)
     * /
    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println ("Test2");
        String value = request.getParameter ("name");
        System.out.println (value);
        String value1 = request.getParameter ("Adress");
        System.out.println (value1);
        String value2 = request.getParameter ("callphone");
        System.out.println (value2);
        String value3 = request.getParameter ("nickname");
        System.out.println (value3);
        String value4 = request.getParameter ("password");
        System.out.println (value4);
        request.setAttribute (value, "name");
        request.setAttribute (value1, "Adress");
        request.setAttribute (value2, "callphone");
        request.setAttribute (value3, "nickname");
        request.setAttribute (value4, "password");
        String view1 = "/WEB-INF/view/Result.jsp";
        RequestDispatcher dispatcher1 = request.getRequestDispatcher (view1);
        dispatcher1.forward (request, response);
        // Process to store data in DB
        try {
            Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
        } catch (ClassNotFoundException e) {
            // TODO auto-generated catch block
            e.printStackTrace ();
        }
        Connection con = null;
        try {
            con = DriverManager.getConnection ("jdbc: odbc: Database3.accdb");
        } catch (SQLException e) {
            // TODO auto-generated catch block
            e.printStackTrace ();
        }
        try {
            Statement stmt = con.createStatement ();
        } catch (SQLException e) {
            // TODO auto-generated catch block
            e.printStackTrace ();
        }

        // doGet (request, response);
    }
}
  • Answer # 1

    You could not specify the exact path to connect to the database.