Inserting rows of servlet

PHOTO EMBED

Sun Nov 03 2024 18:31:53 GMT+0000 (Coordinated Universal Time)

Saved by @signup_returns #html

//Inserting rows of servlet

//index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Insert Student Data</title>
</head>
<body>
    <h1>Insert Student Data</h1>
    <form action="InsertStudent" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>

        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>

        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><br><br>

        <input type="submit" value="Insert">
    </form>
</body>
</html>



//InsertStudentServlet.java

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 jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

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

    public InsertStudentServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection con = null;
        PreparedStatement st = null;

        response.setContentType("text/html");
        PrintWriter pw = response.getWriter();

        try {
            // Load the Driver Class
            Class.forName("oracle.jdbc.OracleDriver");
            System.out.println("Driver loaded");

            // Create the connection
            con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "system", "1234");
            System.out.println("Connection established");

            String user = request.getParameter("username");
            String pwd = request.getParameter("password");
            String email = request.getParameter("email");

            // Insert statement
            st = con.prepareStatement("INSERT INTO studentdata (username, password, email) VALUES (?, ?, ?)");
            st.setString(1, user);
            st.setString(2, pwd);
            st.setString(3, email);

            int r = st.executeUpdate();
            if (r > 0) {
                pw.println("<h1>Student data inserted successfully!</h1>");
            } else {
                pw.println("<h1>Error inserting student data.</h1>");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            pw.println("<h1>Error loading database driver: " + e.getMessage() + "</h1>");
        } catch (SQLException e) {
            e.printStackTrace();
            pw.println("<h1>Error inserting data: " + e.getMessage() + "</h1>");
        } finally {
            try {
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
content_copyCOPY