feature: #1319 add table module pattern (#1742)

* modify table module pattern

* fix code smells

* resolve conversation

Co-authored-by: tao-sun2 <sustc18st@gmai.com>
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
This commit is contained in:
Tao
2021-05-18 03:06:35 +08:00
committed by GitHub
parent 122e6edb38
commit e498c25675
11 changed files with 671 additions and 0 deletions
@@ -0,0 +1,96 @@
package com.iluwatar.tablemodule;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import lombok.extern.slf4j.Slf4j;
/**
* This class organizes domain logic with the user table in the
* database. A single instance of this class contains the various
* procedures that will act on the data.
*/
@Slf4j
public class UserTableModule {
/**
* Public element for creating schema.
*/
public static final String CREATE_SCHEMA_SQL =
"CREATE TABLE IF NOT EXISTS USERS (ID NUMBER, USERNAME VARCHAR(30) "
+ "UNIQUE,PASSWORD VARCHAR(30))";
/**
* Public element for deleting schema.
*/
public static final String DELETE_SCHEMA_SQL = "DROP TABLE USERS IF EXISTS";
private final DataSource dataSource;
/**
* Public constructor.
*
* @param userDataSource the data source in the database
*/
public UserTableModule(final DataSource userDataSource) {
this.dataSource = userDataSource;
}
/**
* Login using username and password.
*
* @param username the username of a user
* @param password the password of a user
* @return the execution result of the method
* @throws SQLException if any error
*/
public int login(final String username, final String password)
throws SQLException {
var sql = "select count(*) from USERS where username=? and password=?";
ResultSet resultSet = null;
try (var connection = dataSource.getConnection();
var preparedStatement =
connection.prepareStatement(sql)
) {
var result = 0;
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
result = resultSet.getInt(1);
}
if (result == 1) {
LOGGER.info("Login successfully!");
} else {
LOGGER.info("Fail to login!");
}
return result;
} finally {
if (resultSet != null) {
resultSet.close();
}
}
}
/**
* Register a new user.
*
* @param user a user instance
* @return the execution result of the method
* @throws SQLException if any error
*/
public int registerUser(final User user) throws SQLException {
var sql = "insert into USERS (username, password) values (?,?)";
try (var connection = dataSource.getConnection();
var preparedStatement =
connection.prepareStatement(sql)
) {
preparedStatement.setString(1, user.getUsername());
preparedStatement.setString(2, user.getPassword());
var result = preparedStatement.executeUpdate();
LOGGER.info("Register successfully!");
return result;
}
}
}