html,jquery,ajax,servlet,mysql實現前端資料寫入資料庫

來源:互聯網
上載者:User

標籤:encoding   response   imp   word   man   web   amp   http   pack   

Myeclipse下建立WebProject項目

程式僅實現前端傳資料到後端寫入資料庫

前端html代碼:

<!DOCTYPE html><html>  <head>    <title>register.html</title>        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>            <body><label>使用者名稱:</label><input id="name" name="name" type="text" />          <label>密碼:</label><input id="password" name="password" type="password" />          <input id="register" type="button" value="註冊"/>  </body>  <script type="text/javascript" src="js/jquery-3.2.1.js"></script>    <script>          $("#register").click(function(){              var name=$("#name").val();              var password=$("#password").val();              if(name!=""&&password!=""){              $.ajax({                  type:"POST",                  url:"add",                  dataType:"JSON",                  data:{                      "name":name,                      "password":password,                 }            })          }else{              alert("請檢查您的輸入!");          }           })              </script></html>

後端Servlet代碼:

package com.jquery.register;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class add extends HttpServlet {    public void destroy() {        super.destroy();     }    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {            doPost(request,response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        PrintWriter out = response.getWriter();        request.setCharacterEncoding("utf-8");          response.setCharacterEncoding("utf-8");         Connection conn = null;         PreparedStatement stmt = null;         ResultSet rs = null;         String name=request.getParameter("name");         String password=request.getParameter("password");         String drive = "com.mysql.jdbc.Driver";         System.out.println("ajax後台互動成功");           try{             Class.forName(drive);             conn = DriverManager                     .getConnection("jdbc:mysql://localhost:3306/test?user=root&password=123");             System.out.println("資料庫連接成功");             String sql="insert  into user_table(name,password)";             sql+=" values(?,?)";             stmt = conn.prepareStatement(sql);             stmt.setString(1,name);             stmt.setString(2,password);             stmt.executeUpdate();             System.out.println("success");//        out.flush();//        out.close();    }catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }finally {            try {                //注意關閉原則:從裡到外                    if (rs != null) {                        rs.close();                    }                    if (stmt != null) {                        stmt.close();                    }                    if (conn != null) {                        conn.close();                    }                } catch (SQLException e) {                    e.printStackTrace();                }            }    }    public void init() throws ServletException {        // Put your code here    }}

 

 

 

 

html,jquery,ajax,servlet,mysql實現前端資料寫入資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.