Created
September 25, 2013 19:17
-
-
Save Muwbi/6704614 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.*; | |
public class StorageLib { | |
/** | |
* Der MySQL-Host, z.B. 'localhost' oder 'vweb02.nitrado.net' | |
*/ | |
private static final String MYSQL_HOST = "localhost"; | |
/** | |
* Der MySQL-Benutzer, z.B. 'root' oder 'ni12345_1sql1' | |
*/ | |
private static final String MYSQL_USER = "root"; | |
/** | |
* Das MySQL-Passwort | |
*/ | |
private static final String MYSQL_PASSWORD = "123"; | |
/** | |
* Der Name der MySQL-Datebank, z.B. 'db' oder 'ni12345_1sql1' | |
*/ | |
private static final String MYSQL_DATABASE = "db"; | |
/** | |
* Der Name der MySQL-Tabelle, z.B. 'money' | |
*/ | |
private static final String MYSQL_TABLE = "money"; | |
/** | |
* Die Spalte, in der die Spieler-Namen gespeichert werden | |
*/ | |
private static final String USER_COLUMN = "Player"; | |
/** | |
* Die Spalte, in der die Zahlen gespeichert werden | |
*/ | |
private static final String INT_COLUMN = "Zahl"; | |
private static Connection connection; | |
public static void load() { | |
try { | |
Class.forName("com.mysql.jdbc.Driver"); | |
} catch (ClassNotFoundException ex) { | |
ex.printStackTrace(); | |
} | |
try { | |
connection = DriverManager.getConnection("jdbc:mysql://" + MYSQL_HOST + "/" + MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD); | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static void set(String playerName, int number) { | |
try { | |
PreparedStatement checkStatement = connection.prepareStatement("SELECT ? FROM ? WHERE ? = ?;"); | |
checkStatement.setString(1, INT_COLUMN); | |
checkStatement.setString(2, MYSQL_TABLE); | |
checkStatement.setString(3, USER_COLUMN); | |
checkStatement.setString(4, playerName); | |
if (checkStatement.executeQuery().next()) { | |
PreparedStatement updateStatement = connection.prepareStatement("UPDATE ? SET ? = ? WHERE ? = ?;"); | |
updateStatement.setString(1, MYSQL_TABLE); | |
updateStatement.setString(2, INT_COLUMN); | |
updateStatement.setInt(3, number); | |
updateStatement.setString(4, USER_COLUMN); | |
updateStatement.setString(5, playerName); | |
updateStatement.execute(); | |
} else { | |
PreparedStatement createStatement = connection.prepareStatement("INSERT INTO ? (?, ?) VALUES (?, ?);"); | |
createStatement.setString(1, MYSQL_TABLE); | |
createStatement.setString(2, USER_COLUMN); | |
createStatement.setString(3, INT_COLUMN); | |
createStatement.setString(4, playerName); | |
createStatement.setInt(5, number); | |
createStatement.execute(); | |
} | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
public static int get(String playerName) { | |
try { | |
PreparedStatement selectStatement = connection.prepareStatement("SELECT ? FROM ? WHERE ? = ?;"); | |
selectStatement.setString(1, INT_COLUMN); | |
selectStatement.setString(2, MYSQL_TABLE); | |
selectStatement.setString(3, USER_COLUMN); | |
selectStatement.setString(4, playerName); | |
ResultSet selectedResult = selectStatement.executeQuery(); | |
if (selectedResult.next()) { | |
return selectedResult.getInt(1); | |
} | |
} catch (SQLException ex) { | |
ex.printStackTrace(); | |
} | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment