Saturday, December 28, 2019

Complete user registration system using PHP and MySQL database

Hello everyone, In this tutorial, we will learn about the Complete user registration system using PHP and MySQL database.

The first thing we'll need to do is set up our database.
Create a database called registration. In the registration database, add a table called users. The user's table will take the following four fields.

  • id
  • username  -  varchar(100)
  • email  -  varchar(100)
  • password  -  varchar(100)


Or you can create it on the MySQL prompt using the following SQL script:

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `username` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Now create a folder called registration in a directory accessible to our server. i.e create the folder inside htdocs (If you are using XAMPP Server).

Now create some pages like index.php, register.php, login.php, server.php, error.php, style.css etc and go for next step.

Registering a user

Open the register.php file and paste the following code in it:
<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
  <title>Registration system PHP and MySQL</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="header">
      <h2>Register</h2>
  </div>
  
  <form method="post" action="register.php">
      <?php include('errors.php'); ?>
      <div class="input-group">
        <label>Username</label>
        <input type="text" name="username" value="<?php echo $username; ?>">
      </div>
      <div class="input-group">
        <label>Email</label>
        <input type="email" name="email" value="<?php echo $email; ?>">
      </div>
      <div class="input-group">
        <label>Password</label>
        <input type="password" name="password_1">
      </div>
      <div class="input-group">
        <label>Confirm password</label>
        <input type="password" name="password_2">
      </div>
      <div class="input-group">
        <button type="submit" class="btn" name="reg_user">Register</button>
      </div>
      <p>
          Already a member? <a href="login.php">Sign in</a>
      </p>
  </form>
</body>
</html>


As you can see in the head section, we are linking to a style.css file. Open up the style.css file and paste the following CSS in it:
* {
  margin: 0px;
  padding: 0px;
}
body {
  font-size: 120%;
  background: #F8F8FF;
}

.header {
  width: 30%;
  margin: 50px auto 0px;
  color: white;
  background: #5F9EA0;
  text-align: center;
  border: 1px solid #B0C4DE;
  border-bottom: none;
  border-radius: 10px 10px 0px 0px;
  padding: 20px;
}
form, .content {
  width: 30%;
  margin: 0px auto;
  padding: 20px;
  border: 1px solid #B0C4DE;
  background: white;
  border-radius: 0px 0px 10px 10px;
}
.input-group {
  margin: 10px 0px 10px 0px;
}
.input-group label {
  display: block;
  text-align: left;
  margin: 3px;
}
.input-group input {
  height: 30px;
  width: 93%;
  padding: 5px 10px;
  font-size: 16px;
  border-radius: 5px;
  border: 1px solid gray;
}
.btn {
  padding: 10px;
  font-size: 15px;
  color: white;
  background: #5F9EA0;
  border: none;
  border-radius: 5px;
}
.error {
  width: 92%;
  margin: 0px auto;
  padding: 10px;
  border: 1px solid #a94442;
  color: #a94442;
  background: #f2dede;
  border-radius: 5px;
  text-align: left;
}
.success {
  color: #3c763d;
  background: #dff0d8;
  border: 1px solid #3c763d;
  margin-bottom: 20px;
}



Let's now write the code that will receive information submitted from the form and store (register) the information in the database. As promised earlier, we do this in the server.php file.

Open server.php and paste this code in it:

server.php
<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array();

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'registration');

// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
  $password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($username)) { array_push($errors, "Username is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($password_1)) { array_push($errors, "Password is required"); }
  if ($password_1 != $password_2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);
 
  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
      $password = md5($password_1);//encrypt the password before saving in the database

      $query = "INSERT INTO users (username, email, password)
                VALUES('$username', '$email', '$password')";
      mysqli_query($db, $query);
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: index.php');
  }
}
// ... 



Sessions are used to track logged in users and so we include a session_start() at the top of the file.

The comments in the code pretty much explain everything, but I'll highlight a few things here.
The if statement determines if the reg_user button on the registration form is clicked. Remember, in our form, the submit button has a name attribute set to reg_user and that is what we are referencing in the if statement.
All the data is received from the form and checked to make sure that the user correctly filled the form. Passwords are also compared to make sure they match.
If no errors were encountered, the user is registered in the users table in the database with a hashed password. The hashed password is for security reasons. It ensures that even if a hacker manages to gain access to your database, they would not be able to read your password.
But error messages are not displaying now because our errors.php file is still empty. To display the errors, paste this code in the errors.php file.

<?php  if (count($errors) > 0) : ?>
  <div class="error">
      <?php foreach ($errors as $error) : ?>
        <p><?php echo $error ?></p>
      <?php endforeach ?>
  </div>
<?php  endif ?>







When a user is registered in the database, they are immediately logged in and redirected to the index.php page.
And that's it for registration. Let's look at user login.

Login user:

Logging a user in is an even easier thing to do. Just open the login page and put this code inside it:

<?php include('server.php') ?>
<!DOCTYPE html>
<html>
<head>
  <title>Registration system PHP and MySQL</title>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
  <div class="header">
      <h2>Login</h2>
  </div>
   
  <form method="post" action="login.php">
      <?php include('errors.php'); ?>
      <div class="input-group">
          <label>Username</label>
          <input type="text" name="username" >
      </div>
      <div class="input-group">
          <label>Password</label>
          <input type="password" name="password">
      </div>
      <div class="input-group">
          <button type="submit" class="btn" name="login_user">Login</button>
      </div>
      <p>
          Not yet a member? <a href="register.php">Sign up</a>
      </p>
  </form>
</body>
</html>
 

 

Everything on this page is quite similar to the register.php page. Now the code that logs the user in is to be written in the same server.php file. So open the server.php file and add this code at the end of the file:

 

// LOGIN USER
if (isset($_POST['login_user'])) {
  $username = mysqli_real_escape_string($db, $_POST['username']);
  $password = mysqli_real_escape_string($db, $_POST['password']);

  if (empty($username)) {
    array_push($errors, "Username is required");
  }
  if (empty($password)) {
    array_push($errors, "Password is required");
  }

  if (count($errors) == 0) {
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: index.php');
    }else {
      array_push($errors, "Wrong username/password combination");
    }
  }
}

?>
 

Again all this does is check if the user has filled the form correctly, verifies that their credentials match a record from the database and logs them in if it does. After logging in, the user is redirected them to the index.php file with a success message. 

Now let's see what happens in the index.php file. Open it up and paste the following code in it:

<?php
  session_start();

  if (!isset($_SESSION['username'])) {
      $_SESSION['msg'] = "You must log in first";
      header('location: login.php');
  }
  if (isset($_GET['logout'])) {
      session_destroy();
      unset($_SESSION['username']);
      header("location: login.php");
  }
?>
<!DOCTYPE html>
<html>
<head>
    <title>Home</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div class="header">
    <h2>Home Page</h2>
</div>
<div class="content">
      <!-- notification message -->
      <?php if (isset($_SESSION['success'])) : ?>
      <div class="error success" >
          <h3>
          <?php
              echo $_SESSION['success'];
              unset($_SESSION['success']);
          ?>
          </h3>
      </div>
      <?php endif ?>

    <!-- logged in user information -->
    <?php  if (isset($_SESSION['username'])) : ?>
        <p>Welcome <strong><?php echo $_SESSION['username']; ?></strong></p>
        <p> <a href="index.php?logout='1'" style="color: red;">logout</a> </p>
    <?php endif ?>
</div>
       
</body>
</html>
 

 

The first if statement checks if the user is already logged in. If they are not logged in, they will be redirected to the login page. Hence this page is accessible to only logged in users. If you'd like to make any page accessible only to logged in users, all you have to do is place this if statement at the top of the file. The second if statement checks if the user has clicked the logout button. If yes, the system logs them out and redirects them back to the login page.

And that's it!!

  •  
  • Add to Phrasebook
    • No word lists for English -> Bengali...
    • Create a new word list...
  • Copy

Wednesday, September 4, 2019

Student Faculty file sharing web project using JSP, SERVLET and MYSQL


Project Name: Student-Faculty File sharing portal Technology: JSP, SERVLET, MYSQL Features : 1. Student Section : a)Student registration with validation b)Student Login c)Access Books, Notice, Result d) Discuss with other students e) Mail to Faculty Members 2. Faculty Section : a) Login b)Upload books,notice, Result c)Discuss with others members d)View reports e)Manage Students

"আমার হৃদয়েশ্বরী" বাংলা ভালবাসার কবিতা - Alamgir Hossain(আলমগীর হোসেন)

হঠাৎ পরিচয়, কিন্তু আজ জলন্ত শিশিরবিন্দু হতে সুর্যডোবা গোধূলি পর্যন্ত আমি তোমায় খুঁজে ফিরি । আমি লালন করি তোমার অস্তিত্বকে অনুষ্ণ, উষ্ণ প্রতিদিন পুরোটা রজনীতে । কোকিলীয় আবেগী কন্ঠে যখন ভেসে আসে তোমার ভালবাসার অনুনয় --- আমি না, থাকি না তখন এ ব্রহ্মাণ্ডে ভেসে বেড়ায় মুক্তমনার মত তোমার ভুবনে । কি এমন মোহিনীশক্তিতে আবদ্ধ করেছো শুনি !!! আমার যে সবকিছু এখন শুধুই তুমি । ওগো – প্রণয়িনী কি আছে তোমার ওই যাদুমাখা ঠোঁটে...!!!! আমার যে ইচ্ছে করে সারাক্ষণ আলতো ছোয়ায় স্পর্শ করতে । দিব মালিকানা তোমায়, আমার এই মর্মদেশের যতন করে রেখো তুমি, তোমার মত করে । নিরন্তর সময়ে তোমায় নিয়ে লালন করা ভালবাসায় আমি যে খুঁজে পায় সুখের ঠিকানা । রাখব তোমায় আমার চিত্তের সেই মনিকোঠায় যেখানে তুমি, আমি হব একাকার শক্তি নিয়ে আমাদের ভালবাসার । একবারও ভেবেছো কী ? সেদিন ছিলে অপরিচিতা--- আজ তুমি আমার হৃদয়ের হৃদয়েশ্বরী ।

Resolved versions for app (26.0.0) and test app (27.1.1) differ problem solved in Android Studio

Resolved versions for app (26.0.0) and test app (27.1.1) differ problem solved in Android Studio
(Solution of Android Studio Problem)

Friday, June 14, 2019

Insert, update, delete in jsp and servlet with mysql database

Database:
database name: db_school

create table tbl_student(
 id int not null auto_increment,
name varchar(255),
email varchar(255),
mobile varchar(255),
roll int,
subject varchar(255),
university varchar(255),
primary key(id)
);



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>Md. Alamgir Hossain</title>
</head>
<style>
    button {
          height: 50px;
          width: 100px;
}  
</style>
<body>
  
    <h1 align="center">Welcome to Student Management<br>
    <a href="register.jsp"><button>Register</button></a>
  
    </h1>
</body>
</html>

register.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>Registration</title>
</head>
<body>
    <h1 align="center">Complete Registration with Correct Information</h1>
    <form action="RegisterServ" method="post" name="registerForm">
        <table align="center" border="1">
            <tr width="100%">
                <td>Name:</td>
                <td><input type="text" name="name" placeholder="Enter your name--"></td>
            </tr>
            <tr width="100%">
                <td>Email:</td>
                <td><input type="text" name="email" placeholder="Enter your email--"></td>
            </tr>
            <tr width="100%">
                <td>Mobile:</td>
                <td><input type="text" name="mobile" placeholder="Enter your mobile--"></td>
            </tr>
            <tr width="100%">
                <td>Roll:</td>
                <td><input type="text" name="roll" placeholder="Enter your roll---"></td>
            </tr>
            <tr width="100%">
                <td>Subject:</td>
                <td><input type="text" name="subject" placeholder="Enter your subject---"></td>
            </tr>
            <tr width="100%">
                <td>University:</td>
                <td><input type="text" name="university" placeholder="Enter your university"></td>
            </tr>
        </table>
        <table align="center">
            <tr width="100%">
                <td><b><input type="submit" value="Submit" name="registerSubmit"></b></td>
            </tr>
        </table>
    </form>
</body>
</html>

home.jsp


<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ 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>All Registered</title>
</head>
<body>
<%
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
%>
    <h1 align="center">All registered members are</h1>
    <table align="center" name="tblHome" border="1">
        <tr width="100%">
            <td align="center">Name</td>
            <td align="center">Email</td>
            <td align="center">Number</td>
            <td align="center">Roll</td>
            <td align="center">Subject</td>
            <td align="center">University</td>
            <td align="center">Actions</td>
        </tr>
        <%
            conn = DBConnection.createConnection();
            String selectQuery = "SELECT * FROM tbl_student";
            pst = conn.prepareStatement(selectQuery);
            rs = pst.executeQuery();
          
            while(rs.next()){
        %>
            <tr width="100%">
                <td align="center"><%=rs.getString("name") %></td>
                <td align="center"><%=rs.getString("email") %></td>
                <td align="center"><%=rs.getString("mobile") %></td>
                <td align="center"><%=rs.getString("roll") %></td>
                <td align="center"><%=rs.getString("subject") %></td>
                <td align="center"><%=rs.getString("university") %></td>
                <td align ="center">
                    <a href="edit.jsp?roll=<%=rs.getString("roll")%>">Edit</a>
                    <a href="delete.jsp?rol=<%=rs.getString("roll")%>">Delete</a>
                </td>
            </tr>
        <%} %>
    </table>
</body>
</html>

delete.jsp


<%@page import="java.sql.Statement"%>
<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.Connection"%>
<%
    String rolll = request.getParameter("rol");
    int roll = Integer.parseInt(rolll);
    Connection conn = DBConnection.createConnection();
    Statement st = conn.createStatement();
    st.executeUpdate("delete from tbl_student where roll = '"+roll+"'");
    //st.executeUpdate(dlteQuery);
    out.println("Deleted!!");
    response.sendRedirect("home.jsp");
%>

edit.jsp

<%@page import="com.Util.DBConnection"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ 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>Update Information</title>
</head>
<body>
<%
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
  
    String roll = request.getParameter("roll");
    int rolll = Integer.parseInt(roll);
%>
<h1 align="center">Update information with Correct Info</h1>
    <form action="updateServ" method="post" name="registerForm">
    <%
        conn = DBConnection.createConnection();
        String sql = "select * from tbl_student where roll='"+rolll+"'";
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        while(rs.next()){
    %>
        <table align="center" border="1">
            <tr width="100%">
                <td>Name:</td>
                <td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
            </tr>
            <tr width="100%">
                <td>Email:</td>
                <td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
            </tr>
            <tr width="100%">
                <td>Mobile:</td>
                <td><input type="text" name="mobile" value="<%=rs.getString("mobile")%>"></td>
            </tr>
            <tr width="100%">
                <td>Roll:</td>
                <td><input type="text" name="roll" value="<%=rs.getString("roll")%>"></td>
            </tr>
            <tr width="100%">
                <td>Subject:</td>
                <td><input type="text" name="subject" value="<%=rs.getString("subject")%>"></td>
            </tr>
            <tr width="100%">
                <td>University:</td>
                <td><input type="text" name="university" value="<%=rs.getString("university")%>"></td>
            </tr>
        </table>
        <%} %>
        <table align="center">
            <tr width="100%">
                <td><b><input type="submit" value="Update" name="registerSubmit"></b></td>
            </tr>
        </table>
    </form>
    </body>
</html>

RegisterServ.java//com.Controller

package com.Controller;

import java.io.IOException;

import java.io.PrintWriter;

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 com.Dao.RegisterDao;
import com.Model.Register;

/**
 * Servlet implementation class RegisterServ
 */
@WebServlet("/RegisterServ")
public class RegisterServ extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServ() {
        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 email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String roll = request.getParameter("roll");
        String subject = request.getParameter("subject");
        String university = request.getParameter("university");
      
        Register register = new Register();
        register.setName(name);
        register.setEmail(email);
        register.setMobile(mobile);
        register.setRoll(roll);
        register.setSubject(subject);
        register.setUniversity(university);
      
        RegisterDao registerDao = new RegisterDao();
        String return_message;
        try {
            return_message = registerDao.Userregister(register);
            if(return_message.equals("SUCCESS")) {
                request.getRequestDispatcher("/home.jsp").forward(request, response);
            }else {
                request.setAttribute("errMessage", return_message);
                request.getRequestDispatcher("/register.jsp").forward(request, response);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
      
      
    }

}

updateServ.java//com.Controller

package com.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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 com.Util.DBConnection;

/**
 * Servlet implementation class updateServ
 */
@WebServlet("/updateServ")
public class updateServ extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public updateServ() {
        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 email = request.getParameter("email");
        String number = request.getParameter("mobile");
        String rollll = request.getParameter("roll");
        String subject = request.getParameter("subject");
        String university = request.getParameter("university");
        int rr = Integer.parseInt(rollll);
        Connection conn = null;
        PreparedStatement pst = null;
      
        try {
            conn = DBConnection.createConnection();
            String updateQuery = "update tbl_student set name=?, email=?, mobile=?, subject=?, university=? where roll='"+rr+"'";
            pst = conn.prepareStatement(updateQuery);
            if(rollll!=null){
              
                String updateQuery1 = "update tbl_student set name=?, email=?, mobile=?, subject=?, university=? where roll='"+rr+"'";
                pst = conn.prepareStatement(updateQuery1);
                pst.setString(1, name);
                pst.setString(2, email);
                pst.setString(3, number);
                //pst.setString(4, rollll);
                pst.setString(4, subject);
                pst.setString(5, university);
                //pst.setString(7, rollll);
                pst.executeUpdate();
                out.println("Updated");
                //response.sendRedirect("home.jsp");
                request.getRequestDispatcher("/home.jsp").forward(request, response);
            }
          
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
    }

}

RegisterDao.java//com.Dao

package com.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.Model.Register;
import com.Util.DBConnection;

public class RegisterDao {
    public String Userregister(Register register) throws ClassNotFoundException {
        String name = register.getName();
        String email = register.getEmail();
        String mobile = register.getMobile();
        String roll = register.getRoll();
        String subject = register.getSubject();
        String university = register.getUniversity();
      
        Connection conn = null;
        PreparedStatement pSt = null;
      
        try {
            conn = DBConnection.createConnection();
            String insertQuery = "insert into tbl_student(name, email, mobile, roll, subject, university) values(?,?,?,?,?,?)";
            pSt = conn.prepareStatement(insertQuery);
            pSt.setString(1, name);
            pSt.setString(2, email);
            pSt.setString(3, mobile);
          
            int r = Integer.parseInt(roll);
            pSt.setInt(4, r);
            pSt.setString(5, subject);
            pSt.setString(6, university);
          
            int i = pSt.executeUpdate();
          
            if(i!=0) {
                return "SUCCESS";
            }
          
        } catch (SQLException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
      
        return "Something Went qrong!!!";
    }
}

Register.java//com.Model

package com.Model;

public class Register {
    private String name, email, mobile, roll, subject, university;
  
    public String getName() {
        return name;
    }
    public String getEmail() {
        return email;
    }
    public String getMobile() {
        return mobile;
    }
    public String getRoll() {
        return roll;
    }
    public String getSubject() {
        return subject;
    }
    public String getUniversity() {
        return university;
    }
  
    ///Set method start
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    public void setRoll(String roll) {
        this.roll = roll;
    }
    public void setSubject(String subject) {
        this.subject = subject;
    }
    public void setUniversity(String university) {
        this.university = university;
    }
}

DBConnection.java//com.Util


package com.Util;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.SQLException;

public class DBConnection {
    public static Connection createConnection() throws ClassNotFoundException {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/db_school";
        String user = "root";
        String password = "";
      
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("Connected");
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

mysql jdbc connector download link: