How do you get the HTML forms to MySQL with JDBC?



  • 2 HTML forms

    <input type="text" name="login" size="45" /> 
    

    and

    <input type="password" name="password" size="45" />
    

    Database MySQL Serverwhere similar fields are present login varchar (45) NOT NULL and password varchar (45) NOT NULL

    Show me how with help. JDBC record HTML forms in OBD MySQLPHP I don't think you're other Java-technology.



  • Rapid implementation with the use of survivor:

    html

    <html>
        <head>
            <meta charset="utf-8">
            <link rel="stylesheet" type="text/css" href="style.css">
        </head>
        <body id="body"> 
            <form method="post" action="TheFirstServlet">    
                <input name="login">
                <input name="password">
                <button>Записать</button>
            </form>
        </body>
    </html>
    

    TheFirstServlet

    public class FirstServlet extends HttpServlet {
    

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    }
    

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    request.setCharacterEncoding("UTF-8");
    String login = request.getParameter("login");
    String password = request.getParameter("password");
    insert(login, password);
    PrintWriter printWriter;
    try {
    printWriter = response.getWriter();
    printWriter.println("Ну, попытались");
    } catch(IOException exc) {}
    }

    public void insert(String login, String password) {
    Connection conn = null;
    Statement stmt = null;
    try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection("jdbc.url", "jdbc.login", "jdbc.password");
    stmt = conn.createStatement();
    String sql = "INSERT INTO NAME_TABLE (login,password)" +
    "VALUES ('" + login + "','" + password + "')";
    stmt.executeUpdate(sql);
    } catch (SQLException | ClassNotFoundException exc) {
    exc.printStackTrace();
    System.out.println("Не записал");
    }
    finally {
    try {
    if(conn!=null) conn.close();
    if(stmt!=null) stmt.close();
    } catch (SQLException exc) {}
    }
    }

    Well, that's the way it is.




Suggested Topics

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