Description
DOCUMENT ME!
License
Open Source License
Parameter
Parameter | Description |
---|
gameName | DOCUMENT ME! |
player1 | DOCUMENT ME! |
player2 | DOCUMENT ME! |
player3 | DOCUMENT ME! |
player4 | DOCUMENT ME! |
score1 | DOCUMENT ME! |
score2 | DOCUMENT ME! |
score3 | DOCUMENT ME! |
score4 | DOCUMENT ME! |
startTime | DOCUMENT ME! |
Exception
Parameter | Description |
---|
SQLException | DOCUMENT ME! |
Return
true if the save succeeded
Declaration
public static boolean saveGameScores(String gameName, String player1,
String player2, String player3, String player4, short score1,
short score2, short score3, short score4,
java.util.Date startTime) throws SQLException
Method Source Code
//package com.java2s;
/**//w ww. ja v a 2 s . c o m
* Java Settlers - An online multiplayer version 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The author of this program can be reached at thomas@infolab.northwestern.edu
**/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
public class Main {
private 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 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;
/**
* DOCUMENT ME!
*
* @param gameName DOCUMENT ME!
* @param player1 DOCUMENT ME!
* @param player2 DOCUMENT ME!
* @param player3 DOCUMENT ME!
* @param player4 DOCUMENT ME!
* @param score1 DOCUMENT ME!
* @param score2 DOCUMENT ME!
* @param score3 DOCUMENT ME!
* @param score4 DOCUMENT ME!
* @param startTime DOCUMENT ME!
*
* @return true if the save succeeded
*
* @throws SQLException DOCUMENT ME!
*/
public static boolean saveGameScores(String gameName, String player1,
String player2, String player3, String player4, short score1,
short score2, short score3, short score4,
java.util.Date startTime) throws SQLException {
// TODO 6-player: save their scores too, if
// those fields are in the database.
// ensure that the JDBC connection is still valid
if (checkConnection()) {
try {
// fill in the data values to the Prepared statement
saveGameCommand.setString(1, gameName);
saveGameCommand.setString(2, player1);
saveGameCommand.setString(3, player2);
saveGameCommand.setString(4, player3);
saveGameCommand.setString(5, player4);
saveGameCommand.setShort(6, score1);
saveGameCommand.setShort(7, score2);
saveGameCommand.setShort(8, score3);
saveGameCommand.setShort(9, score4);
saveGameCommand.setTimestamp(10,
new Timestamp(startTime.getTime()));
// execute the Command
saveGameCommand.executeUpdate();
return true;
} catch (SQLException sqlE) {
errorCondition = true;
sqlE.printStackTrace();
throw sqlE;
}
}
return false;
}
/**
* 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(userName, password);
}
return false;
}
/**
* initialize and checkConnection use this to get ready.
*/
private static boolean connect(String user, String pswd)
throws SQLException {
connection = DriverManager.getConnection(dbURL, user, pswd);
errorCondition = false;
userName = user;
password = pswd;
// 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);
return true;
}
}
Related
- removeTimefromDate(Date inputDate)
- resetDayTime(Time date)
- resetNetworkTimeout(Connection rawConnection, Executor executor, int oldTimeout)
- roundDatetimeValue(Object value)
- roundSmallDateTimeValue(Object value)
- serialize(LocalDateTime date)
- serializeSqlTime(Time time)
- setTime(PreparedStatement statement, int index, Date value)
- sqlDateTimeAdd(java.util.Date date, long milliseconds)