Example usage for java.sql ResultSet getCharacterStream

List of usage examples for java.sql ResultSet getCharacterStream

Introduction

In this page you can find the example usage for java.sql ResultSet getCharacterStream.

Prototype

java.io.Reader getCharacterStream(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.io.Reader object.

Usage

From source file:pl.psnc.synat.wrdz.realm.db.WrdzUserDatabaseHandler.java

/**
 * Authenticates user using username and password he provided and comparing it to the data in the user database.
 * /*from w  w w . j ava 2s. c om*/
 * @param username
 *            name of the user who is to be authenticated.
 * @param password
 *            password of the user who is to be authenticated.
 * @return whether or not user data is valid (passed user data matches data in the database).
 */
public boolean isUserValid(String username, char[] password) {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    boolean valid = false;
    byte[] salt = null;
    try {
        connection = getConnection();
        statement = connection.prepareStatement(saltQuery);
        statement.setString(1, username);
        resultSet = statement.executeQuery();
        if (resultSet.next()) {
            BigDecimal decimalSalt = resultSet.getBigDecimal(1);
            if (decimalSalt != null) {
                salt = decimalSalt.toBigIntegerExact().toByteArray();
            }
        }
    } catch (SQLException ex) {
        logger.log(Level.SEVERE, "Cannot validate user " + username + ", exception: " + ex.toString());
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "Cannot validate user", ex);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Invalid user " + username);
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "Cannot validate user", ex);
        }
    } finally {
        close(connection, statement, resultSet);
    }
    try {
        char[] hashedPassword = digestAuthHandler.hashPassword(password, salt);
        connection = getConnection();
        statement = connection.prepareStatement(passwordQuery);
        statement.setString(1, username);
        resultSet = statement.executeQuery();
        if (resultSet.next()) {
            Reader reader = resultSet.getCharacterStream(1);
            char[] retrievedPassword = extractFromReader(reader);
            valid = digestAuthHandler.comparePasswords(hashedPassword, retrievedPassword);
        }
    } catch (SQLException ex) {
        logger.log(Level.SEVERE, "Cannot validate user " + username + ", exception: " + ex.toString());
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "Cannot validate user", ex);
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Invalid user " + username);
        if (logger.isLoggable(Level.FINE)) {
            logger.log(Level.FINE, "Cannot validate user", ex);
        }
    } finally {
        close(connection, statement, resultSet);
    }
    return valid;
}