Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.kamisoyokaze.mcscores;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class database {
- private MCscores plugin;
- private String host = plugin.getConfig_host();
- private String port = plugin.getConfig_port();
- private String database = plugin.getConfig_database();
- private String user = plugin.getConfig_user();
- private String password = plugin.getConfig_password();
- public int retrieve(String name) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);
- PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + name + ";");
- //Query
- ResultSet result = statement.executeQuery();
- //variable to execute query
- int score = result.getInt(3);
- return score;
- }
- public void enter(String kname) throws ClassNotFoundException, SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "," + user + "," + password);
- boolean exists = false;
- PreparedStatement existing = con.prepareStatement("SELECT EXISTS(SELECT * FROM highscores WHERE name = " + kname + ")");
- if(existing.executeQuery() != null) {
- exists = true;
- }
- if(exists){
- PreparedStatement statement = con.prepareStatement("SELECT * FROM highscores WHERE name = " + kname + ";");
- ResultSet result = statement.executeQuery();
- int score = result.getInt(3);
- int newscore = score++;
- PreparedStatement changeScore = con.prepareStatement("UPDATE `highscores`.`highscores` SET `score` = " + newscore + "' WHERE `highscores`.`name` = 'bawb'");
- changeScore.executeQuery();
- System.out.println(kname + " score updated!");
- }
- else {
- PreparedStatement addRecord = con.prepareStatement("INSERT INTO `highscores`.`highscores` (`id` ,`name` ,`score`) VALUES (NULL , " + kname + "', '1');");
- addRecord.executeQuery();
- System.out.println(kname + " score updated!");
- }
- }
- }
Add Comment
Please, Sign In to add comment