Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Thursday, February 22, 2024

Code for INSERTION of DATA into TABLE using JSP.

Ins.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Insert Data</title>

</head>

<body>

<h2>Insert Data</h2>

<form action="insert.jsp" method="post">

<label for="username">User Name:</label>

<input type="text" id="name" name="username" required><br><br>

<label for="pass">Password:</label>

<input type="text" id="pass" name="password" required><br><br>

<input type="submit" value="Insert Data">

</form>

</body>

</html>


Insert.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ page import="java.sql.*" %>

<%@ page import="java.io.*" %>

<%

String un = request.getParameter("username");

String pass = request.getParameter("password");

try {

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gk", "root", "");

// SQL query to insert data into the table

String query = "INSERT INTO login (un, pass) VALUES (?, ?)";

PreparedStatement pstmt = con.prepareStatement(query);

pstmt.setString(1, un);

pstmt.setString(2, pass);

int rowsAffected = pstmt.executeUpdate();

if (rowsAffected > 0) {

// Insertion successful

out.println("Data inserted successfully");

} else {

// Insertion failed

out.println("Failed to insert data");

}

con.close();

}

catch (Exception e) {

out.println("Error: " + e);

}

%>




Output:








No comments:

Post a Comment