Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
3.1 kB
7
Indexable
Never
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Register")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String country = request.getParameter("country");

        // JDBC MySQL connection details
        String jdbcUrl = "jdbc:mysql://your_mysql_server:3306/your_database";
        String dbUser = "your_username";
        String dbPassword = "your_password";

        try {
            // Load JDBC driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Establish connection
            Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);

            // Create users table if not exists
            createUsersTable(connection);

            // Prepare SQL statement
            String sql = "INSERT INTO users (email, password, name, country) VALUES (?, ?, ?, ?)";
            try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
                preparedStatement.setString(1, email);
                preparedStatement.setString(2, password);
                preparedStatement.setString(3, name);
                preparedStatement.setString(4, country);

                // Execute SQL statement
                preparedStatement.executeUpdate();

                // Close resources
                preparedStatement.close();
                connection.close();
            }

            // Send response to client
            PrintWriter out = response.getWriter();
            out.println("User registered successfully!");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error processing registration.");
        }
    }

    private void createUsersTable(Connection connection) throws SQLException {
        // Create users table if not exists
        try (Statement statement = connection.createStatement()) {
            String createTableSQL = "CREATE TABLE IF NOT EXISTS users (" +
                    "id INT AUTO_INCREMENT PRIMARY KEY," +
                    "email VARCHAR(255) NOT NULL," +
                    "password VARCHAR(255) NOT NULL," +
                    "name VARCHAR(255) NOT NULL," +
                    "country VARCHAR(255) NOT NULL" +
                    ")";
            statement.executeUpdate(createTableSQL);
        }
    }
}
Leave a Comment