JDBC in Java || Insert Data of multiple page in a single table.

Before this take a review for JDBC in Java or if you know how JDBC in java then you can go for this.

How to Insert data from multiple forms in a single MYSQL table in Java using JSP and Servlet and MySQL. If you are working on java web project and you need to perform an operation like you have three different form and you need to store data of that three from in a single table. So I am here with some solution.

We have a table in MYSQL database with column name:  name, last_name, email, mobile, address, zip.

Page1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Page2.jsp" method="post">
<pre>

Name :     <input type="text" name="name">

LastName : <input type="text" name="lastname">

<input type="submit" value="next">

</pre>
</form>

</body>
</html>

Page2.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("name");
String lastname = request.getParameter("lastname");
out.println(name+ " "+lastname);
session.setAttribute("name",name);
session.setAttribute("lastname", lastname);


%>
<form action="Page3.jsp" method="post">
<pre>

Email :  <input type="text" name="email">

Mobile : <input type="text" name="mobile">

<input type="submit" value="next">

</pre>
</form>


</body>
</html>

Page3.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%


String email = request.getParameter("email");
String mobile = request.getParameter("mobile");
out.println(email+ " "+mobile);
session.setAttribute("email",email);
session.setAttribute( "mobile",mobile);


%>
 
<form action="http://localhost:8080/Testing/InserData" method="post">
<pre>

Address : <input type="text" name="address">

Zip :     <input type="text" name="zip">

<input type="submit" value="Submit">

</pre>
</form>

</body>
</html>

InserData.java(Servlet)

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
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;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class InserData
 */
@WebServlet("/InserData")
public class InserData extends HttpServlet {
  private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InserData() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    String name = (String)session.getAttribute("name");
    String lastname = (String)session.getAttribute("lastname");
    String email = (String)session.getAttribute("email");
    String mobile = (String)session.getAttribute("mobile");
    
    
    
    String address  = request.getParameter("address");
    String zip  = request.getParameter("zip");
    try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
      Statement stmt = con.createStatement();
      stmt.executeUpdate("insert into user_profile(name,lastname,email,mobile,address,zip) values('"+name+"','"+lastname+"','"+email+"','"+mobile+"','"+address+"','"+zip+"')");
      out.println("Data is inserted ");
      
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    
  }

}

Add a Comment

Your email address will not be published. Required fields are marked *

four × three =