JDBC in Java || Insert data from the single page into multiple tables.

Insert data from the single page into multiple tables is a tricky task. But using JDBC in this tutorial we will solve it in an easy So if you don’t know how to perform JDBC operation in Java please follow the link.

Java database connectivity tutorial in a simple way.

Let’s create a Java web project using MySQL, JSP and servlet and eclipse.

how to insert data from the single page into multiple tables.

Following are requirements for creating a dynamic web application with Eclipse?

Database:    MySQL workbench.

Language : HTML or JSP,servlet.

Server:         Glassfish or Tomcat.

Let’s start the insert data from the single page into multiple tables and MySQL.

Configure your  MYSQL database:

Create a new database with name “mydb” and table1 with the name “student”.

CREATE TABLE `mydb`.`student` (
  `sid` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(45) NULL,
  `surname` VARCHAR(45) NULL,
  `email` VARCHAR(45) NULL,
  PRIMARY KEY (`sid`));

Create table2 with the name “education”.

CREATE TABLE `mydb`.`education` (
  `eid` INT NOT NULL AUTO_INCREMENT,
  `college` VARCHAR(45) NULL,
  `sclass` VARCHAR(45) NULL,
  `subject` VARCHAR(45) NULL,
  PRIMARY KEY (`eid`));

index.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="InsertData" method="post">
    <pre>
Name : 		<input type="text" name="name">

Surname : 	<input type="text" name="surname">

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

College : 	<input type="text" name="college">

Class : 	<input type="text" name="sclass">

Subject : 	<input type="text" name="subject">

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

  </pre>


  </form>


</body>
</html>

InsertData.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;

/**
 * Servlet implementation class InsertData
 */
@WebServlet("/InsertData")
public class InsertData extends HttpServlet {
  private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsertData() {
        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();
    String name = request.getParameter("name");
    String surname = request.getParameter("surname");
    String email = request.getParameter("email");
    String college = request.getParameter("college");
    String sclass = request.getParameter("sclass");
    String subject = request.getParameter("subject");
    
    //database Connectivity code.........
    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 student (name,surname,email)values('"+name+"','"+surname+"','"+email+"') ");
          stmt.executeUpdate("insert into education (college,sclass,subject)values('"+college+"','"+sclass+"','"+subject+"') ");
          
          out.println("Data is inserted successfully in both tables");
          
          
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    
  }

}

 

2 Comments

Add a Comment

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

fifteen − 1 =