Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
58
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.  
  38. public static void loadData() throws SQLException {
  39. int i = 0;
  40. PreparedStatement ps = getConnection().prepareStatement("SELECT * FROM `users`");
  41. ResultSet rs = ps.executeQuery();
  42. while (rs.next()) {
  43. User u = User.get(UUID.fromString(rs.getString("uuid")));
  44. u.setName(rs.getString("name"));
  45. i++;
  46. }
  47. System.out.print("Loaded " + i + " users");
  48. }
  49.  
  50.  
  51. public static void saveData() throws SQLException {
  52. int i = 0;
  53. for (User u : UserUtils.getUsers()) {
  54. StringBuilder sb = new StringBuilder();
  55. sb.append("INSERT INTO users (uuid, name) VALUES (");
  56. sb.append("'" + u.getUuid().toString() + "',");
  57. sb.append("'" + u.getName() + "'");
  58. sb.append(") ON DUPLICATE KEY UPDATE ");
  59. sb.append("name='" + u.getName() +"';");
  60. getConnection().createStatement().executeUpdate(sb.toString());
  61.  
  62. i++;
  63. }
  64. System.out.print("Saved " + i + " users");
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement