Implementing Login functionality in JDBC using MVC Architecture

In this post we will see the practical approach of implementing Login facility using MVC pattern.

Model Class

[pastacode lang=”java” manual=”package%20com.javaforlearn%3B%0A%0Apublic%20class%20Student_info%20%0A%7B%0A%0A%09String%20username%3B%0A%09String%20password%3B%0A%09%0A%09public%20String%20getUsername()%20%0A%09%7B%0A%09%09return%20username%3B%0A%09%7D%0A%09%0A%09public%20void%20setUsername(String%20username)%20%0A%09%7B%0A%09%09this.username%20%3D%20username%3B%0A%09%7D%0A%09%0A%09public%20String%20getPassword()%20%0A%09%7B%0A%09%09return%20password%3B%0A%09%7D%0A%09%0A%09public%20void%20setPassword(String%20password)%0A%09%7B%0A%09%09this.password%20%3D%20password%3B%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

DAO (data access object) class

[pastacode lang=”java” manual=”package%20com.javaforlearn%3B%0A%0Aimport%20java.sql.Connection%3B%0Aimport%20java.sql.DriverManager%3B%0Aimport%20java.sql.PreparedStatement%3B%0Aimport%20java.sql.ResultSet%3B%0Aimport%20java.sql.SQLException%3B%0A%0Aimport%20com.mysql.jdbc.Driver%3B%0A%0Apublic%20class%20Student_DAO%20%0A%7B%0A%09%0A%09%0A%09Connection%20con%3Dnull%3B%0A%09PreparedStatement%20p%20%3Dnull%3B%0A%09ResultSet%20rs%3Dnull%3B%0A%09%0A%09%0A%09public%20ResultSet%20authentication(String%20user%2C%20String%20pass)%0A%09%0A%09%7B%0A%09%09%0A%09%09%2F*%0A%09%09%20*%20load%20the%20driver%0A%09%09%20*%2F%0A%09%09try%20%0A%09%09%7B%0A%09%09%09%0A%09%09%09Driver%20driverref%3Dnew%20Driver()%3B%0A%09%09%09DriverManager.registerDriver(driverref)%3B%0A%09%09%09%0A%09%09%09String%20dburl%3D%22jdbc%3Amysql%3A%2F%2Fbhavesh-pc%3A3306%2Ftest%3Fuser%3Dj2ee%26password%3Dj2ee%22%3B%0A%09%09%09con%3DDriverManager.getConnection(dburl)%3B%0A%09%09%09%0A%09%09%09%2F*%0A%09%09%09%20*%20issue%20sql%20query%20via%20driver%0A%09%09%09%20*%2F%0A%09%09%09%0A%09%09%09String%20q%20%3D%20%22%20select%20*from%20password_info%20where%20user%3D%3F%20and%20current_password%3D%3F%20%22%3B%0A%09%09%09%0A%09%09%09%20p%3D%20con.prepareStatement(q)%3B%0A%09%09%09%20%0A%09%09%09%20p.setString(1%2Cuser)%3B%0A%09%09%09%20p.setString(2%2Cpass)%3B%0A%09%09%09%09%09%09%0A%09%09%20%20%20%20rs%3Dp.executeQuery()%3B%0A%09%09%20%20%20%20%09%09%20%20%20%20%0A%09%09%7D%0A%09%09catch%20(Exception%20e)%0A%09%20%20%20%20%7B%0A%09%09%09%0A%09%09%09e.printStackTrace()%3B%0A%09%09%7D%0A%09%09%09%0A%09%09return%20rs%3B%09%09%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]


Main class

[pastacode lang=”java” manual=”package%20com.javaforlearn%3B%0A%0Aimport%20java.sql.ResultSet%3B%0Aimport%20java.sql.SQLException%3B%0A%0Apublic%20class%20Login%20%0A%7B%0A%09%0A%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%7B%0A%20%20%20%20%20%20%0A%09%20%20%20Student_info%20rf%3Dnew%20Student_info()%3B%0A%09%20%20%20%0A%09%20%20%20rf.setUsername(%22bhavesh%22)%3B%0A%09%20%20%20rf.setPassword(%224321%22)%3B%0A%09%20%20%20%0A%09%20%20%20Student_DAO%20rf1%3Dnew%20Student_DAO()%3B%0A%09%20%20%20%0A%09%20%20%20ResultSet%20rs%3Drf1.authentication(rf.getUsername()%2Crf.getPassword())%3B%0A%09%20%20%20%0A%09%20%20%20try%20%0A%09%20%20%20%7B%09%09%20%20%20%0A%09%09if(rs.next())%0A%09%09%20%20%20%7B%0A%09%09%09%20%20%20System.out.println(%22Login%20successful%22)%3B%09%09%09%0A%09%09%20%20%20%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09System.out.println(%22Wrong%20credential%2C%20Try%20again%22)%3B%09%0A%09%09%7D%09%09%09%09%0A%09%20%20%7D%20%09%0A%09%20%20%20catch%20(SQLException%20e)%20%0A%09%20%20%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%20%20%20%7D%09%09%09%0A%20%20%20%7D%09%09%0A%7D” message=”” highlight=”” provider=”manual”/]

output: Login successful

Leave a Reply