Verify the user's username and password from user table. - Java java.sql

Java examples for java.sql:Table

Description

Verify the user's username and password from user table.

Demo Code


//package com.java2s;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /**// w w w.j  a  va 2  s  . co m
     * Verify the user's username and password.
     * @param username the username
     * @param password the password, MD5 twice
     * @param statement the Statement connected to the database
     * @return the user's type if the password is correct, null if the authentication is failed
     * @throws SQLException
     */
    public static String authenticate(String username, String password,
            Statement statement) {
        try {
            ResultSet rs = statement
                    .executeQuery("SELECT type FROM users WHERE userid='"
                            + username + "' AND encryptedpassword='"
                            + password + "';");
            if (rs.next()) {
                return rs.getString("type");
            } else
                return null;
        } catch (SQLException e) {
            return null;
        }
    }
}

Related Tutorials