Guest User

Untitled

a guest
Jun 25th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. package me.smickles.DynamicMarket;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.SQLException;
  9. import java.util.ArrayList;
  10. import java.util.Set;
  11.  
  12. import org.bukkit.OfflinePlayer;
  13. import org.bukkit.configuration.ConfigurationSection;
  14. import org.bukkit.configuration.file.FileConfiguration;
  15. import org.bukkit.entity.Player;
  16.  
  17.  
  18.  
  19. public class MySql {
  20.  
  21. static FileConfiguration config = XBank.config;
  22. static String url = config.getString("xp.config.database");
  23. static String user = config.getString("xp.config.user");
  24. static String pass = config.getString("xp.config.password");
  25.  
  26. public static void convertYML(FileConfiguration config){
  27.  
  28. ConfigurationSection groupSection = config.getConfigurationSection("xp.user"); //saves the section we are in for re-use
  29. Set<String> list = groupSection.getKeys(false); //grabs all keys in the section
  30.  
  31.  
  32. for (String key : list) { //iterate over all keys
  33. //map.put(key, groupSection.getInt(key))
  34. try {
  35. createUserFromString(key,groupSection.getInt(key));
  36. System.out.println("Converting user " + key);
  37. } catch (SQLException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. }
  42. System.out.println("Finished converting config... You may now clear the config of users.");
  43. }
  44.  
  45. public static void createTables() throws SQLException{
  46. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  47. PreparedStatement Statement = conn.prepareStatement("CREATE TABLE IF NOT EXISTS `XBank` ( `id` int(11) NOT NULL auto_increment, `User` varchar(50) NOT NULL, `Balance` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"); //Put your query in the quotes
  48. Statement.executeUpdate(); //Executes the query
  49. Statement.close(); //Closes the query
  50. conn.close(); //Closes the connection
  51. }
  52. public static void setBalance(Player p, int i) throws SQLException{
  53. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  54. PreparedStatement Statement = conn.prepareStatement("UPDATE `XBank` SET Balance='" + i + "' WHERE User='" + p.getName() + "';"); //Put your query in the quotes
  55. Statement.executeUpdate(); //Executes the query
  56. Statement.close(); //Closes the query
  57. conn.close(); //Closes the connection
  58. }
  59. public static int getBalance(Player p) throws SQLException{
  60. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  61. Statement state = conn.createStatement();
  62. ResultSet rs = state.executeQuery("SELECT * FROM `XBank` WHERE User='" + p.getName() + "';");
  63. ArrayList<Integer> array = new ArrayList<Integer>();
  64. while(rs.next()){
  65. array.add(rs.getInt("Balance"));
  66.  
  67. }
  68. int result = Integer.parseInt(array.get(0).toString());
  69. state.close();
  70. conn.close();
  71. return result;
  72. }
  73.  
  74.  
  75. public static void setBalanceOffline(OfflinePlayer target, int i) throws SQLException{
  76. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  77. PreparedStatement Statement = conn.prepareStatement("UPDATE `XBank` SET Balance=" + i + " WHERE User='" + target.getName() + "';"); //Put your query in the quotes
  78. Statement.executeUpdate(); //Executes the query
  79. Statement.close(); //Closes the query
  80. conn.close(); //Closes the connection
  81. }
  82.  
  83. public static void createUser(Player player) throws SQLException{
  84. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  85. Statement state = conn.createStatement();
  86. final ResultSet rs = state.executeQuery("SELECT * FROM `XBank` WHERE User='"+ player.getName() + "';");
  87.  
  88.  
  89. if(rs.first())
  90. {
  91. conn.close();
  92. return;
  93. }
  94. else {
  95. PreparedStatement Statement1 = conn.prepareStatement("INSERT INTO `XBank` (`id`, `User`, `Balance`) VALUES (NULL, '"+ player.getName() + "', '0');"); //Put your query in the quotes
  96. Statement1.executeUpdate();
  97. Statement1.close();
  98. //INSERT INTO `XBank` (`id`, `User`, `Balance`) VALUES (NULL, 'TehRainbowGuy', '0');
  99. }
  100. state.close();
  101. conn.close(); //Closes the connection
  102. }
  103. public static void createUserFromString(String player, int bal) throws SQLException{
  104. Connection conn = DriverManager.getConnection(url, user, pass); //Creates the connection
  105. Statement state = conn.createStatement();
  106. final ResultSet rs = state.executeQuery("SELECT * FROM `XBank` WHERE User='"+ player + "';");
  107.  
  108.  
  109. if(rs.first())
  110. {
  111. conn.close();
  112. return;
  113. }
  114. else {
  115. PreparedStatement Statement1 = conn.prepareStatement("INSERT INTO `XBank` (`id`, `User`, `Balance`) VALUES (NULL, '"+ player + "', '"+ bal +"');"); //Put your query in the quotes
  116. Statement1.executeUpdate();
  117. Statement1.close();
  118. //INSERT INTO `XBank` (`id`, `User`, `Balance`) VALUES (NULL, 'TehRainbowGuy', '0');
  119. }
  120. state.close();
  121. conn.close(); //Closes the connection
  122. }
  123.  
  124. }
Add Comment
Please, Sign In to add comment