Here you can find the source of checkConnection()
private static boolean checkConnection() throws SQLException
//package com.java2s; /**// w w w . j av a 2 s .c o m * 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.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; /** * 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; } /** * 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; } }