Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private static HikariConfig config = new HikariConfig();
  4. private static HikariDataSource ds;
  5.  
  6. static {
  7. config.setJdbcUrl("jdbc:mysql://localhost:3306/mordziaty");
  8. config.setUsername("root");
  9. config.setPassword("");
  10. config.addDataSourceProperty("cachePrepStmts", "true");
  11. config.addDataSourceProperty("prepStmtCacheSize", "250");
  12. config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
  13. ds = new HikariDataSource(config);
  14. }
  15.  
  16. public static Connection getConnection() throws SQLException {
  17. return ds.getConnection();
  18. }
  19.  
  20. public static void createTable() throws SQLException{
  21. StringBuilder sb = new StringBuilder();
  22.  
  23. sb.append("create table if not exists users(");
  24. sb.append("uuid varchar(36) not null,");
  25. sb.append("name varchar(20) not null,");
  26. sb.append("primary key(uuid));");
  27.  
  28. try {
  29. getConnection().createStatement().executeUpdate(sb.toString());
  30. System.out.print("udalo sie stworzyc tabeleki");
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. System.out.print("Nie udalo sie stworzyc tabelek");
  34. }
  35. }
  36.  
  37. public static void loadData() throws SQLException {
  38. int i = 0;
  39. ResultSet rs = getConnection().createStatement().executeQuery("SELECT * FROM `users`");
  40. while (rs.next()) {
  41. User u = User.get(UUID.fromString(rs.getString("uuid")));
  42. u.setName(rs.getString("name"));
  43. i++;
  44. }
  45. Bukkit.getConsoleSender().sendMessage("Loaded " + i + " users");
  46. }
  47.  
  48.  
  49. public static void saveData() throws SQLException {
  50. int i = 0;
  51. for (User u : UserUtils.getUsers()) {
  52. StringBuilder sb = new StringBuilder();
  53. sb.append("INSERT INTO users (uuid, name) VALUES (");
  54. sb.append("'" + u.getUuid().toString() + "',");
  55. sb.append("'" + u.getName() + "'");
  56. sb.append(") ON DUPLICATE KEY UPDATE ");
  57. sb.append("name='" + u.getName() +"';");
  58. getConnection().createStatement().executeUpdate(sb.toString());
  59. i++;
  60. }
  61. Bukkit.getConsoleSender().sendMessage("Saved " + i + " §users");
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement