Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 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("coins int not null,");
  27. sb.append("primary key(uuid));");
  28.  
  29. try {
  30. getConnection().createStatement().executeUpdate(sb.toString());
  31. System.out.print("udalo sie stworzyc tabeleki");
  32. } catch (SQLException e) {
  33. e.printStackTrace();
  34. System.out.print("Nie udalo sie stworzyc tabelek");
  35.  
  36. }
  37. }
  38.  
  39. public static void loadData() throws SQLException {
  40. int i = 0;
  41. PreparedStatement ps = getConnection().prepareStatement("SELECT * FROM `users`");
  42. ResultSet rs = ps.executeQuery();
  43. while (rs.next()) {
  44. User u = User.get(UUID.fromString(rs.getString("uuid")));
  45. u.setName(rs.getString("name"));
  46. u.setCoins(rs.getInt("coins"));
  47. i++;
  48. }
  49. System.out.print("Loaded " + i + " users");
  50. }
  51.  
  52.  
  53. public static void saveData() throws SQLException {
  54. int i = 0;
  55. try {
  56. for (User u : UserUtils.getUsers()) {
  57. StringBuilder sb = new StringBuilder();
  58. sb.append("'" + u.getUuid().toString() +"',");
  59. sb.append("'" + u.getName() +"',");
  60. sb.append("'" + u.getCoins() +"'");
  61. sb.append(") ON DUPLICATE KEY UPDATE ");
  62. sb.append("name='" + u.getName() +"',");
  63. sb.append("coins='" + u.getCoins() +"';");
  64. getConnection().createStatement().executeUpdate(sb.toString());
  65.  
  66. i++;
  67. }
  68. System.out.print("Saved " + i + " users");
  69. } catch (SQLException e) {
  70. e.printStackTrace();
  71. System.out.print("Blad z zapisem danych!");
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement