1. Users class
  2. Model class
  3. DAO class interface

For Energy Billing System I want to add login screen for the user that will be displayed before access to user panel. The user can enter username and password, click submit button to proceed the login. For this purpose, I create a simple database in mysql.

Users class:

Create a table called: users, by using fallowing sql:

CREATE TABLE users (                   
          id int(11) NOT NULL AUTO_INCREMENT,  
          user_name varchar(50) NOT NULL,      
          user_password varchar(50) NOT NULL,  
          PRIMARY KEY (`id`)                     
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Checking information about table that we created:

mysql> show tables;
+------------------+
| Tables_in_spring |
+------------------+
| users            |
+------------------+
1 row in set (0,01 sec)

mysql> select *  from users;
+----+-----------+---------------+
| id | user_name | user_password |
+----+-----------+---------------+
|  1 | tom       | tom           |
|  2 | john      | john          |
+----+-----------+---------------+
2 rows in set (0,00 sec)

mysql> describe users;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| id            | int(11)     | NO   | PRI | NULL    | auto_increment |
| user_name     | varchar(50) | NO   |     | NULL    |                |
| user_password | varchar(50) | NO   |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
3 rows in set (0,00 sec)

Added two records into users table:

insert into users (user_name,user_password) values ('tom','tom');
insert into users (user_name,user_password) values ('john','john');

Model class:

Now in a database, we should have two records. Next step is to create a model class. This Entity is mapped to the users table.

@Entity
@Table(name = "users")
@SuppressWarnings("serial")
public class Users implements Serializable {

	@Id
	@GeneratedValue
	@Column(name = "id", length = 11 )
	private Long id;

	@Column(name = "user_name")
	String userName;

	@Column(name = "user_password")
	String userPassword;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPassword() {
		return userPassword;
	}

	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}
}

DAO class interface:

Created DAO class interface:

package com.billingsystem.dao;

public interface LoginDAO {
	public boolean checkLogin(String userName, String userPassword);
}

For now is a small part of login implementation. In the next post, I will present complete implementation.


Reference:

  1. Getting started with mysql