Servlet Mysql数据接口,前后端交互

Servlet Mysql数据接口,前后端交互

一、目录结构

Servlet Mysql数据接口,前后端交互_第1张图片

注意:这里一定要将所需要的jar包放入WEB-INF下面lib文件夹下(可新建),否则将会报出找不到数据库驱动的问题。

1:name,pass
6:data
2:get data from DB
5:data
3:get data from DB
4:data
index.html
LoginServlet.java
getMysqlData.java
数据库

二、前端index.html页面

<form action="http://localhost:8080/servlet_learn_1_war_exploded/login-servlet">
    name:<input type="text" name="name"><br>
    pass:<input type="text" name="pass"><br>
    <button type="submit">Login</button>
</form>

三、获取数据库内容

getMysqlData.java

package com.example.servlet_learn_1;

import com.google.gson.JsonObject;
import org.json.JSONArray;
import org.json.JSONObject;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class getMysqlData {
     
    public String getData() throws ClassNotFoundException, SQLException {
     
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://47.94.148.82:3306/test?useSSL=true&serverTimezone=GMT";
        String user = "newroot";
        String password = "123456";
        Connection myConnection = DriverManager.getConnection(url,user,password);
        Statement mystatement = myConnection.createStatement();
        ResultSet rs = mystatement.executeQuery("  SELECT * FROM  test.user");
        JSONArray array = new JSONArray(); // 存放查询的数据
        while(rs.next()) {
     
            String name = rs.getString("name");
            String pass = rs.getString("pass");
            JSONObject temp = new JSONObject(); // 将name pass存放在一个json数据中
            temp.put("name", rs.getString("name"));
            temp.put("pass", rs.getString("pass"));
            array.put(temp); // 追加在返回结果中
        }
        return array.toString(); // 返回查询的数据
    }

    public int check(String name, String pass){
     
//        JSONArray result =
        return 1;
    }

}

四、Servlet接口

package com.example.servlet_learn_1;

import com.google.gson.Gson;
import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "loginServlet", value = "/login-servlet")
public class LoginServlet extends HttpServlet {
     
    private String message;

    public void init() {
     
        message = "Hello World fafdsaassfdsa!";
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
        String name = (req.getParameter("name")); // 获取输入框的参数
        String pass = (req.getParameter("pass"));
        getMysqlData getdata = new getMysqlData();

        String resultdata = null;
        try {
     
            resultdata = getdata.getData();
        } catch (ClassNotFoundException e) {
     
            e.printStackTrace();
        } catch (SQLException throwables) {
     
            throwables.printStackTrace();
        }

        String json = new Gson().toJson(resultdata); //将获取的数据库json数据,转存为string
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(resultdata); // 将获取的数据写回到前端
    }


    public void destroy() {
     
    }
}

五、运行

(1)启动服务器

Servlet Mysql数据接口,前后端交互_第2张图片

(2)输入参数

Servlet Mysql数据接口,前后端交互_第3张图片

(3)后端获取到前端发送来的数据

image-20210414223118213

(4)前端获取到的数据

Servlet Mysql数据接口,前后端交互_第4张图片

数据库驱动jar包

mysql-connector-java-8.0.23.jar

你可能感兴趣的:(java遇到常见的错误,servlet,数据库)