Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerSQL {
- public static Connection conn;
- public static void registerPlayer(Player p){
- ResultSet rs = executeQuery("SELECT PlayerName FROM playersql WHERE PlayerName='" + p.getName() + "'");
- try {
- if(!rs.next()){
- executeUpdate("INSERT INTO playersql (PlayerName,Wetoins,Weteys) VALUES ('" + p.getName() + "',0,0)");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static int getWetoins(Player p){
- ResultSet coinsRS = executeQuery("SELECT Wetoins FROM playersql WHERE PlayerName='" + p.getName() + "'");
- int coins = 0;
- try {
- while(coinsRS.next()){
- coins = coinsRS.getInt("Wetoins");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return coins;
- }
- public static void addWetoins(Player p,int amount){
- executeUpdate("UPDATE playersql SET Wetoins=" + (getWetoins(p)+amount) + " WHERE PlayerName='" + p.getName() + "'");
- }
- public static void removeWetoins(Player p,int amount){
- executeUpdate("UPDATE playersql SET Wetoins=" + (getWetoins(p)-amount) + " WHERE PlayerName='" + p.getName() + "'");
- }
- public static int getWeteys(Player p){
- ResultSet coinsRS = executeQuery("SELECT Weteys FROM playersql WHERE PlayerName='" + p.getName() + "'");
- int coins = 0;
- try {
- while(coinsRS.next()){
- coins = coinsRS.getInt("Weteys");
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return coins;
- }
- public static void addWeteys(Player p,int amount){
- executeUpdate("UPDATE playersql SET Weteys=" + (getWeteys(p)+amount) + " WHERE PlayerName='" + p.getName() + "'");
- }
- public static void removeWeteys(Player p,int amount){
- executeUpdate("UPDATE playersql SET Weteys=" + (getWeteys(p)-amount) + " WHERE PlayerName='" + p.getName() + "'");
- }
- public static void connect(String host,Integer port,String USER,String PASS,String DB_NAME){
- String DB = "jdbc:mysql://" + host + ":" + port + "/" +DB_NAME;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Bukkit.getLogger().info("About to connect to database");
- conn = DriverManager.getConnection(DB + "?autoReconnect=true", USER, PASS);
- Bukkit.getLogger().info("Successfully connected.");
- conn.createStatement();
- }catch(SQLException e){
- e.printStackTrace();
- }catch(ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static Statement createStatement(){
- Statement s=null;
- try {
- s = conn.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return s;
- }
- public static void closeConnection() {
- try {
- if( conn != null && !conn.isClosed() ) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static ResultSet executeQuery(String q){
- ResultSet rs = null;
- try {
- rs = createStatement().executeQuery(q);
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getLogger().warning("[Coins] Error occured while trying to execute query: \"" + q + "\"");
- }
- return rs;
- }
- public static void executeUpdate(String u){
- try {
- createStatement().executeUpdate(u);
- } catch (SQLException e) {
- e.printStackTrace();
- Bukkit.getLogger().warning("[Coins] Error occured while trying to execute update: \"" + u + "\"");
- }
- }
- public static void updateData(String table, String newData, String conditions){
- executeUpdate("UPDATE " + table + " SET " + newData + " WHERE " + conditions);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement