java web project login and registration with JSP and servlet with Mysql

In this tutorial, we are going to cover complete Login and Registration activity with JSP and servlet and MySQL.

Here is complete video tutorial with practical which will show you how to create java application and connect it with MSQL a database. or an idea for java database connectivity(JDBC) with MySQL so please watch the complete video first.

 What are the 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 java database connectivity(JDBC) with MySQL.

Configure your  MYSQL database:

Create a new database with name “mydb” and table name “user”.


CREATE TABLE `mydb`.`user` (
`uid` INT NOT NULL AUTO_INCREMENT,
`uname` VARCHAR(45) NULL,
`upass` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
PRIMARY KEY (`uid`));

registration.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="Main">
<pre>

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

Password:<input name="pass" type="text">

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

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

</pre>
</form>
</body>
</html>

Create a servlet Main.java. and insert below code in get() or post() method. 


String name = request.getParameter("name");

String pass = request.getParameter("pass");

String email = request.getParameter("email");

MyDb db = new MyDb();

Connection con = db.getCon();

try {

Statement stmt =  con.createStatement();

stmt.executeUpdate("insert into user (uname,upass,email)values('"+name+"','"+pass+"','"+email+"')");

out.println("data inserted sucessfully");

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Create a class (MyDb.java).

This file will help you to understand how do create connection with mysql(java database connectivity).


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class MyDb {

public Connection con;

public Connection getCon(){

try {

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

con =  DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return con;

}}

Now, Registration Part is Completed now will code for login part.

Create login.jsp or login.html


<body>
<h1>Hello World!</h1>
<pre>
<form method="post" action="Login">
<input type="text" name = "uname">

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

User Pass : <input value="Submit" type="submit">
</form>

</pre>
</body>

Create a servlet Login.java and insert below code in get or post method.


String name = request.getParameter("uname");
String pass = request.getParameter("upass");
MyDb db = new MyDb();
Connection con = db.getCon();
try {
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from user where uname = '"+name+"'");
if(rs.next()){
out.println("Login Sucessfully");
}

} catch (SQLException ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

View Demo

I hope its now clear to you. how to create a login and registration page and java database connectivity(jdbc) with mysql.

3 Comments

Add a Comment

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

fifteen + sixteen =