Implementing Login functionality in JDBC using MVC Architecture

Implementing Login functionality in JDBC using MVC Architecture
Model Class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.javaforlearn;
 
public class Student_info 
{
 
	String username;
	String password;
 
	public String getUsername() 
	{
		return username;
	}
 
	public void setUsername(String username) 
	{
		this.username = username;
	}
 
	public String getPassword() 
	{
		return password;
	}
 
	public void setPassword(String password)
	{
		this.password = password;
	}
}
DAO(data access object) class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.javaforlearn;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.mysql.jdbc.Driver;
 
public class Student_DAO 
{
 
 
	Connection con=null;
	PreparedStatement p =null;
	ResultSet rs=null;
 
 
	public ResultSet authentication(String user, String pass)
 
	{
 
		/*
		 * load the driver
		 */
 
		try 
		{
 
 
			Driver driverref=new Driver();
			DriverManager.registerDriver(driverref);
 
 
 
			String dburl="jdbc:mysql://bhavesh-pc:3306/test?user=j2ee&password=j2ee";
			con=DriverManager.getConnection(dburl);
 
			/*
			 * issue sql query via driver
			 */
 
			String q = " select *from password_info where user=? and current_password=? ";
 
			 p= con.prepareStatement(q);
 
			 p.setString(1,user);
			 p.setString(2,pass);
 
		    rs=p.executeQuery();
 
 
		}
		catch (Exception e)
	    {
 
			e.printStackTrace();
		}
 
		return rs;
 
	}
}
Main class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.javaforlearn;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class Login 
{
 
   public static void main(String[] args)
   {
 
 
 
	   Student_info rf=new Student_info();
 
	   rf.setUsername("bhavesh");
	   rf.setPassword("4321");
 
	   Student_DAO rf1=new Student_DAO();
 
	   ResultSet rs=rf1.authentication(rf.getUsername(),rf.getPassword());
 
	   try 
	   {
 
		if(rs.next())
		   {
			   System.out.println("Login successful");
 
		   }
		else
		{
			System.out.println("Wrong credential, Try again");	
		}
 
 
	  } 
 
	   catch (SQLException e) 
	   {
 
		e.printStackTrace();
	   }
 
   }
 
}
 
output:
Login successful

Leave a Reply

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