All instructions for connection & insert data in mysql from java JSP and Servlet using tomcat server


From this tutorial you can learn about...

All instructions for connection & insert data in mysql from java JSP and Servlet using tomcat server...



At first create a database "tutorialdb".
Now create  a table student with 6 columns(Name,Uname,Password,Email,Subject,University);
Code :

CREATE TABLE `tutorialdb`.`student` (
  `Name` varchar(100) NOT NULL,
  `Uname` varchar(50) NOT NULL,
  `Password` varchar(50) NOT NULL,
  `Email` varchar(100) NOT NULL,
  `Subject` varchar(50) NOT NULL,
  `University` varchar(100) NOT NULL
)

Right click on your project and create a new jsp  file. The jsp file name is  : index.jsp

-------------------------------Code for 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>Mysql Database Connection using jsp, servlet and tomcat</title>
</head>
<body>
<h1>Md. Alamgir Hossain</h1>
<h1>Dept. of Computer Science & Engineering</h1>
<h1>Jessore University of Science & Technology</h1>

<form action="firstdbserv" method="Post">
            Name : <input type="text" name="name"><br><br>    
        User Name : <input type="text" name="uname"><br><br>
        Password : <input type="password" name="pass"><br><br>
        Email : <input type="text" name="email"><br><br>
        Subject : <input type="text" name="subject"><br><br>
        University :<input type="text" name="university"><br><br>
        <input type="submit" value="Submit"><br>
    </form>
</body>
</html>


Now right click on src folder and take a new servlet and our servlet name is firstdbserv.java


-------------------------------- Code for firstdbserv.java-----------------------------

package eduhelp.serv;

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 firstdbserv
 */
@WebServlet("/firstdbserv")
public class firstdbserv extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public firstdbserv() {
        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 uname = request.getParameter("uname");
        String password = request.getParameter("pass");
        String email = request.getParameter("email");
        String subject = request.getParameter("subject");
        String university = request.getParameter("university");     

        try {

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

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

            Statement st = conn.createStatement();

            String sql = "insert into student (Name,Uname,Password,Email,Subject,University) values('"+name+"','"+uname+"','"+password+"','"+email+"','"+subject+"','"+university+"')";

            st.executeUpdate(sql);
            out.println("Data is Successfully Inserted into Student Table");
               }catch (ClassNotFoundException e) {

              e.printStackTrace();

        } catch (SQLException e) {

              e.printStackTrace();

         }
    }

}



Save all files and run the index.jsp file.
Now check your database.....................

See all instructions in this video

8 comments:

  1. This is the very first tutorial no the subject that actually works and doesnt pop up any strange error. Thanks a lot, perfect work, very neat and effective. This is really really helpful for a lot people all around the world.

    ReplyDelete
  2. Thanks, this is generally helpful.
    Still, I followed step-by-step your method in this Java training
    Core Java online training

    ReplyDelete