Java JDBC Reds Connection getUserFace(String sUserName)

Here you can find the source of getUserFace(String sUserName)

Description

returns default face for player

License

Open Source License

Parameter

Parameter Description
sUserName username of player

Exception

Parameter Description
SQLException if database is horked

Return

1 if user account doesn't exist

Declaration

public static int getUserFace(String sUserName) throws SQLException 

Method Source Code

//package com.java2s;
/**//from   w w w .  j  av a  2  s . c  om
 * Open Settlers - an open implementation of the game Settlers of Catan
 * Copyright (C) 2003  Robert S. Thomas
 * Portions of this file Copyright (C) 2009-2010 Jeremy D Monin <jeremy@nand.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. **/

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import java.sql.SQLException;

public class Main {
    public static Connection connection = null;
    /**
     * Retain the URL (default, or passed via props to {@link #initialize(String, String, Properties)}).
     * @since 1.1.09
     */
    private static String dbURL = null;
    /**
     * This flag indicates that the connection should be valid, yet the last
     * operation failed. Methods will attempt to reconnect prior to their
     * operation if this is set.
     */
    private static boolean errorCondition = false;
    /** Cached username used when reconnecting on error */
    private static String userName;
    /** Cached password used when reconnecting on error */
    private static String password;
    private static String CREATE_ACCOUNT_COMMAND = "INSERT INTO users VALUES (?,?,?,?,?,?,?,?,?);";
    private static String RECORD_LOGIN_COMMAND = "INSERT INTO logins VALUES (?,?,?);";
    private static String USER_PASSWORD_QUERY = "SELECT password FROM users WHERE ( users.nickname = ? );";
    private static String HOST_QUERY = "SELECT nickname FROM users WHERE ( users.host = ? );";
    private static String LASTLOGIN_UPDATE = "UPDATE users SET lastlogin = ? WHERE nickname = ? ;";
    private static String SAVE_GAME_COMMAND = "INSERT INTO games VALUES (?,?,?,?,?,?,?,?,?,?,?);";
    private static String ROBOT_PARAMS_QUERY = "SELECT * FROM robotparams WHERE robotname = ?;";
    private static String RESET_HUMAN_STATS = "UPDATE users SET wins = 0, losses = 0, totalpoints = 0 WHERE nickname = ?;";
    private static String UPDATE_ROBOT_STATS = "UPDATE robotparams SET wins = wins + ?, losses = losses + ?, totalpoints = totalpoints + ? WHERE robotname = ?;";
    private static String UPDATE_USER_STATS = "UPDATE users SET wins = wins + ?, losses = losses + ?, totalpoints = totalpoints + ? WHERE nickname = ?;";
    private static String USER_FACE_QUERY = "SELECT face FROM users WHERE users.nickname = ?;";
    private static String USER_FACE_UPDATE = "UPDATE users SET face = ? WHERE nickname = ?;";
    private static PreparedStatement createAccountCommand = null;
    private static PreparedStatement recordLoginCommand = null;
    private static PreparedStatement userPasswordQuery = null;
    private static PreparedStatement hostQuery = null;
    private static PreparedStatement lastloginUpdate = null;
    private static PreparedStatement saveGameCommand = null;
    private static PreparedStatement robotParamsQuery = null;
    private static PreparedStatement resetHumanStats = null;
    private static PreparedStatement updateRobotStats = null;
    private static PreparedStatement updateUserStats = null;
    private static PreparedStatement userFaceQuery = null;
    private static PreparedStatement userFaceUpdate = null;

    /**
     * returns default face for player
     *
     * @param sUserName username of player
     *
     * @return 1 if user account doesn't exist
     *
     * @throws SQLException if database is horked
     */
    public static int getUserFace(String sUserName) throws SQLException {
        int face = 1;

        // ensure that the JDBC connection is still valid
        if (checkConnection()) {
            try {
                // fill in the data values to the Prepared statement
                userFaceQuery.setString(1, sUserName);

                // execute the Query
                ResultSet resultSet = userFaceQuery.executeQuery();

                // if no results, user is not authenticated
                if (resultSet.next()) {
                    face = resultSet.getInt(1);
                }

                resultSet.close();
            } catch (SQLException sqlE) {
                handleSQLException(sqlE);
            }
        }

        return face;
    }

    /**
     * Checks if connection is supposed to be present and attempts to reconnect
     * if there was previously an error.  Reconnecting closes the current
     * conection, opens a new one, and re-initializes the prepared statements.
     *
     * @return true if the connection is established upon return
     */
    private static boolean checkConnection() throws SQLException {
        if (connection != null) {
            return (!errorCondition) || connect();
        }

        return false;
    }

    /**
     * Common behavior for SQL Exceptions.
     */
    protected static void handleSQLException(SQLException x)
            throws SQLException {
        errorCondition = true;
        x.printStackTrace();
        throw x;
    }

    /**
     * initialize and checkConnection use this to get ready.
     */
    private static boolean connect() throws SQLException {
        connection = DriverManager.getConnection(dbURL, userName, password);

        errorCondition = false;

        // prepare PreparedStatements for queries
        createAccountCommand = connection
                .prepareStatement(CREATE_ACCOUNT_COMMAND);
        recordLoginCommand = connection
                .prepareStatement(RECORD_LOGIN_COMMAND);
        userPasswordQuery = connection
                .prepareStatement(USER_PASSWORD_QUERY);
        hostQuery = connection.prepareStatement(HOST_QUERY);
        lastloginUpdate = connection.prepareStatement(LASTLOGIN_UPDATE);
        saveGameCommand = connection.prepareStatement(SAVE_GAME_COMMAND);
        robotParamsQuery = connection.prepareStatement(ROBOT_PARAMS_QUERY);

        resetHumanStats = connection.prepareStatement(RESET_HUMAN_STATS);
        userFaceQuery = connection.prepareStatement(USER_FACE_QUERY);
        userFaceUpdate = connection.prepareStatement(USER_FACE_UPDATE);
        updateRobotStats = connection.prepareStatement(UPDATE_ROBOT_STATS);
        updateUserStats = connection.prepareStatement(UPDATE_USER_STATS);

        return true;
    }
}

Related

  1. createRedshiftTable(String redshiftURL, Properties loginProperties, String tableName, List fields)
  2. checkConnection()
  3. checkConnection()
  4. getRow(String username, String password, String url, String driver, String query, String[] params)
  5. getUserFromHost(String host)
  6. query(String sql, Object... args)
  7. queryForList(String sql, Object... args)