Insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form action = "InsertAction">
Id:<input type="text" name="Id"><br>
Name:<input type="text" name="Name"><br>
Course:<input type="text" name="Course"><br>
Age:<input type="text" name="Age"><br>
Address:<input type="text" name="Address"><br>
Email:<input type="text" name="Email"><br>
<input type="submit">
</form>
</body>
</html>
InsertAction.java (Servlet Page )
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
public class InsertAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Connection connection = null;
int Id=Integer.parseInt(request.getParameter("Id"));
String Name= request.getParameter("Name");
String Course = request.getParameter("Course");
int Age=Integer.parseInt(request.getParameter("Age"));
String Address= request.getParameter("Address");
String Email = request.getParameter("Email");
System.out.println("-"+Id+"-"+Name+"-"+Course+"-"+Age+"-"+Address+"-"+Email);
try
{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/education";
String user = "root";
String password = "admin";
connection = DriverManager.getConnection("jdbc:mysql://localhost/education", "root", "admin");
//connection = DriverManager.getConnection(url,user,password);
Statement stmt = connection.createStatement();
String sql = "INSERT INTO education VALUES('"+Id+"', '"+Name+"', '"+Course+"', '"+Age+"', '"+Address+"', '"+Email+"')";
stmt.executeUpdate(sql);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment