How to add user registration record into the database(MySQL)

Tutorial:2

In this tutorial, we will discuss how to create a Login.jsp and Register.jsp pages.

This tutorial we discuss  all the steps for creating a login.jsp and register.jsp page
Please first read the first tutorial of this project in the code section.
If you understand the first tutorial then you will understand the second tutorial.
First, we create login.jsp  and register.jsp and copy and paste this code into your Eclipse IDE.
In next tutorial, we discuss login.jsp and register.jsp and store registration record in the database.
database connectivity. 

Example:

1)Login.jsp
   
 n<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
 pageEncoding=”ISO-8859-1″%>
<%@ page import=”java.sql.*,java.io.*” %>   
<%@ page import=”javax.sql.*” %>

<!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 uname = request.getParameter(“user”);
    String pass = request.getParameter(“pass”);
  //  ResultSet resultSet = null;
    try{
       Class.forName(“com.mysql.jdbc.Driver”);
       Connection con =                                 DriverManager.getConnection(“jdbc:mysql://localhost:3306/registration”,”root”,”root”);
       String Query = “select * from register where Username=? and Password=?”;
       PreparedStatement st = con.prepareStatement(Query);
       st.setString(1,uname);
       st.setString(2,pass);
       ResultSet resultSet = st.executeQuery();
          if(resultSet.next()){
       out.println(“username:”+uname);
       out.println(“password:”+pass);
       } 
    else{
       response.sendRedirect(“login.html”);
       }
      st.close();
      con.close();
   
       
        }catch(SQLException e){
        e.printStackTrace();
        }
   
  %>

</body>
</html>


2)Register.jsp
 <%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
    pageEncoding=”ISO-8859-1″ %>
    <%@ page language=”java” %>
<%@ page session=”true” %>
<%@ page import=”java.sql.*,java.io.*”%>
<%@ page import =”javax.sql.*” %>
   
<!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 FirstName = request.getParameter(“fname”);
     String LastName = request.getParameter(“lname”);
     String Username = request.getParameter(“uname”);
     String Password = request.getParameter(“pass”);
     String Email = request.getParameter(“email”);
     String MobileNo = request.getParameter(“mno”);
     String City = request.getParameter(“ct”);
   
  try{
    Class.forName(“com.mysql.jdbc.Driver”);
    Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/registration”,”root”,”root”);
   
    ResultSet rs;
   
String Query  = “Insert into register(FirstName,LastName,Username,Password,Email,MobileNo,City)values(?,?,?,?,?,?,?)”;
    PreparedStatement st = con.prepareStatement(Query);
   
    st.setString(1, FirstName);
    st.setString(2, LastName);
    st.setString(3, Username);
    st.setString(4, Password);
    st.setString(5, Email);
    st.setString(6, MobileNo);
    st.setString(7, City);

    int result = st.executeUpdate();
   
    out.println(“User register Successfully”);
    System.out.println(“Register Successfully”);
   
    st.close();
    con.close();
   
  }catch(SQLException e){
        e.printStackTrace();
    }
 %>
</body>
</html>

3)Table created in database:
    My database name is:- registration
    and my table name is:- register
    Here is table:-




Add a Comment

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