难以在数据库中输入信息(java servlet)

njthzxwz  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(166)

为什么我不能在数据库的代码段中输入数据?
在另一个类中,我检查了数据库中是否有人登录并使用了相同的连接。

@WebServlet(name = "ServletAdmin")
public class ServletAdmin extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.getWriter().println("Hallo");

        Statement statement = null;
        try {

            statement = LoginDao.connection.createStatement();

            String query = " INSERT INTO `login`  (`username`, `password`, `firstName`, `lastName`)  VALUES (?, ?, ?, ?)";

            PreparedStatement preparedStmt = LoginDao.connection.prepareStatement(query);
            preparedStmt.setString (1, "Barney");
            preparedStmt.setString (2, "Rubble");
            preparedStmt.setString (3, "had");
            preparedStmt.setString (4, "4156");

            boolean resultset = preparedStmt.execute();

            resp.getWriter().println(resultset);

        } catch (SQLException throwables) {
            System.out.println(throwables.getLocalizedMessage());
        }

    }

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

        response.sendRedirect("Insert-member.jsp");

    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题