Guest User

Untitled

a guest
Oct 23rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 KB | None | 0 0
  1. package me.kamisoyokaze.mcscores;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class database {
  10.     private MCscores plugin;
  11.    
  12.     private String host = plugin.getConfig_host();
  13.     private String port = plugin.getConfig_port();
  14.     private String database = plugin.getConfig_database();
  15.     private String user = plugin.getConfig_user();
  16.     private String password = plugin.getConfig_password();
  17.  
  18.     public int retrieve(String name) throws ClassNotFoundException, SQLException {
  19.         Class.forName("com.mysql.jdbc.Driver");
  20.         Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);
  21.  
  22.         PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + name + ";");
  23.         //Query
  24.  
  25.         ResultSet result = statement.executeQuery();
  26.         //variable to execute query
  27.         int score = result.getInt(3);
  28.         return score;      
  29.     }
  30.  
  31.     public void enter(String kname) throws ClassNotFoundException, SQLException {
  32.         Class.forName("com.mysql.jdbc.Driver");
  33.         Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);
  34.  
  35.         boolean exists = false;
  36.  
  37.         PreparedStatement existing = con.prepareStatement("SELECT EXISTS(SELECT * FROM highscores WHERE name = " + kname + ")");
  38.  
  39.         if(existing.executeQuery() != null) {
  40.             exists = true;
  41.         }
  42.  
  43.         if(exists){
  44.             PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + kname + ";");
  45.  
  46.             ResultSet result = statement.executeQuery();
  47.             int score = result.getInt(3);
  48.             int newscore = score++;
  49.  
  50.             PreparedStatement changeScore = con.prepareStatement("UPDATE  `highscores`.`highscores` SET  `score` =  " + newscore + "' WHERE  `highscores`.`name` =  'bawb'");
  51.             changeScore.executeQuery();
  52.             System.out.println(kname + " score updated!");
  53.         }
  54.         else {
  55.             PreparedStatement addRecord = con.prepareStatement("INSERT INTO  `highscores`.`highscores` (`id` ,`name` ,`score`) VALUES (NULL ,  " + kname + "',  '1');");
  56.             addRecord.executeQuery();
  57.             System.out.println(kname + " score updated!");
  58.         }
  59.     }
  60. }
Add Comment
Please, Sign In to add comment