это код, который я написал для него, но я не вижу в нем ничего плохого, кроме случая, когда я просто поиграйтесь с подготовленным оператором, который я использую
Код: Выделить всё
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String role = request.getParameter("role");
String name = request.getParameter("name");
String surname = request.getParameter("surname");
response.setContentType("text/html");
PrintWriter pw =response.getWriter();
//initializing connections
Connection con=null;
ResultSet rs=null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ergasia2","root","root");
String sql = "INSERT INTO user(username, password, name, surname, role) VALUES (?,?,?,?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1,username);
stmt.setString(2,password);
stmt.setString(3,name);
stmt.setString(4,surname);
stmt.setString(5,role);
rs = stmt.executeQuery();
RequestDispatcher rd = request.getRequestDispatcher("index.html");
pw.println("User created successfully.");
rd.include(request, response);
} catch(Exception e) {e.printStackTrace();}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect(request.getContextPath() + "/RegisterServlet");
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... ts-that-do
Мобильная версия